vr.c revision 1.59 1 /* $NetBSD: vr.c,v 1.59 2011/02/20 07:58:14 matt 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.59 2011/02/20 07:58:14 matt Exp $");
39
40 #include "opt_vr41xx.h"
41 #include "opt_tx39xx.h"
42 #include "opt_kgdb.h"
43
44 #define __INTR_PRIVATE
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/reboot.h>
49 #include <sys/device.h>
50 #include <sys/bus.h>
51 #include <sys/cpu.h>
52
53 #include <uvm/uvm_extern.h>
54
55 #include <machine/sysconf.h>
56 #include <machine/bootinfo.h>
57 #include <machine/bus_space_hpcmips.h>
58 #include <machine/platid.h>
59 #include <machine/platid_mask.h>
60
61 #include <dev/hpc/hpckbdvar.h>
62
63 #include <hpcmips/vr/vr.h>
64 #include <hpcmips/vr/vr_asm.h>
65 #include <hpcmips/vr/vrcpudef.h>
66 #include <hpcmips/vr/vripreg.h>
67 #include <hpcmips/vr/rtcreg.h>
68
69 #include <mips/cache.h>
70
71 #include "vrip_common.h"
72 #if NVRIP_COMMON > 0
73 #include <hpcmips/vr/vripvar.h>
74 #endif
75
76 #include "vrbcu.h"
77 #if NVRBCU > 0
78 #include <hpcmips/vr/bcuvar.h>
79 #endif
80
81 #include "vrdsu.h"
82 #if NVRDSU > 0
83 #include <hpcmips/vr/vrdsuvar.h>
84 #endif
85
86 #include "com.h"
87 #include "com_vrip.h"
88 #include "com_hpcio.h"
89 #if NCOM > 0
90 #include <sys/termios.h>
91 #include <sys/ttydefaults.h>
92 #include <dev/ic/comreg.h>
93 #include <dev/ic/comvar.h>
94 #if NCOM_VRIP > 0
95 #include <hpcmips/vr/siureg.h>
96 #include <hpcmips/vr/com_vripvar.h>
97 #endif
98 #if NCOM_HPCIO > 0
99 #include <hpcmips/dev/com_hpciovar.h>
100 #endif
101 #ifndef CONSPEED
102 #define CONSPEED TTYDEF_SPEED
103 #endif
104 #endif
105
106 #include "hpcfb.h"
107 #include "vrkiu.h"
108 #if (NVRKIU > 0) || (NHPCFB > 0)
109 #include <dev/wscons/wsdisplayvar.h>
110 #include <dev/rasops/rasops.h>
111 #endif
112
113 #if NHPCFB > 0
114 #include <dev/hpc/hpcfbvar.h>
115 #endif
116
117 #if NVRKIU > 0
118 #include <arch/hpcmips/vr/vrkiureg.h>
119 #include <arch/hpcmips/vr/vrkiuvar.h>
120 #endif
121
122 #ifdef DEBUG
123 #define STATIC
124 #else
125 #define STATIC static
126 #endif
127
128 /*
129 * This is a mask of bits to clear in the SR when we go to a
130 * given interrupt priority level.
131 */
132 const struct ipl_sr_map __ipl_sr_map_vr = {
133 .sr_bits = {
134 [IPL_NONE] = 0,
135 [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
136 [IPL_SOFTNET] = MIPS_SOFT_INT_MASK,
137 [IPL_VM] = MIPS_SOFT_INT_MASK
138 | MIPS_INT_MASK_0,
139 [IPL_SCHED] = MIPS_SOFT_INT_MASK
140 | MIPS_INT_MASK_0
141 | MIPS_INT_MASK_1,
142 [IPL_DDB] = MIPS_INT_MASK,
143 [IPL_VM] = MIPS_INT_MASK,
144 },
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(int, vaddr_t, uint32_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 *, uint32_t, uint32_t);
166 STATIC int (*vr_intr_handler[4])(void *, uint32_t, uint32_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(void)
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(hpcmips_cpuname, "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(hpcmips_cpuname, "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 /*
348 XXX see port-hpcmips/42934
349 if (badaddr(page, 4))
350 goto bad;
351 */
352
353 /* stop memory probing at first memory image */
354 if (memcmp(page, (void *)MIPS_PHYS_TO_KSEG0(0), 128) == 0)
355 return;
356
357 *(volatile int *)(page+0) = 0xa5a5a5a5;
358 *(volatile int *)(page+4) = 0x5a5a5a5a;
359 wbflush();
360 if (*(volatile int *)(page+0) != 0xa5a5a5a5)
361 goto bad;
362
363 *(volatile int *)(page+0) = 0x5a5a5a5a;
364 *(volatile int *)(page+4) = 0xa5a5a5a5;
365 wbflush();
366 if (*(volatile int *)(page+0) != 0x5a5a5a5a)
367 goto bad;
368
369 #ifdef NARLY_MEMORY_PROBE
370 x = random();
371 for (i = 0; i < PAGE_SIZE; i += 4)
372 *(volatile int *)(page+i) = (x ^ i);
373 wbflush();
374 for (i = 0; i < PAGE_SIZE; i += 4)
375 if (*(volatile int *)(page+i) != (x ^ i))
376 goto bad;
377
378 x = random();
379 for (i = 0; i < PAGE_SIZE; i += 4)
380 *(volatile int *)(page+i) = (x ^ i);
381 wbflush();
382 for (i = 0; i < PAGE_SIZE; i += 4)
383 if (*(volatile int *)(page+i) != (x ^ i))
384 goto bad;
385 #endif /* NARLY_MEMORY_PROBE */
386
387 if (!mem_clusters[n].size)
388 mem_clusters[n].start = addr;
389 mem_clusters[n].size += PAGE_SIZE;
390 continue;
391
392 bad:
393 if (mem_clusters[n].size)
394 ++n;
395 continue;
396 }
397 if (mem_clusters[n].size)
398 ++n;
399 mem_cluster_cnt = n;
400 }
401
402 void
403 vr_fb_init(void **kernend)
404 {
405 /* Nothing to do */
406 }
407
408 void
409 vr_cons_init(void)
410 {
411 #if NCOM > 0 || NHPCFB > 0 || NVRKIU > 0
412 bus_space_tag_t iot = hpcmips_system_bus_space();
413 #endif
414 #if NCOM > 0
415 static struct vr_com_platdep *com_info;
416 #endif
417 #if NVRKIU > 0
418 static struct vr_kiu_platdep *kiu_info;
419 #endif
420
421 #if NCOM > 0
422 com_info = platid_search(&platid, platdep_com_table,
423 sizeof(platdep_com_table)/sizeof(*platdep_com_table),
424 sizeof(*platdep_com_table));
425 #ifdef KGDB
426 if (com_info->attach != NULL) {
427 /* if KGDB is defined, always use the serial port for KGDB */
428 if ((*com_info->attach)(iot, com_info->addr, 9600,
429 com_info->freq,
430 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 1)) {
431 printf("%s(%d): can't init kgdb's serial port",
432 __FILE__, __LINE__);
433 }
434 }
435 #else /* KGDB */
436 if (com_info->attach != NULL && (bootinfo->bi_cnuse&BI_CNUSE_SERIAL)) {
437 /* Serial console */
438 if ((*com_info->attach)(iot, com_info->addr, CONSPEED,
439 com_info->freq,
440 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8, 0)) {
441 printf("%s(%d): can't init serial console",
442 __FILE__, __LINE__);
443 } else {
444 return;
445 }
446 }
447 #endif /* KGDB */
448 #endif /* NCOM > 0 */
449
450 #if NHPCFB > 0
451 if (hpcfb_cnattach(NULL)) {
452 printf("%s(%d): can't init fb console", __FILE__, __LINE__);
453 } else {
454 goto find_keyboard;
455 }
456 find_keyboard:
457 #endif /* NHPCFB > 0 */
458
459 #if NVRKIU > 0
460 kiu_info = platid_search(&platid, platdep_kiu_table,
461 sizeof(platdep_kiu_table)/sizeof(*platdep_kiu_table),
462 sizeof(*platdep_kiu_table));
463 if (kiu_info->addr != VRIP_NO_ADDR) {
464 if (vrkiu_cnattach(iot, kiu_info->addr)) {
465 printf("%s(%d): can't init vrkiu as console",
466 __FILE__, __LINE__);
467 } else {
468 return;
469 }
470 }
471 #endif /* NVRKIU > 0 */
472 }
473
474 extern char vr_hibernate[];
475 extern char evr_hibernate[];
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 /*
486 * copy vr_hibernate() to top of physical memory.
487 */
488 memcpy((void *)MIPS_KSEG0_START, vr_hibernate,
489 evr_hibernate - (char *)vr_hibernate);
490 /* sync I&D cache */
491 mips_dcache_wbinv_all();
492 mips_icache_sync_all();
493 /*
494 * call vr_hibernate() at MIPS_KSEG0_START.
495 */
496 ((void (*)(void *,int))MIPS_KSEG0_START)(
497 (void *)MIPS_KSEG0_START, ptoa(physmem));
498 /* not reach */
499 vr_reboot(howto&~RB_HALT, bootstr);
500 }
501 /*
502 * halt
503 */
504 if (howto & RB_HALT) {
505 #if NVRIP_COMMON > 0
506 _spllower(~MIPS_INT_MASK_0);
507 vrip_intr_suspend();
508 #else
509 splhigh();
510 #endif
511 __asm(".set noreorder");
512 __asm(".word " ___STRING(VR_OPCODE_SUSPEND));
513 __asm("nop");
514 __asm("nop");
515 __asm("nop");
516 __asm("nop");
517 __asm("nop");
518 __asm(".set reorder");
519 #if NVRIP_COMMON > 0
520 vrip_intr_resume();
521 #endif
522 }
523 /*
524 * reset
525 */
526 #if NVRDSU
527 vrdsu_reset();
528 #else
529 printf("%s(%d): There is no DSU.", __FILE__, __LINE__);
530 #endif
531 }
532
533 /*
534 * Handle interrupts.
535 */
536 void
537 VR_INTR(int ppl, vaddr_t pc, uint32_t status)
538 {
539 uint32_t ipending;
540 int ipl;
541
542 while (ppl < (ipl = splintr(&ipending))) {
543 /* Deal with unneded compare interrupts occasionally so that
544 * we can keep spllowersoftclock. */
545 if (ipending & MIPS_INT_MASK_5) {
546 mips3_cp0_compare_write(0);
547 }
548
549 if (ipending & MIPS_INT_MASK_1) {
550 (*vr_intr_handler[1])(vr_intr_arg[1], pc, ipending);
551 }
552
553 if (ipending & MIPS_INT_MASK_0) {
554 (*vr_intr_handler[0])(vr_intr_arg[0], pc, status);
555 }
556 }
557 }
558
559 void *
560 vr_intr_establish(int line, int (*ih_fun)(void *, uint32_t, uint32_t),
561 void *ih_arg)
562 {
563
564 KDASSERT(vr_intr_handler[line] == vr_null_handler);
565
566 vr_intr_handler[line] = ih_fun;
567 vr_intr_arg[line] = ih_arg;
568
569 return ((void *)line);
570 }
571
572 void
573 vr_intr_disestablish(void *ih)
574 {
575 int line = (int)ih;
576
577 vr_intr_handler[line] = vr_null_handler;
578 vr_intr_arg[line] = NULL;
579 }
580
581 int
582 vr_null_handler(void *arg, uint32_t pc, uint32_t status)
583 {
584
585 printf("vr_null_handler\n");
586
587 return (0);
588 }
589
590 /*
591 int x4181 = VR4181;
592 int x4101 = VR4101;
593 int x4102 = VR4102;
594 int x4111 = VR4111;
595 int x4121 = VR4121;
596 int x4122 = VR4122;
597 int xo4181 = ONLY_VR4181;
598 int xo4101 = ONLY_VR4101;
599 int xo4102 = ONLY_VR4102;
600 int xo4111_4121 = ONLY_VR4111_4121;
601 int g4101=VRGROUP_4101;
602 int g4102=VRGROUP_4102;
603 int g4181=VRGROUP_4181;
604 int g4102_4121=VRGROUP_4102_4121;
605 int g4111_4121=VRGROUP_4111_4121;
606 int g4102_4122=VRGROUP_4102_4122;
607 int g4111_4122=VRGROUP_4111_4122;
608 int single_vrip_base=SINGLE_VRIP_BASE;
609 int vrip_base_addr=VRIP_BASE_ADDR;
610 */
611