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