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