xref: /netbsd-src/sys/arch/algor/algor/algor_p4032_intr.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: algor_p4032_intr.c,v 1.12 2006/09/05 01:33:24 gdamore Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Platform-specific interrupt support for the Algorithmics P-4032.
41  *
42  * The Algorithmics P-4032 has an interrupt controller that is pretty
43  * flexible -- it can take an interrupt source and route it to an
44  * arbitrary MIPS CPU hardware interrupt pin.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: algor_p4032_intr.c,v 1.12 2006/09/05 01:33:24 gdamore Exp $");
49 
50 #include "opt_ddb.h"
51 
52 #include <sys/param.h>
53 #include <sys/queue.h>
54 #include <sys/malloc.h>
55 #include <sys/systm.h>
56 #include <sys/device.h>
57 #include <sys/kernel.h>
58 
59 #include <machine/bus.h>
60 #include <machine/autoconf.h>
61 #include <machine/intr.h>
62 
63 #include <mips/locore.h>
64 
65 #include <dev/ic/mc146818reg.h>
66 
67 #include <algor/algor/algor_p4032reg.h>
68 #include <algor/algor/algor_p4032var.h>
69 
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 
73 #define	REGVAL(x)	*((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
74 
75 struct p4032_irqreg {
76 	bus_addr_t	addr;
77 	u_int32_t	val;
78 };
79 
80 #define	IRQREG_8BIT		0
81 #define	IRQREG_ERROR		1
82 #define	IRQREG_PCI		2
83 #define	NIRQREG			3
84 
85 struct p4032_irqreg p4032_irqregs[NIRQREG] = {
86 	{ P4032_IRR0,		0 },
87 	{ P4032_IRR1,		0 },
88 	{ P4032_IRR2,		0 },
89 };
90 
91 #define	NSTEERREG		3
92 
93 struct p4032_irqreg p4032_irqsteer[NSTEERREG] = {
94 	{ P4032_XBAR0,		0 },
95 	{ P4032_XBAR1,		0 },
96 	{ P4032_XBAR2,		0 },
97 };
98 
99 #define	NPCIIRQS		4
100 
101 /* See algor_p4032var.h */
102 #define	N8BITIRQS		8
103 
104 #define	IRQMAP_PCIBASE		0
105 #define	IRQMAP_8BITBASE		NPCIIRQS
106 #define	NIRQMAPS		(IRQMAP_8BITBASE + N8BITIRQS)
107 
108 const char *p4032_intrnames[NIRQMAPS] = {
109 	/*
110 	 * PCI INTERRUPTS
111 	 */
112 	"PCIIRQ 0",
113 	"PCIIRQ 1",
114 	"PCIIRQ 2",
115 	"PCIIRQ 3",
116 
117 	/*
118 	 * 8-BIT DEVICE INTERRUPTS
119 	 */
120 	"PCI ctlr",
121 	"floppy",
122 	"pckbc",
123 	"com 1",
124 	"com 2",
125 	"centronics",
126 	"gpio",
127 	"mcclock",
128 };
129 
130 struct p4032_irqmap {
131 	int	irqidx;
132 	int	cpuintr;
133 	int	irqreg;
134 	int	irqbit;
135 	int	xbarreg;
136 	int	xbarshift;
137 };
138 
139 const struct p4032_irqmap p4032_irqmap[NIRQMAPS] = {
140 	/*
141 	 * PCI INTERRUPTS
142 	 */
143 	/* PCIIRQ 0 */
144 	{ 0,			0,
145 	  IRQREG_PCI,		IRR2_PCIIRQ0,
146 	  2,			0 },
147 
148 	/* PCIIRQ 1 */
149 	{ 1,			0,
150 	  IRQREG_PCI,		IRR2_PCIIRQ1,
151 	  2,			2 },
152 
153 	/* PCIIRQ 2 */
154 	{ 2,			0,
155 	  IRQREG_PCI,		IRR2_PCIIRQ2,
156 	  2,			4 },
157 
158 	/* PCIIRQ 3 */
159 	{ 3,			0,
160 	  IRQREG_PCI,		IRR2_PCIIRQ3,
161 	  2,			6 },
162 
163 	/*
164 	 * 8-BIT DEVICE INTERRUPTS
165 	 */
166 	{ P4032_IRQ_PCICTLR,	1,
167 	  IRQREG_8BIT,		IRR0_PCICTLR,
168 	  0,			0 },
169 
170 	{ P4032_IRQ_FLOPPY,	1,
171 	  IRQREG_8BIT,		IRR0_FLOPPY,
172 	  0,			2 },
173 
174 	{ P4032_IRQ_PCKBC,	1,
175 	  IRQREG_8BIT,		IRR0_PCKBC,
176 	  0,			4 },
177 
178 	{ P4032_IRQ_COM1,	1,
179 	  IRQREG_8BIT,		IRR0_COM1,
180 	  0,			6 },
181 
182 	{ P4032_IRQ_COM2,	1,
183 	  IRQREG_8BIT,		IRR0_COM2,
184 	  1,			0 },
185 
186 	{ P4032_IRQ_LPT,	1,
187 	  IRQREG_8BIT,		IRR0_LPT,
188 	  1,			2 },
189 
190 	{ P4032_IRQ_GPIO,	1,
191 	  IRQREG_8BIT,		IRR0_GPIO,
192 	  1,			4 },
193 
194 	{ P4032_IRQ_RTC,	1,
195 	  IRQREG_8BIT,		IRR0_RTC,
196 	  1,			6 },
197 };
198 
199 struct p4032_intrhead {
200 	struct evcnt intr_count;
201 	int intr_refcnt;
202 };
203 struct p4032_intrhead p4032_intrtab[NIRQMAPS];
204 
205 #define	NINTRS			2	/* MIPS INT0 - INT1 */
206 
207 /*
208  * This is a mask of bits to clear in the SR when we go to a
209  * given software interrupt priority level.
210  * Hardware ipls are port/board specific.
211  */
212 const uint32_t mips_ipl_si_to_sr[_IPL_NSOFT] = {
213 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFT */
214 	MIPS_SOFT_INT_MASK_0,			/* IPL_SOFTCLOCK */
215 	MIPS_SOFT_INT_MASK_1,			/* IPL_SOFTNET */
216 	MIPS_SOFT_INT_MASK_1,			/* IPL_SOFTSERIAL */
217 };
218 
219 struct p4032_cpuintr {
220 	LIST_HEAD(, algor_intrhand) cintr_list;
221 	struct evcnt cintr_count;
222 };
223 
224 struct p4032_cpuintr p4032_cpuintrs[NINTRS];
225 const char *p4032_cpuintrnames[NINTRS] = {
226 	"int 0 (pci)",
227 	"int 1 (8-bit)",
228 };
229 
230 const char *p4032_intrgroups[NINTRS] = {
231 	"pci",
232 	"8-bit",
233 };
234 
235 void	*algor_p4032_intr_establish(int, int (*)(void *), void *);
236 void	algor_p4032_intr_disestablish(void *);
237 
238 int	algor_p4032_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
239 const char *algor_p4032_pci_intr_string(void *, pci_intr_handle_t);
240 const struct evcnt *algor_p4032_pci_intr_evcnt(void *, pci_intr_handle_t);
241 void	*algor_p4032_pci_intr_establish(void *, pci_intr_handle_t, int,
242 	    int (*)(void *), void *);
243 void	algor_p4032_pci_intr_disestablish(void *, void *);
244 void	algor_p4032_pci_conf_interrupt(void *, int, int, int, int, int *);
245 
246 void	algor_p4032_iointr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
247 
248 void
249 algor_p4032_intr_init(struct p4032_config *acp)
250 {
251 	const struct p4032_irqmap *irqmap;
252 	int i;
253 
254 	for (i = 0; i < NIRQREG; i++)
255 		REGVAL(p4032_irqregs[i].addr) = p4032_irqregs[i].val;
256 
257 	for (i = 0; i < NINTRS; i++) {
258 		LIST_INIT(&p4032_cpuintrs[i].cintr_list);
259 		evcnt_attach_dynamic(&p4032_cpuintrs[i].cintr_count,
260 		    EVCNT_TYPE_INTR, NULL, "mips", p4032_cpuintrnames[i]);
261 	}
262 	evcnt_attach_static(&mips_int5_evcnt);
263 
264 	for (i = 0; i < NIRQMAPS; i++) {
265 		irqmap = &p4032_irqmap[i];
266 
267 		p4032_irqsteer[irqmap->xbarreg].val |=
268 		    irqmap->cpuintr << irqmap->xbarshift;
269 
270 		evcnt_attach_dynamic(&p4032_intrtab[i].intr_count,
271 		    EVCNT_TYPE_INTR, NULL, p4032_intrgroups[irqmap->cpuintr],
272 		    p4032_intrnames[i]);
273 	}
274 
275 	for (i = 0; i < NSTEERREG; i++)
276 		REGVAL(p4032_irqsteer[i].addr) = p4032_irqsteer[i].val;
277 
278 	acp->ac_pc.pc_intr_v = NULL;
279 	acp->ac_pc.pc_intr_map = algor_p4032_pci_intr_map;
280 	acp->ac_pc.pc_intr_string = algor_p4032_pci_intr_string;
281 	acp->ac_pc.pc_intr_evcnt = algor_p4032_pci_intr_evcnt;
282 	acp->ac_pc.pc_intr_establish = algor_p4032_pci_intr_establish;
283 	acp->ac_pc.pc_intr_disestablish = algor_p4032_pci_intr_disestablish;
284 	acp->ac_pc.pc_conf_interrupt = algor_p4032_pci_conf_interrupt;
285 	acp->ac_pc.pc_pciide_compat_intr_establish = NULL;
286 
287 	algor_intr_establish = algor_p4032_intr_establish;
288 	algor_intr_disestablish = algor_p4032_intr_disestablish;
289 	algor_iointr = algor_p4032_iointr;
290 }
291 
292 void
293 algor_p4032_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
294 {
295 	u_long ctrdiff[4], startctr, endctr, cps;
296 	u_int32_t irr;
297 	int i;
298 
299 	/* Disable interrupts first. */
300 	bus_space_write_1(st, sh, 0, MC_REGB);
301 	bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
302 	    MC_REGB_24HR);
303 
304 	/* Initialize for 16Hz. */
305 	bus_space_write_1(st, sh, 0, MC_REGA);
306 	bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
307 
308 	REGVAL(P4032_IRR0) = IRR0_RTC;
309 
310 	/* Run the loop an extra time to prime the cache. */
311 	for (cps = 0, i = 0; i < 4; i++) {
312 		led_display('h', 'z', '0' + i, ' ');
313 
314 		/* Enable the interrupt. */
315 		bus_space_write_1(st, sh, 0, MC_REGB);
316 		bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
317 		    MC_REGB_BINARY | MC_REGB_24HR);
318 
319 		/* Wait for it to happen. */
320 		startctr = mips3_cp0_count_read();
321 		do {
322 			irr = REGVAL(P4032_IRR0);
323 			endctr = mips3_cp0_count_read();
324 		} while ((irr & IRR0_RTC) == 0);
325 
326 		/* ACK. */
327 		bus_space_write_1(st, sh, 0, MC_REGC);
328 		(void) bus_space_read_1(st, sh, 1);
329 
330 		/* Disable. */
331 		bus_space_write_1(st, sh, 0, MC_REGB);
332 		bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
333 		    MC_REGB_24HR);
334 
335 		ctrdiff[i] = endctr - startctr;
336 	}
337 
338 	REGVAL(P4032_IRR0) = 0;
339 
340 	/* Compute the number of cycles per second. */
341 	cps = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16;
342 
343 	/* Compute the number of ticks for hz. */
344 	cycles_per_hz = cps / hz;
345 
346 	/* Compute the delay divisor. */
347 	delay_divisor = (cps / 1000000) / 2;
348 
349 	printf("Timer calibration: %lu cycles/sec [(%lu, %lu) * 16]\n",
350 	    cps, ctrdiff[2], ctrdiff[3]);
351 	printf("CPU clock speed = %lu.%02luMHz "
352 	    "(hz cycles = %lu, delay divisor = %u)\n",
353 	    cps / 1000000, (cps % 1000000) / 10000,
354 	    cycles_per_hz, delay_divisor);
355 }
356 
357 void *
358 algor_p4032_intr_establish(int irq, int (*func)(void *), void *arg)
359 {
360 	const struct p4032_irqmap *irqmap;
361 	struct algor_intrhand *ih;
362 	int s;
363 
364 	irqmap = &p4032_irqmap[irq];
365 
366 	KASSERT(irq == irqmap->irqidx);
367 
368 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
369 	if (ih == NULL)
370 		return (NULL);
371 
372 	ih->ih_func = func;
373 	ih->ih_arg = arg;
374 	ih->ih_irq = 0;
375 	ih->ih_irqmap = irqmap;
376 
377 	s = splhigh();
378 
379 	/*
380 	 * First, link it into the tables.
381 	 */
382 	LIST_INSERT_HEAD(&p4032_cpuintrs[irqmap->cpuintr].cintr_list,
383 	    ih, ih_q);
384 
385 	/*
386 	 * Now enable it.
387 	 */
388 	if (p4032_intrtab[irqmap->irqidx].intr_refcnt++ == 0) {
389 		p4032_irqregs[irqmap->irqreg].val |= irqmap->irqbit;
390 		REGVAL(p4032_irqregs[irqmap->irqreg].addr) =
391 		    p4032_irqregs[irqmap->irqreg].val;
392 	}
393 
394 	splx(s);
395 
396 	return (ih);
397 }
398 
399 void
400 algor_p4032_intr_disestablish(void *cookie)
401 {
402 	const struct p4032_irqmap *irqmap;
403 	struct algor_intrhand *ih = cookie;
404 	int s;
405 
406 	irqmap = ih->ih_irqmap;
407 
408 	s = splhigh();
409 
410 	/*
411 	 * First, remove it from the table.
412 	 */
413 	LIST_REMOVE(ih, ih_q);
414 
415 	/*
416 	 * Now, disable it, if there is nothing remaining on the
417 	 * list.
418 	 */
419 	if (p4032_intrtab[irqmap->irqidx].intr_refcnt-- == 1) {
420 		p4032_irqregs[irqmap->irqreg].val &= ~irqmap->irqbit;
421 		REGVAL(p4032_irqregs[irqmap->irqreg].addr) =
422 		    p4032_irqregs[irqmap->irqreg].val;
423 	}
424 
425 	splx(s);
426 
427 	free(ih, M_DEVBUF);
428 }
429 
430 void
431 algor_p4032_iointr(u_int32_t status, u_int32_t cause, u_int32_t pc,
432     u_int32_t ipending)
433 {
434 	const struct p4032_irqmap *irqmap;
435 	struct algor_intrhand *ih;
436 	int level, i;
437 	u_int32_t irr[NIRQREG];
438 
439 	/* Check for ERROR interrupts. */
440 	if (ipending & MIPS_INT_MASK_4) {
441 		irr[IRQREG_ERROR] = REGVAL(p4032_irqregs[IRQREG_ERROR].addr);
442 		if (irr[IRQREG_ERROR] & IRR1_BUSERR)
443 			printf("WARNING: Bus error\n");
444 		if (irr[IRQREG_ERROR] & IRR1_POWERFAIL)
445 			printf("WARNING: Power failure\n");
446 		if (irr[IRQREG_ERROR] & IRR1_DEBUG) {
447 #ifdef DDB
448 			printf("Debug switch -- entering debugger\n");
449 			led_display('D','D','B',' ');
450 			Debugger();
451 			led_display('N','B','S','D');
452 #else
453 			printf("Debug switch ignored -- "
454 			    "no debugger configured\n");
455 #endif
456 		}
457 
458 		/* Clear them. */
459 		REGVAL(p4032_irqregs[IRQREG_ERROR].addr) = irr[IRQREG_ERROR];
460 	}
461 
462 	/* Do floppy DMA request interrupts. */
463 	if (ipending & MIPS_INT_MASK_3) {
464 		/*
465 		 * XXX Hi, um, yah, we need to deal with
466 		 * XXX the floppy interrupt here.
467 		 */
468 
469 		cause &= ~MIPS_INT_MASK_3;
470 		_splset(MIPS_SR_INT_IE |
471 		    ((status & ~cause) & MIPS_HARD_INT_MASK));
472 	}
473 
474 	/*
475 	 * Read the interrupt pending registers, mask them with the
476 	 * ones we have enabled, and service them in order of decreasing
477 	 * priority.
478 	 */
479 	for (i = 0; i < NIRQREG; i++) {
480 		if (i == IRQREG_ERROR)
481 			continue;
482 		irr[i] = REGVAL(p4032_irqregs[i].addr) & p4032_irqregs[i].val;
483 	}
484 
485 	for (level = (NINTRS - 1); level >= 0; level--) {
486 		if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
487 			continue;
488 		p4032_cpuintrs[level].cintr_count.ev_count++;
489 		for (ih = LIST_FIRST(&p4032_cpuintrs[level].cintr_list);
490 		     ih != NULL; ih = LIST_NEXT(ih, ih_q)) {
491 			irqmap = ih->ih_irqmap;
492 			if (irr[irqmap->irqreg] & irqmap->irqbit) {
493 				p4032_intrtab[
494 				    irqmap->irqidx].intr_count.ev_count++;
495 				(*ih->ih_func)(ih->ih_arg);
496 			}
497 		}
498 		cause &= ~(MIPS_INT_MASK_0 << level);
499 	}
500 
501 	/* Re-enable anything that we have processed. */
502 	_splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK));
503 }
504 
505 /*****************************************************************************
506  * PCI interrupt support
507  *****************************************************************************/
508 
509 int
510 algor_p4032_pci_intr_map(struct pci_attach_args *pa,
511     pci_intr_handle_t *ihp)
512 {
513 	static const int pciirqmap[6/*device*/][4/*pin*/] = {
514 		{ 1, -1, -1, -1 },		/* 5: Ethernet */
515 		{ 2, 3, 0, 1 },			/* 6: PCI slot 1 */
516 		{ 3, 0, 1, 2 },			/* 7: PCI slot 2 */
517 		{ 0, -1, -1, -1 },		/* 8: SCSI */
518 		{ -1, -1, -1, -1 },		/* 9: not used */
519 		{ 0, 1, 2, 3 },			/* 10: custom connector */
520 	};
521 	pcitag_t bustag = pa->pa_intrtag;
522 	int buspin = pa->pa_intrpin;
523 	pci_chipset_tag_t pc = pa->pa_pc;
524 	int device, irq;
525 
526 	if (buspin == 0) {
527 		/* No IRQ used. */
528 		return (1);
529 	}
530 
531 	if (buspin > 4) {
532 		printf("algor_p4032_pci_intr_map: bad interrupt pin %d\n",
533 		    buspin);
534 		return (1);
535 	}
536 
537 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
538 	if (device < 5 || device > 10) {
539 		printf("algor_p4032_pci_intr_map: bad device %d\n",
540 		    device);
541 		return (1);
542 	}
543 
544 	irq = pciirqmap[device - 5][buspin - 1];
545 	if (irq == -1) {
546 		printf("algor_p4032_pci_intr_map: no mapping for "
547 		    "device %d pin %d\n", device, buspin);
548 		return (1);
549 	}
550 
551 	*ihp = irq;
552 	return (0);
553 }
554 
555 const char *
556 algor_p4032_pci_intr_string(void *v, pci_intr_handle_t ih)
557 {
558 
559 	if (ih >= NPCIIRQS)
560 		panic("algor_p4032_intr_string: bogus IRQ %ld", ih);
561 
562 	return (p4032_intrnames[ih]);
563 }
564 
565 const struct evcnt *
566 algor_p4032_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
567 {
568 
569 	return (&p4032_intrtab[ih].intr_count);
570 }
571 
572 void *
573 algor_p4032_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
574     int (*func)(void *), void *arg)
575 {
576 
577 	if (ih >= NPCIIRQS)
578 		panic("algor_p4032_intr_establish: bogus IRQ %ld", ih);
579 
580 	return (algor_p4032_intr_establish(ih, func, arg));
581 }
582 
583 void
584 algor_p4032_pci_intr_disestablish(void *v, void *cookie)
585 {
586 
587 	return (algor_p4032_intr_disestablish(cookie));
588 }
589 
590 void
591 algor_p4032_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
592     int *iline)
593 {
594 
595 	/*
596 	 * We actually don't need to do anything; everything is handled
597 	 * in pci_intr_map().
598 	 */
599 	*iline = 0;
600 }
601