dec_3min.c revision 1.26
1/* $NetBSD: dec_3min.c,v 1.26 1999/11/25 01:40:22 simonb Exp $ */
2
3/*
4 * Copyright (c) 1998 Jonathan Stone.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Jonathan Stone for
17 *      the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1988 University of Utah.
35 * Copyright (c) 1992, 1993
36 *	The Regents of the University of California.  All rights reserved.
37 *
38 * This code is derived from software contributed to Berkeley by
39 * the Systems Programming Group of the University of Utah Computer
40 * Science Department, The Mach Operating System project at
41 * Carnegie-Mellon University and Ralph Campbell.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *	This product includes software developed by the University of
54 *	California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 *    may be used to endorse or promote products derived from this software
57 *    without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
72 */
73
74#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
75
76__KERNEL_RCSID(0, "$NetBSD: dec_3min.c,v 1.26 1999/11/25 01:40:22 simonb Exp $");
77
78
79#include <sys/types.h>
80#include <sys/systm.h>
81
82#include <machine/cpu.h>
83#include <machine/intr.h>
84#include <machine/reg.h>
85#include <machine/psl.h>
86#include <machine/sysconf.h>
87
88#include <mips/mips/mips_mcclock.h>	/* mcclock CPUspeed estimation */
89
90/* all these to get ioasic_base */
91#include <sys/device.h>			/* struct cfdata for.. */
92#include <dev/tc/tcvar.h>		/* tc type definitions for.. */
93#include <dev/tc/ioasicreg.h>		/* ioasic interrrupt masks */
94#include <dev/tc/ioasicvar.h>		/* ioasic_base */
95
96#include <pmax/pmax/turbochannel.h>
97#include <pmax/pmax/machdep.h>
98
99#include <pmax/pmax/kmin.h>		/* 3min baseboard addresses */
100#include <pmax/pmax/memc.h>		/* 3min/maxine memory errors */
101
102
103/*
104 * forward declarations
105 */
106void		dec_3min_init __P((void));
107void		dec_3min_bus_reset __P((void));
108
109void		dec_3min_enable_intr
110		   __P ((unsigned slotno, int (*handler)(void *),
111			 void *sc, int onoff));
112int		dec_3min_intr __P((unsigned, unsigned, unsigned, unsigned));
113void		dec_3min_device_register __P((struct device *, void *));
114void		dec_3min_cons_init __P((void));
115
116
117/*
118 * Local declarations.
119 */
120void dec_3min_mcclock_cpuspeed __P((volatile struct chiptime *mcclock_addr,
121			       int clockmask));
122void kn02ba_wbflush __P((void));
123unsigned kn02ba_clkread __P((void));
124
125static u_int32_t kmin_tc3_imask;
126
127#ifdef MIPS3
128static unsigned latched_cycle_cnt;
129extern u_int32_t mips3_cycle_count __P((void));
130#endif
131
132
133void
134dec_3min_init()
135{
136	extern char cpu_model[];
137	extern int physmem_boardmax;
138
139	platform.iobus = "tcbus";
140	platform.bus_reset = dec_3min_bus_reset;
141	platform.cons_init = dec_3min_cons_init;
142	platform.device_register = dec_3min_device_register;
143	platform.iointr = dec_3min_intr;
144	platform.memsize = memsize_scan;
145	platform.clkread = kn02ba_clkread;
146
147	/* clear any memory errors */
148	*(u_int32_t *)MIPS_PHYS_TO_KSEG1(KMIN_REG_TIMEOUT) = 0;
149	kn02ba_wbflush();
150
151	ioasic_base = MIPS_PHYS_TO_KSEG1(KMIN_SYS_ASIC);
152	mips_hardware_intr = dec_3min_intr;
153	tc_enable_interrupt = dec_3min_enable_intr;
154
155	/*
156	 * Since all the motherboard interrupts come through the
157	 * IOASIC, it has to be turned off for all the spls and
158	 * since we don't know what kinds of devices are in the
159	 * TURBOchannel option slots, just splhigh().
160	 */
161	splvec.splbio = MIPS_SPL_0_1_2_3;
162	splvec.splnet = MIPS_SPL_0_1_2_3;
163	splvec.spltty = MIPS_SPL_0_1_2_3;
164	splvec.splimp = MIPS_SPL_0_1_2_3;
165	splvec.splclock = MIPS_SPL_0_1_2_3;
166	splvec.splstatclock = MIPS_SPL_0_1_2_3;
167
168	/* calibrate cpu_mhz value */
169	dec_3min_mcclock_cpuspeed(
170	    (void *)(ioasic_base + IOASIC_SLOT_8_START), MIPS_INT_MASK_3);
171
172	*(u_int32_t *)(ioasic_base + IOASIC_LANCE_DECODE) = 0x3;
173	*(u_int32_t *)(ioasic_base + IOASIC_SCSI_DECODE) = 0xe;
174#if 0
175	*(u_int32_t *)(ioasic_base + IOASIC_SCC0_DECODE) = (0x10|4);
176	*(u_int32_t *)(ioasic_base + IOASIC_SCC1_DECODE) = (0x10|6);
177	*(u_int32_t *)(ioasic_base + IOASIC_CSR) = 0x00000f00;
178#endif
179
180	/* sanitize interrupt mask */
181	kmin_tc3_imask = (KMIN_INTR_CLOCK|KMIN_INTR_PSWARN|KMIN_INTR_TIMEOUT);
182	*(u_int32_t *)(ioasic_base + IOASIC_INTR) = 0;
183	*(u_int32_t *)(ioasic_base + IOASIC_IMSK) = kmin_tc3_imask;
184
185	/*
186	 * The kmin memory hardware seems to wrap memory addresses
187	 * with 4Mbyte SIMMs, which causes the physmem computation
188	 * to lose.  Find out how big the SIMMS are and set
189	 * max_ physmem accordingly.
190	 * XXX Do MAXINEs lose the same way?
191	 */
192	physmem_boardmax = KMIN_PHYS_MEMORY_END + 1;
193	if ((KMIN_MSR_SIZE_16Mb & *(int *)MIPS_PHYS_TO_KSEG1(KMIN_REG_MSR))
194			== 0)
195		physmem_boardmax = physmem_boardmax >> 2;
196	physmem_boardmax = MIPS_PHYS_TO_KSEG1(physmem_boardmax);
197
198	sprintf(cpu_model, "DECstation 5000/1%d (3MIN)", cpu_mhz);
199}
200
201/*
202 * Initalize the memory system and I/O buses.
203 */
204void
205dec_3min_bus_reset()
206{
207
208	/*
209	 * Reset interrupts, clear any errors from newconf probes
210	 */
211
212	*(u_int32_t *)MIPS_PHYS_TO_KSEG1(KMIN_REG_TIMEOUT) = 0;
213	kn02ba_wbflush();
214
215	*(u_int32_t *)(ioasic_base + IOASIC_INTR) = 0;
216	kn02ba_wbflush();
217}
218
219void
220dec_3min_cons_init()
221{
222	/* notyet */
223}
224
225
226void
227dec_3min_device_register(dev, aux)
228	struct device *dev;
229	void *aux;
230{
231	panic("dec_3min_device_register unimplemented");
232}
233
234
235void
236dec_3min_enable_intr(slotno, handler, sc, on)
237	unsigned int slotno;
238	int (*handler) __P((void *));
239	void *sc;
240	int on;
241{
242	unsigned mask;
243
244	switch (slotno) {
245		/* slots 0-2 don't interrupt through the IOASIC. */
246	case 0:
247		mask = MIPS_INT_MASK_0;	break;
248	case 1:
249		mask = MIPS_INT_MASK_1; break;
250	case 2:
251		mask = MIPS_INT_MASK_2; break;
252
253	case KMIN_SCSI_SLOT:
254		mask = (IOASIC_INTR_SCSI | IOASIC_INTR_SCSI_PTR_LOAD |
255			IOASIC_INTR_SCSI_OVRUN | IOASIC_INTR_SCSI_READ_E);
256		break;
257
258	case KMIN_LANCE_SLOT:
259		mask = KMIN_INTR_LANCE;
260		break;
261	case KMIN_SCC0_SLOT:
262		mask = KMIN_INTR_SCC_0;
263		break;
264	case KMIN_SCC1_SLOT:
265		mask = KMIN_INTR_SCC_1;
266		break;
267	case KMIN_ASIC_SLOT:
268		mask = KMIN_INTR_ASIC;
269		break;
270	default:
271		return;
272	}
273
274#if defined(DEBUG) || defined(DIAGNOSTIC)
275	printf("3MIN: imask %x, %sabling slot %d, sc %p handler %p\n",
276	       kmin_tc3_imask, (on? "en" : "dis"), slotno, sc, handler);
277#endif
278
279	/*
280	 * Enable the interrupt  handler, and if it's an IOASIC
281	 * slot, set the IOASIC interrupt mask.
282	 * Otherwise, set the appropriate spl level in the R3000
283	 * register.
284	 * Be careful to set handlers  before enabling, and disable
285	 * interrupts before clearing handlers.
286	 */
287
288	if (on) {
289		/* Set the interrupt handler and argument ... */
290		tc_slot_info[slotno].intr = handler;
291		tc_slot_info[slotno].sc = sc;
292
293		/* ... and set the relevant mask */
294		if (slotno <= 2) {
295			/* it's an option slot */
296			int s = splhigh();
297			s  |= mask;
298			splx(s);
299		} else {
300			/* it's a baseboard device going via the ASIC */
301			kmin_tc3_imask |= mask;
302		}
303	} else {
304		/* Clear the relevant mask... */
305		if (slotno <= 2) {
306			/* it's an option slot */
307			int s = splhigh();
308			printf("kmin_intr: cannot disable option slot %d\n",
309			    slotno);
310			s &= ~mask;
311			splx(s);
312		} else {
313			/* it's a baseboard device going via the ASIC */
314			kmin_tc3_imask &= ~mask;
315		}
316		/* ... and clear the handler */
317		tc_slot_info[slotno].intr = 0;
318		tc_slot_info[slotno].sc = 0;
319	}
320	*(u_int32_t *)(ioasic_base + IOASIC_IMSK) = kmin_tc3_imask;
321	kn02ba_wbflush();
322}
323
324
325
326/*
327 * 3min hardware interrupts. (DECstation 5000/1xx)
328 */
329int
330dec_3min_intr(cpumask, pc, status, cause)
331	unsigned cpumask;
332	unsigned pc;
333	unsigned status;
334	unsigned cause;
335{
336	static int user_warned = 0;
337	static int intr_depth = 0;
338	u_int32_t old_mask;
339
340	intr_depth++;
341	old_mask = *(u_int32_t *)(ioasic_base + IOASIC_IMSK);
342
343	if (cpumask & MIPS_INT_MASK_4)
344		prom_haltbutton();
345
346	if (cpumask & MIPS_INT_MASK_3) {
347		/* NB: status & MIPS_INT_MASK3 must also be set */
348		/* masked interrupts are still observable */
349		u_int32_t intr, imsk, turnoff;
350
351		turnoff = 0;
352		intr = *(u_int32_t *)(ioasic_base + IOASIC_INTR);
353		imsk = *(u_int32_t *)(ioasic_base + IOASIC_IMSK);
354		intr &= imsk;
355
356		if (intr & IOASIC_INTR_SCSI_PTR_LOAD) {
357			turnoff |= IOASIC_INTR_SCSI_PTR_LOAD;
358#ifdef notdef
359			asc_dma_intr();
360#endif
361		}
362
363		if (intr & (IOASIC_INTR_SCSI_OVRUN | IOASIC_INTR_SCSI_READ_E))
364			turnoff |= IOASIC_INTR_SCSI_OVRUN | IOASIC_INTR_SCSI_READ_E;
365
366		if (intr & IOASIC_INTR_LANCE_READ_E)
367			turnoff |= IOASIC_INTR_LANCE_READ_E;
368
369		if (turnoff)
370			*(u_int32_t *)(ioasic_base + IOASIC_INTR) = ~turnoff;
371
372		if (intr & KMIN_INTR_TIMEOUT)
373			kn02ba_errintr();
374
375		if (intr & KMIN_INTR_CLOCK) {
376			struct clockframe cf;
377
378			__asm __volatile("lbu $0,48(%0)" ::
379				"r"(ioasic_base + IOASIC_SLOT_8_START));
380#ifdef MIPS3
381			if (CPUISMIPS3) {
382				latched_cycle_cnt = mips3_cycle_count();
383			}
384#endif
385			cf.pc = pc;
386			cf.sr = status;
387			hardclock(&cf);
388			intrcnt[HARDCLOCK]++;
389		}
390
391		/* If clock interrups were enabled, re-enable them ASAP. */
392		if (old_mask & KMIN_INTR_CLOCK) {
393			/* ioctl interrupt mask to splclock and higher */
394			*(u_int32_t *)(ioasic_base + IOASIC_IMSK)
395				= old_mask &
396					~(KMIN_INTR_SCC_0|KMIN_INTR_SCC_1 |
397					  IOASIC_INTR_LANCE|IOASIC_INTR_SCSI);
398			kn02ba_wbflush();
399			_splset(MIPS_SR_INT_IE | (status & MIPS_INT_MASK_3));
400		}
401
402		if (intr_depth > 1)
403			 goto done;
404
405		if ((intr & KMIN_INTR_SCC_0) &&
406		    tc_slot_info[KMIN_SCC0_SLOT].intr) {
407			(*(tc_slot_info[KMIN_SCC0_SLOT].intr))
408			  (tc_slot_info[KMIN_SCC0_SLOT].sc);
409			intrcnt[SERIAL0_INTR]++;
410		}
411
412		if ((intr & KMIN_INTR_SCC_1) &&
413		    tc_slot_info[KMIN_SCC1_SLOT].intr) {
414			(*(tc_slot_info[KMIN_SCC1_SLOT].intr))
415			  (tc_slot_info[KMIN_SCC1_SLOT].sc);
416			intrcnt[SERIAL1_INTR]++;
417		}
418
419#ifdef notyet /* untested */
420		/* If tty interrupts were enabled, re-enable them ASAP. */
421		if ((old_mask & (KMIN_INTR_SCC_1|KMIN_INTR_SCC_0)) ==
422		     (KMIN_INTR_SCC_1|KMIN_INTR_SCC_0)) {
423			*imaskp = old_mask &
424			  ~(KMIN_INTR_SCC_0|KMIN_INTR_SCC_1 |
425			  IOASIC_INTR_LANCE|IOASIC_INTR_SCSI);
426			kn02ba_wbflush();
427		}
428
429		/* XXX until we know about SPLs of TC options. */
430		if (intr_depth > 1)
431			 goto done;
432#endif
433		if ((intr & IOASIC_INTR_LANCE) &&
434		    tc_slot_info[KMIN_LANCE_SLOT].intr) {
435			(*(tc_slot_info[KMIN_LANCE_SLOT].intr))
436			  (tc_slot_info[KMIN_LANCE_SLOT].sc);
437			intrcnt[LANCE_INTR]++;
438		}
439
440		if ((intr & IOASIC_INTR_SCSI) &&
441		    tc_slot_info[KMIN_SCSI_SLOT].intr) {
442			(*(tc_slot_info[KMIN_SCSI_SLOT].intr))
443			  (tc_slot_info[KMIN_SCSI_SLOT].sc);
444			intrcnt[SCSI_INTR]++;
445		}
446
447		if (user_warned && ((intr & KMIN_INTR_PSWARN) == 0)) {
448			printf("%s\n", "Power supply ok now.");
449			user_warned = 0;
450		}
451		if ((intr & KMIN_INTR_PSWARN) && (user_warned < 3)) {
452			user_warned++;
453			printf("%s\n", "Power supply overheating");
454		}
455	}
456	if ((cpumask & MIPS_INT_MASK_0) && tc_slot_info[0].intr) {
457		(*tc_slot_info[0].intr)(tc_slot_info[0].sc);
458		intrcnt[SLOT0_INTR]++;
459 	}
460
461	if ((cpumask & MIPS_INT_MASK_1) && tc_slot_info[1].intr) {
462		(*tc_slot_info[1].intr)(tc_slot_info[1].sc);
463		intrcnt[SLOT1_INTR]++;
464	}
465	if ((cpumask & MIPS_INT_MASK_2) && tc_slot_info[2].intr) {
466		(*tc_slot_info[2].intr)(tc_slot_info[2].sc);
467		intrcnt[SLOT2_INTR]++;
468	}
469
470done:
471	/* restore entry state */
472	splhigh();
473	intr_depth--;
474	*(u_int32_t *)(ioasic_base + IOASIC_IMSK) = old_mask;
475
476
477	return (MIPS_SR_INT_IE | (status & ~cause & MIPS_HARD_INT_MASK));
478}
479
480
481
482/*
483 ************************************************************************
484 * Extra functions
485 ************************************************************************
486 */
487
488
489
490
491/*
492 * Count instructions between 4ms mcclock interrupt requests,
493 * using the ioasic clock-interrupt-pending bit to determine
494 * when clock ticks occur.
495 * Set up iosiac to allow only clock interrupts, then
496 * call
497 */
498void
499dec_3min_mcclock_cpuspeed(mcclock_addr, clockmask)
500	volatile struct chiptime *mcclock_addr;
501	int clockmask;
502{
503	u_int32_t saved_imask;
504
505	saved_imask = *(u_int32_t *)(ioasic_base + IOASIC_IMSK);
506
507	/* Allow only clock interrupts through ioasic. */
508	*(u_int32_t *)(ioasic_base + IOASIC_IMSK) = KMIN_INTR_CLOCK;
509	kn02ba_wbflush();
510
511	mc_cpuspeed(mcclock_addr, clockmask);
512
513	*(u_int32_t *)(ioasic_base + IOASIC_IMSK) = saved_imask;
514	kn02ba_wbflush();
515}
516
517void
518kn02ba_wbflush()
519{
520	/* read twice IOASIC_IMSK */
521	__asm __volatile("lw $0,%0; lw $0,%0" :: "i"(0xbc040120));
522}
523
524unsigned
525kn02ba_clkread()
526{
527#ifdef MIPS3
528	if (CPUISMIPS3) {
529		u_int32_t mips3_cycles;
530
531		mips3_cycles = mips3_cycle_count() - latched_cycle_cnt;
532		/* XXX divides take 78 cycles: approximate with * 41/2048 */
533		return((mips3_cycles >> 6) + (mips3_cycles >> 8) +
534		       (mips3_cycles >> 11));
535	}
536#endif
537	return 0;
538}
539