1*ae873cf0Sthorpej /* $NetBSD: algor_p4032_intr.c,v 1.27 2020/11/14 02:23:04 thorpej Exp $ */
271cb790fSthorpej
371cb790fSthorpej /*-
471cb790fSthorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
571cb790fSthorpej * All rights reserved.
671cb790fSthorpej *
771cb790fSthorpej * This code is derived from software contributed to The NetBSD Foundation
871cb790fSthorpej * by Jason R. Thorpe.
971cb790fSthorpej *
1071cb790fSthorpej * Redistribution and use in source and binary forms, with or without
1171cb790fSthorpej * modification, are permitted provided that the following conditions
1271cb790fSthorpej * are met:
1371cb790fSthorpej * 1. Redistributions of source code must retain the above copyright
1471cb790fSthorpej * notice, this list of conditions and the following disclaimer.
1571cb790fSthorpej * 2. Redistributions in binary form must reproduce the above copyright
1671cb790fSthorpej * notice, this list of conditions and the following disclaimer in the
1771cb790fSthorpej * documentation and/or other materials provided with the distribution.
1871cb790fSthorpej *
1971cb790fSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2071cb790fSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2171cb790fSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2271cb790fSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2371cb790fSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2471cb790fSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2571cb790fSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2671cb790fSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2771cb790fSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2871cb790fSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2971cb790fSthorpej * POSSIBILITY OF SUCH DAMAGE.
3071cb790fSthorpej */
3171cb790fSthorpej
3271cb790fSthorpej /*
3371cb790fSthorpej * Platform-specific interrupt support for the Algorithmics P-4032.
3471cb790fSthorpej *
3571cb790fSthorpej * The Algorithmics P-4032 has an interrupt controller that is pretty
3671cb790fSthorpej * flexible -- it can take an interrupt source and route it to an
3771cb790fSthorpej * arbitrary MIPS CPU hardware interrupt pin.
3871cb790fSthorpej */
3971cb790fSthorpej
406fdfc66cSlukem #include <sys/cdefs.h>
41*ae873cf0Sthorpej __KERNEL_RCSID(0, "$NetBSD: algor_p4032_intr.c,v 1.27 2020/11/14 02:23:04 thorpej Exp $");
426fdfc66cSlukem
4371cb790fSthorpej #include "opt_ddb.h"
44ea810943Smatt #define __INTR_PRIVATE
4571cb790fSthorpej
4671cb790fSthorpej #include <sys/param.h>
47391925c7Sdyoung #include <sys/bus.h>
48f5439ed7Smatt #include <sys/cpu.h>
49f5439ed7Smatt #include <sys/device.h>
50f5439ed7Smatt #include <sys/intr.h>
51f5439ed7Smatt #include <sys/kernel.h>
52*ae873cf0Sthorpej #include <sys/kmem.h>
53f5439ed7Smatt #include <sys/queue.h>
54f5439ed7Smatt #include <sys/systm.h>
55f5439ed7Smatt
56f5439ed7Smatt #include <algor/autoconf.h>
5771cb790fSthorpej
58ce66bf08Sthorpej #include <mips/locore.h>
59ce66bf08Sthorpej
60ce66bf08Sthorpej #include <dev/ic/mc146818reg.h>
61ce66bf08Sthorpej
6271cb790fSthorpej #include <algor/algor/algor_p4032reg.h>
6371cb790fSthorpej #include <algor/algor/algor_p4032var.h>
6471cb790fSthorpej
6571cb790fSthorpej #include <dev/pci/pcireg.h>
6671cb790fSthorpej #include <dev/pci/pcivar.h>
6771cb790fSthorpej
685f1c88d7Sperry #define REGVAL(x) *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
6971cb790fSthorpej
70ce66bf08Sthorpej struct p4032_irqreg {
71ce66bf08Sthorpej bus_addr_t addr;
72ce66bf08Sthorpej u_int32_t val;
73ce66bf08Sthorpej };
74ce66bf08Sthorpej
7571cb790fSthorpej #define IRQREG_8BIT 0
7671cb790fSthorpej #define IRQREG_ERROR 1
7771cb790fSthorpej #define IRQREG_PCI 2
7871cb790fSthorpej #define NIRQREG 3
7971cb790fSthorpej
80ce66bf08Sthorpej struct p4032_irqreg p4032_irqregs[NIRQREG] = {
8171cb790fSthorpej { P4032_IRR0, 0 },
8271cb790fSthorpej { P4032_IRR1, 0 },
8371cb790fSthorpej { P4032_IRR2, 0 },
8471cb790fSthorpej };
8571cb790fSthorpej
8671cb790fSthorpej #define NSTEERREG 3
8771cb790fSthorpej
88ce66bf08Sthorpej struct p4032_irqreg p4032_irqsteer[NSTEERREG] = {
8971cb790fSthorpej { P4032_XBAR0, 0 },
9071cb790fSthorpej { P4032_XBAR1, 0 },
9171cb790fSthorpej { P4032_XBAR2, 0 },
9271cb790fSthorpej };
9371cb790fSthorpej
94ce66bf08Sthorpej #define NPCIIRQS 4
9571cb790fSthorpej
96ce66bf08Sthorpej /* See algor_p4032var.h */
97ce66bf08Sthorpej #define N8BITIRQS 8
9871cb790fSthorpej
99ce66bf08Sthorpej #define IRQMAP_PCIBASE 0
100ce66bf08Sthorpej #define IRQMAP_8BITBASE NPCIIRQS
101ce66bf08Sthorpej #define NIRQMAPS (IRQMAP_8BITBASE + N8BITIRQS)
10271cb790fSthorpej
103ea810943Smatt const char * const p4032_intrnames[NIRQMAPS] = {
104ce66bf08Sthorpej /*
105ce66bf08Sthorpej * PCI INTERRUPTS
106ce66bf08Sthorpej */
10771cb790fSthorpej "PCIIRQ 0",
10871cb790fSthorpej "PCIIRQ 1",
10971cb790fSthorpej "PCIIRQ 2",
11071cb790fSthorpej "PCIIRQ 3",
11171cb790fSthorpej
112ce66bf08Sthorpej /*
113ce66bf08Sthorpej * 8-BIT DEVICE INTERRUPTS
114ce66bf08Sthorpej */
11571cb790fSthorpej "PCI ctlr",
11671cb790fSthorpej "floppy",
11771cb790fSthorpej "pckbc",
11871cb790fSthorpej "com 1",
11971cb790fSthorpej "com 2",
12071cb790fSthorpej "centronics",
12171cb790fSthorpej "gpio",
122ce66bf08Sthorpej "mcclock",
12371cb790fSthorpej };
12471cb790fSthorpej
125e51a0439Sthorpej struct p4032_irqmap {
126e51a0439Sthorpej int irqidx;
127e51a0439Sthorpej int cpuintr;
128e51a0439Sthorpej int irqreg;
129e51a0439Sthorpej int irqbit;
130e51a0439Sthorpej int xbarreg;
131e51a0439Sthorpej int xbarshift;
132e51a0439Sthorpej };
133e51a0439Sthorpej
134ce66bf08Sthorpej const struct p4032_irqmap p4032_irqmap[NIRQMAPS] = {
135ce66bf08Sthorpej /*
136ce66bf08Sthorpej * PCI INTERRUPTS
137ce66bf08Sthorpej */
138ce66bf08Sthorpej /* PCIIRQ 0 */
139ce66bf08Sthorpej { 0, 0,
140ce66bf08Sthorpej IRQREG_PCI, IRR2_PCIIRQ0,
141ce66bf08Sthorpej 2, 0 },
14271cb790fSthorpej
143ce66bf08Sthorpej /* PCIIRQ 1 */
144ce66bf08Sthorpej { 1, 0,
145ce66bf08Sthorpej IRQREG_PCI, IRR2_PCIIRQ1,
146ce66bf08Sthorpej 2, 2 },
147ce66bf08Sthorpej
148ce66bf08Sthorpej /* PCIIRQ 2 */
149ce66bf08Sthorpej { 2, 0,
150ce66bf08Sthorpej IRQREG_PCI, IRR2_PCIIRQ2,
151ce66bf08Sthorpej 2, 4 },
152ce66bf08Sthorpej
153ce66bf08Sthorpej /* PCIIRQ 3 */
154ce66bf08Sthorpej { 3, 0,
155ce66bf08Sthorpej IRQREG_PCI, IRR2_PCIIRQ3,
156ce66bf08Sthorpej 2, 6 },
157ce66bf08Sthorpej
158ce66bf08Sthorpej /*
159ce66bf08Sthorpej * 8-BIT DEVICE INTERRUPTS
160ce66bf08Sthorpej */
161e51a0439Sthorpej { P4032_IRQ_PCICTLR, 1,
16271cb790fSthorpej IRQREG_8BIT, IRR0_PCICTLR,
16371cb790fSthorpej 0, 0 },
16471cb790fSthorpej
165e51a0439Sthorpej { P4032_IRQ_FLOPPY, 1,
16671cb790fSthorpej IRQREG_8BIT, IRR0_FLOPPY,
16771cb790fSthorpej 0, 2 },
16871cb790fSthorpej
169e51a0439Sthorpej { P4032_IRQ_PCKBC, 1,
17071cb790fSthorpej IRQREG_8BIT, IRR0_PCKBC,
17171cb790fSthorpej 0, 4 },
17271cb790fSthorpej
173e51a0439Sthorpej { P4032_IRQ_COM1, 1,
17471cb790fSthorpej IRQREG_8BIT, IRR0_COM1,
17571cb790fSthorpej 0, 6 },
17671cb790fSthorpej
177e51a0439Sthorpej { P4032_IRQ_COM2, 1,
17871cb790fSthorpej IRQREG_8BIT, IRR0_COM2,
17971cb790fSthorpej 1, 0 },
18071cb790fSthorpej
181e51a0439Sthorpej { P4032_IRQ_LPT, 1,
18271cb790fSthorpej IRQREG_8BIT, IRR0_LPT,
18371cb790fSthorpej 1, 2 },
18471cb790fSthorpej
185e51a0439Sthorpej { P4032_IRQ_GPIO, 1,
18671cb790fSthorpej IRQREG_8BIT, IRR0_GPIO,
18771cb790fSthorpej 1, 4 },
18871cb790fSthorpej
189e51a0439Sthorpej { P4032_IRQ_RTC, 1,
19071cb790fSthorpej IRQREG_8BIT, IRR0_RTC,
19171cb790fSthorpej 1, 6 },
19271cb790fSthorpej };
19371cb790fSthorpej
194ce66bf08Sthorpej struct p4032_intrhead {
195ce66bf08Sthorpej struct evcnt intr_count;
196ce66bf08Sthorpej int intr_refcnt;
197ce66bf08Sthorpej };
198ce66bf08Sthorpej struct p4032_intrhead p4032_intrtab[NIRQMAPS];
199ce66bf08Sthorpej
200ce66bf08Sthorpej #define NINTRS 2 /* MIPS INT0 - INT1 */
201ce66bf08Sthorpej
202ed66be25Ssimonb
203ce66bf08Sthorpej struct p4032_cpuintr {
204f5439ed7Smatt LIST_HEAD(, evbmips_intrhand) cintr_list;
205ce66bf08Sthorpej struct evcnt cintr_count;
206ce66bf08Sthorpej };
207ce66bf08Sthorpej
208ce66bf08Sthorpej struct p4032_cpuintr p4032_cpuintrs[NINTRS];
209ea810943Smatt const char * const p4032_cpuintrnames[NINTRS] = {
210ce66bf08Sthorpej "int 0 (pci)",
211ce66bf08Sthorpej "int 1 (8-bit)",
212ce66bf08Sthorpej };
213ce66bf08Sthorpej
214ea810943Smatt const char * const p4032_intrgroups[NINTRS] = {
215ce66bf08Sthorpej "pci",
216ce66bf08Sthorpej "8-bit",
217ce66bf08Sthorpej };
218ce66bf08Sthorpej
219e51a0439Sthorpej void *algor_p4032_intr_establish(int, int (*)(void *), void *);
220e51a0439Sthorpej void algor_p4032_intr_disestablish(void *);
221ce66bf08Sthorpej
2223f56488cSdyoung int algor_p4032_pci_intr_map(const struct pci_attach_args *,
2233f56488cSdyoung pci_intr_handle_t *);
224e58a356cSchristos const char *algor_p4032_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
22571cb790fSthorpej const struct evcnt *algor_p4032_pci_intr_evcnt(void *, pci_intr_handle_t);
22671cb790fSthorpej void *algor_p4032_pci_intr_establish(void *, pci_intr_handle_t, int,
22771cb790fSthorpej int (*)(void *), void *);
228e51a0439Sthorpej void algor_p4032_pci_intr_disestablish(void *, void *);
22971cb790fSthorpej void algor_p4032_pci_conf_interrupt(void *, int, int, int, int, int *);
23071cb790fSthorpej
231ea810943Smatt void algor_p4032_iointr(int, vaddr_t, uint32_t);
23271cb790fSthorpej
23371cb790fSthorpej void
algor_p4032_intr_init(struct p4032_config * acp)23471cb790fSthorpej algor_p4032_intr_init(struct p4032_config *acp)
23571cb790fSthorpej {
236ce66bf08Sthorpej const struct p4032_irqmap *irqmap;
23771cb790fSthorpej int i;
23871cb790fSthorpej
23971cb790fSthorpej for (i = 0; i < NIRQREG; i++)
24071cb790fSthorpej REGVAL(p4032_irqregs[i].addr) = p4032_irqregs[i].val;
24171cb790fSthorpej
24271cb790fSthorpej for (i = 0; i < NINTRS; i++) {
243ce66bf08Sthorpej LIST_INIT(&p4032_cpuintrs[i].cintr_list);
244ce66bf08Sthorpej evcnt_attach_dynamic(&p4032_cpuintrs[i].cintr_count,
245ce66bf08Sthorpej EVCNT_TYPE_INTR, NULL, "mips", p4032_cpuintrnames[i]);
246ce66bf08Sthorpej }
247ce66bf08Sthorpej
248ce66bf08Sthorpej for (i = 0; i < NIRQMAPS; i++) {
249ce66bf08Sthorpej irqmap = &p4032_irqmap[i];
250ce66bf08Sthorpej
251ce66bf08Sthorpej p4032_irqsteer[irqmap->xbarreg].val |=
252ce66bf08Sthorpej irqmap->cpuintr << irqmap->xbarshift;
253ce66bf08Sthorpej
25471cb790fSthorpej evcnt_attach_dynamic(&p4032_intrtab[i].intr_count,
255ce66bf08Sthorpej EVCNT_TYPE_INTR, NULL, p4032_intrgroups[irqmap->cpuintr],
256ce66bf08Sthorpej p4032_intrnames[i]);
25771cb790fSthorpej }
25871cb790fSthorpej
259ce66bf08Sthorpej for (i = 0; i < NSTEERREG; i++)
260ce66bf08Sthorpej REGVAL(p4032_irqsteer[i].addr) = p4032_irqsteer[i].val;
26171cb790fSthorpej
26271cb790fSthorpej acp->ac_pc.pc_intr_v = NULL;
26371cb790fSthorpej acp->ac_pc.pc_intr_map = algor_p4032_pci_intr_map;
26471cb790fSthorpej acp->ac_pc.pc_intr_string = algor_p4032_pci_intr_string;
26571cb790fSthorpej acp->ac_pc.pc_intr_evcnt = algor_p4032_pci_intr_evcnt;
26671cb790fSthorpej acp->ac_pc.pc_intr_establish = algor_p4032_pci_intr_establish;
267e51a0439Sthorpej acp->ac_pc.pc_intr_disestablish = algor_p4032_pci_intr_disestablish;
26871cb790fSthorpej acp->ac_pc.pc_conf_interrupt = algor_p4032_pci_conf_interrupt;
26971cb790fSthorpej acp->ac_pc.pc_pciide_compat_intr_establish = NULL;
27071cb790fSthorpej
271e51a0439Sthorpej algor_intr_establish = algor_p4032_intr_establish;
272e51a0439Sthorpej algor_intr_disestablish = algor_p4032_intr_disestablish;
27371cb790fSthorpej algor_iointr = algor_p4032_iointr;
274ce66bf08Sthorpej }
275ce66bf08Sthorpej
276ce66bf08Sthorpej void
algor_p4032_cal_timer(bus_space_tag_t st,bus_space_handle_t sh)277ce66bf08Sthorpej algor_p4032_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
278ce66bf08Sthorpej {
279ce66bf08Sthorpej u_long ctrdiff[4], startctr, endctr, cps;
280ce66bf08Sthorpej u_int32_t irr;
281ce66bf08Sthorpej int i;
282ce66bf08Sthorpej
283ce66bf08Sthorpej /* Disable interrupts first. */
284ce66bf08Sthorpej bus_space_write_1(st, sh, 0, MC_REGB);
285ce66bf08Sthorpej bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
286ce66bf08Sthorpej MC_REGB_24HR);
287ce66bf08Sthorpej
288ce66bf08Sthorpej /* Initialize for 16Hz. */
289ce66bf08Sthorpej bus_space_write_1(st, sh, 0, MC_REGA);
290ce66bf08Sthorpej bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
291ce66bf08Sthorpej
292ce66bf08Sthorpej REGVAL(P4032_IRR0) = IRR0_RTC;
293ce66bf08Sthorpej
294ce66bf08Sthorpej /* Run the loop an extra time to prime the cache. */
295a775e1faStsutsui for (i = 0; i < 4; i++) {
296ce66bf08Sthorpej led_display('h', 'z', '0' + i, ' ');
297ce66bf08Sthorpej
298ce66bf08Sthorpej /* Enable the interrupt. */
299ce66bf08Sthorpej bus_space_write_1(st, sh, 0, MC_REGB);
300ce66bf08Sthorpej bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
301ce66bf08Sthorpej MC_REGB_BINARY | MC_REGB_24HR);
302ce66bf08Sthorpej
303ce66bf08Sthorpej /* Wait for it to happen. */
304ce66bf08Sthorpej startctr = mips3_cp0_count_read();
305ce66bf08Sthorpej do {
306ce66bf08Sthorpej irr = REGVAL(P4032_IRR0);
307ce66bf08Sthorpej endctr = mips3_cp0_count_read();
308ce66bf08Sthorpej } while ((irr & IRR0_RTC) == 0);
309ce66bf08Sthorpej
310ce66bf08Sthorpej /* ACK. */
311ce66bf08Sthorpej bus_space_write_1(st, sh, 0, MC_REGC);
312ce66bf08Sthorpej (void) bus_space_read_1(st, sh, 1);
313ce66bf08Sthorpej
314ce66bf08Sthorpej /* Disable. */
315ce66bf08Sthorpej bus_space_write_1(st, sh, 0, MC_REGB);
316ce66bf08Sthorpej bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
317ce66bf08Sthorpej MC_REGB_24HR);
318ce66bf08Sthorpej
319ce66bf08Sthorpej ctrdiff[i] = endctr - startctr;
320ce66bf08Sthorpej }
321ce66bf08Sthorpej
322ce66bf08Sthorpej REGVAL(P4032_IRR0) = 0;
323ce66bf08Sthorpej
324774fc1f9Stsutsui /* Update CPU frequency values */
325ce66bf08Sthorpej cps = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16;
326c86aec7cStsutsui /* XXX mips_cpu_flags isn't set here; assume CPU_MIPS_DOUBLE_COUNT */
327c86aec7cStsutsui curcpu()->ci_cpu_freq = cps * 2;
328774fc1f9Stsutsui curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
329774fc1f9Stsutsui curcpu()->ci_divisor_delay =
330774fc1f9Stsutsui ((curcpu()->ci_cpu_freq + (1000000 / 2)) / 1000000);
331c86aec7cStsutsui /* XXX assume CPU_MIPS_DOUBLE_COUNT */
332774fc1f9Stsutsui curcpu()->ci_cycles_per_hz /= 2;
333774fc1f9Stsutsui curcpu()->ci_divisor_delay /= 2;
334ce66bf08Sthorpej
335ce66bf08Sthorpej printf("Timer calibration: %lu cycles/sec [(%lu, %lu) * 16]\n",
336c86aec7cStsutsui cps, ctrdiff[2], ctrdiff[3]);
337ce66bf08Sthorpej printf("CPU clock speed = %lu.%02luMHz "
338774fc1f9Stsutsui "(hz cycles = %lu, delay divisor = %lu)\n",
339774fc1f9Stsutsui curcpu()->ci_cpu_freq / 1000000,
340774fc1f9Stsutsui (curcpu()->ci_cpu_freq % 1000000) / 10000,
341774fc1f9Stsutsui curcpu()->ci_cycles_per_hz, curcpu()->ci_divisor_delay);
34271cb790fSthorpej }
34371cb790fSthorpej
34471cb790fSthorpej void *
algor_p4032_intr_establish(int irq,int (* func)(void *),void * arg)345e51a0439Sthorpej algor_p4032_intr_establish(int irq, int (*func)(void *), void *arg)
34671cb790fSthorpej {
347e51a0439Sthorpej const struct p4032_irqmap *irqmap;
348f5439ed7Smatt struct evbmips_intrhand *ih;
34971cb790fSthorpej int s;
35071cb790fSthorpej
351e51a0439Sthorpej irqmap = &p4032_irqmap[irq];
352e51a0439Sthorpej
353e51a0439Sthorpej KASSERT(irq == irqmap->irqidx);
354e51a0439Sthorpej
355*ae873cf0Sthorpej ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
35671cb790fSthorpej ih->ih_func = func;
35771cb790fSthorpej ih->ih_arg = arg;
3587c074dc8Sthorpej ih->ih_irq = 0;
35971cb790fSthorpej ih->ih_irqmap = irqmap;
36071cb790fSthorpej
36171cb790fSthorpej s = splhigh();
36271cb790fSthorpej
36371cb790fSthorpej /*
364ce66bf08Sthorpej * First, link it into the tables.
36571cb790fSthorpej */
366ce66bf08Sthorpej LIST_INSERT_HEAD(&p4032_cpuintrs[irqmap->cpuintr].cintr_list,
367ce66bf08Sthorpej ih, ih_q);
36871cb790fSthorpej
36971cb790fSthorpej /*
37071cb790fSthorpej * Now enable it.
37171cb790fSthorpej */
372ce66bf08Sthorpej if (p4032_intrtab[irqmap->irqidx].intr_refcnt++ == 0) {
37371cb790fSthorpej p4032_irqregs[irqmap->irqreg].val |= irqmap->irqbit;
37471cb790fSthorpej REGVAL(p4032_irqregs[irqmap->irqreg].addr) =
37571cb790fSthorpej p4032_irqregs[irqmap->irqreg].val;
376ce66bf08Sthorpej }
37771cb790fSthorpej
37871cb790fSthorpej splx(s);
37971cb790fSthorpej
38071cb790fSthorpej return (ih);
38171cb790fSthorpej }
38271cb790fSthorpej
38371cb790fSthorpej void
algor_p4032_intr_disestablish(void * cookie)384e51a0439Sthorpej algor_p4032_intr_disestablish(void *cookie)
38571cb790fSthorpej {
386ce66bf08Sthorpej const struct p4032_irqmap *irqmap;
387f5439ed7Smatt struct evbmips_intrhand *ih = cookie;
388ce66bf08Sthorpej int s;
38971cb790fSthorpej
390e51a0439Sthorpej irqmap = ih->ih_irqmap;
391e51a0439Sthorpej
392ce66bf08Sthorpej s = splhigh();
393ce66bf08Sthorpej
394ce66bf08Sthorpej /*
395ce66bf08Sthorpej * First, remove it from the table.
396ce66bf08Sthorpej */
397ce66bf08Sthorpej LIST_REMOVE(ih, ih_q);
398ce66bf08Sthorpej
399ce66bf08Sthorpej /*
400ce66bf08Sthorpej * Now, disable it, if there is nothing remaining on the
401ce66bf08Sthorpej * list.
402ce66bf08Sthorpej */
403ce66bf08Sthorpej if (p4032_intrtab[irqmap->irqidx].intr_refcnt-- == 1) {
404ce66bf08Sthorpej p4032_irqregs[irqmap->irqreg].val &= ~irqmap->irqbit;
405ce66bf08Sthorpej REGVAL(p4032_irqregs[irqmap->irqreg].addr) =
406ce66bf08Sthorpej p4032_irqregs[irqmap->irqreg].val;
40771cb790fSthorpej }
40871cb790fSthorpej
409ce66bf08Sthorpej splx(s);
41071cb790fSthorpej
411*ae873cf0Sthorpej kmem_free(ih, sizeof(*ih));
41271cb790fSthorpej }
41371cb790fSthorpej
41471cb790fSthorpej void
algor_p4032_iointr(int ipl,vaddr_t pc,u_int32_t ipending)415ea810943Smatt algor_p4032_iointr(int ipl, vaddr_t pc, u_int32_t ipending)
41671cb790fSthorpej {
4177c074dc8Sthorpej const struct p4032_irqmap *irqmap;
418f5439ed7Smatt struct evbmips_intrhand *ih;
41971cb790fSthorpej int level, i;
42071cb790fSthorpej u_int32_t irr[NIRQREG];
42171cb790fSthorpej
42271cb790fSthorpej /* Check for ERROR interrupts. */
42371cb790fSthorpej if (ipending & MIPS_INT_MASK_4) {
42471cb790fSthorpej irr[IRQREG_ERROR] = REGVAL(p4032_irqregs[IRQREG_ERROR].addr);
42571cb790fSthorpej if (irr[IRQREG_ERROR] & IRR1_BUSERR)
42671cb790fSthorpej printf("WARNING: Bus error\n");
42771cb790fSthorpej if (irr[IRQREG_ERROR] & IRR1_POWERFAIL)
42871cb790fSthorpej printf("WARNING: Power failure\n");
42971cb790fSthorpej if (irr[IRQREG_ERROR] & IRR1_DEBUG) {
43071cb790fSthorpej #ifdef DDB
43171cb790fSthorpej printf("Debug switch -- entering debugger\n");
43271cb790fSthorpej led_display('D','D','B',' ');
43371cb790fSthorpej Debugger();
43471cb790fSthorpej led_display('N','B','S','D');
43571cb790fSthorpej #else
43671cb790fSthorpej printf("Debug switch ignored -- "
43771cb790fSthorpej "no debugger configured\n");
43871cb790fSthorpej #endif
43971cb790fSthorpej }
44071cb790fSthorpej
44171cb790fSthorpej /* Clear them. */
44271cb790fSthorpej REGVAL(p4032_irqregs[IRQREG_ERROR].addr) = irr[IRQREG_ERROR];
44371cb790fSthorpej }
44471cb790fSthorpej
445ce66bf08Sthorpej /* Do floppy DMA request interrupts. */
44671cb790fSthorpej if (ipending & MIPS_INT_MASK_3) {
44771cb790fSthorpej /*
44871cb790fSthorpej * XXX Hi, um, yah, we need to deal with
449ce66bf08Sthorpej * XXX the floppy interrupt here.
45071cb790fSthorpej */
45171cb790fSthorpej
45271cb790fSthorpej }
45371cb790fSthorpej
45471cb790fSthorpej /*
45571cb790fSthorpej * Read the interrupt pending registers, mask them with the
45671cb790fSthorpej * ones we have enabled, and service them in order of decreasing
45771cb790fSthorpej * priority.
45871cb790fSthorpej */
45971cb790fSthorpej for (i = 0; i < NIRQREG; i++) {
46071cb790fSthorpej if (i == IRQREG_ERROR)
46171cb790fSthorpej continue;
46271cb790fSthorpej irr[i] = REGVAL(p4032_irqregs[i].addr) & p4032_irqregs[i].val;
46371cb790fSthorpej }
46471cb790fSthorpej
465ce66bf08Sthorpej for (level = (NINTRS - 1); level >= 0; level--) {
46671cb790fSthorpej if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
46771cb790fSthorpej continue;
468ce66bf08Sthorpej p4032_cpuintrs[level].cintr_count.ev_count++;
469ce66bf08Sthorpej for (ih = LIST_FIRST(&p4032_cpuintrs[level].cintr_list);
47071cb790fSthorpej ih != NULL; ih = LIST_NEXT(ih, ih_q)) {
4717c074dc8Sthorpej irqmap = ih->ih_irqmap;
4727c074dc8Sthorpej if (irr[irqmap->irqreg] & irqmap->irqbit) {
473ce66bf08Sthorpej p4032_intrtab[
4747c074dc8Sthorpej irqmap->irqidx].intr_count.ev_count++;
47571cb790fSthorpej (*ih->ih_func)(ih->ih_arg);
47671cb790fSthorpej }
47771cb790fSthorpej }
47871cb790fSthorpej }
47971cb790fSthorpej }
48071cb790fSthorpej
48171cb790fSthorpej /*****************************************************************************
48271cb790fSthorpej * PCI interrupt support
48371cb790fSthorpej *****************************************************************************/
48471cb790fSthorpej
48571cb790fSthorpej int
algor_p4032_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)4863f56488cSdyoung algor_p4032_pci_intr_map(const struct pci_attach_args *pa,
48771cb790fSthorpej pci_intr_handle_t *ihp)
48871cb790fSthorpej {
48971cb790fSthorpej static const int pciirqmap[6/*device*/][4/*pin*/] = {
49071cb790fSthorpej { 1, -1, -1, -1 }, /* 5: Ethernet */
49171cb790fSthorpej { 2, 3, 0, 1 }, /* 6: PCI slot 1 */
49271cb790fSthorpej { 3, 0, 1, 2 }, /* 7: PCI slot 2 */
49371cb790fSthorpej { 0, -1, -1, -1 }, /* 8: SCSI */
49471cb790fSthorpej { -1, -1, -1, -1 }, /* 9: not used */
49571cb790fSthorpej { 0, 1, 2, 3 }, /* 10: custom connector */
49671cb790fSthorpej };
49771cb790fSthorpej pcitag_t bustag = pa->pa_intrtag;
49871cb790fSthorpej int buspin = pa->pa_intrpin;
49971cb790fSthorpej pci_chipset_tag_t pc = pa->pa_pc;
50071cb790fSthorpej int device, irq;
50171cb790fSthorpej
50271cb790fSthorpej if (buspin == 0) {
50371cb790fSthorpej /* No IRQ used. */
50471cb790fSthorpej return (1);
50571cb790fSthorpej }
50671cb790fSthorpej
50771cb790fSthorpej if (buspin > 4) {
50871cb790fSthorpej printf("algor_p4032_pci_intr_map: bad interrupt pin %d\n",
50971cb790fSthorpej buspin);
51071cb790fSthorpej return (1);
51171cb790fSthorpej }
51271cb790fSthorpej
51371cb790fSthorpej pci_decompose_tag(pc, bustag, NULL, &device, NULL);
51471cb790fSthorpej if (device < 5 || device > 10) {
51571cb790fSthorpej printf("algor_p4032_pci_intr_map: bad device %d\n",
51671cb790fSthorpej device);
51771cb790fSthorpej return (1);
51871cb790fSthorpej }
51971cb790fSthorpej
52071cb790fSthorpej irq = pciirqmap[device - 5][buspin - 1];
52171cb790fSthorpej if (irq == -1) {
52271cb790fSthorpej printf("algor_p4032_pci_intr_map: no mapping for "
52371cb790fSthorpej "device %d pin %d\n", device, buspin);
52471cb790fSthorpej return (1);
52571cb790fSthorpej }
52671cb790fSthorpej
52771cb790fSthorpej *ihp = irq;
52871cb790fSthorpej return (0);
52971cb790fSthorpej }
53071cb790fSthorpej
53171cb790fSthorpej const char *
algor_p4032_pci_intr_string(void * v,pci_intr_handle_t ih,char * buf,size_t len)532e58a356cSchristos algor_p4032_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
53371cb790fSthorpej {
53471cb790fSthorpej
535ce66bf08Sthorpej if (ih >= NPCIIRQS)
5360f09ed48Sprovos panic("algor_p4032_intr_string: bogus IRQ %ld", ih);
53771cb790fSthorpej
538e58a356cSchristos strlcpy(buf, p4032_intrnames[ih], len);
539e58a356cSchristos return buf;
54071cb790fSthorpej }
54171cb790fSthorpej
54271cb790fSthorpej const struct evcnt *
algor_p4032_pci_intr_evcnt(void * v,pci_intr_handle_t ih)54371cb790fSthorpej algor_p4032_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
54471cb790fSthorpej {
54571cb790fSthorpej
546ce66bf08Sthorpej return (&p4032_intrtab[ih].intr_count);
54771cb790fSthorpej }
54871cb790fSthorpej
54971cb790fSthorpej void *
algor_p4032_pci_intr_establish(void * v,pci_intr_handle_t ih,int level,int (* func)(void *),void * arg)55071cb790fSthorpej algor_p4032_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
55171cb790fSthorpej int (*func)(void *), void *arg)
55271cb790fSthorpej {
55371cb790fSthorpej
554ce66bf08Sthorpej if (ih >= NPCIIRQS)
5550f09ed48Sprovos panic("algor_p4032_intr_establish: bogus IRQ %ld", ih);
55671cb790fSthorpej
557e51a0439Sthorpej return (algor_p4032_intr_establish(ih, func, arg));
558e51a0439Sthorpej }
559e51a0439Sthorpej
560e51a0439Sthorpej void
algor_p4032_pci_intr_disestablish(void * v,void * cookie)561e51a0439Sthorpej algor_p4032_pci_intr_disestablish(void *v, void *cookie)
562e51a0439Sthorpej {
563e51a0439Sthorpej
564e51a0439Sthorpej return (algor_p4032_intr_disestablish(cookie));
56571cb790fSthorpej }
56671cb790fSthorpej
56771cb790fSthorpej void
algor_p4032_pci_conf_interrupt(void * v,int bus,int dev,int pin,int swiz,int * iline)5686fd8d278Sthorpej algor_p4032_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
56971cb790fSthorpej int *iline)
57071cb790fSthorpej {
57171cb790fSthorpej
57271cb790fSthorpej /*
57371cb790fSthorpej * We actually don't need to do anything; everything is handled
57471cb790fSthorpej * in pci_intr_map().
57571cb790fSthorpej */
57671cb790fSthorpej *iline = 0;
57771cb790fSthorpej }
578