pci_kn300.c revision 1.2 1 1.2 mjacob /* $NetBSD: pci_kn300.c,v 1.2 1998/04/15 01:18:17 mjacob Exp $ */
2 1.1 mjacob
3 1.1 mjacob /*
4 1.1 mjacob * Copyright (c) 1998 by Matthew Jacob
5 1.1 mjacob * NASA AMES Research Center.
6 1.1 mjacob * All rights reserved.
7 1.1 mjacob *
8 1.1 mjacob * Redistribution and use in source and binary forms, with or without
9 1.1 mjacob * modification, are permitted provided that the following conditions
10 1.1 mjacob * are met:
11 1.1 mjacob * 1. Redistributions of source code must retain the above copyright
12 1.1 mjacob * notice immediately at the beginning of the file, without modification,
13 1.1 mjacob * this list of conditions, and the following disclaimer.
14 1.1 mjacob * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 mjacob * notice, this list of conditions and the following disclaimer in the
16 1.1 mjacob * documentation and/or other materials provided with the distribution.
17 1.1 mjacob * 3. The name of the author may not be used to endorse or promote products
18 1.1 mjacob * derived from this software without specific prior written permission.
19 1.1 mjacob *
20 1.1 mjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1 mjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 mjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 mjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 1.1 mjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 mjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 mjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 mjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 mjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 mjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 mjacob * SUCH DAMAGE.
31 1.1 mjacob */
32 1.1 mjacob
33 1.1 mjacob #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 1.1 mjacob
35 1.2 mjacob __KERNEL_RCSID(0, "$NetBSD: pci_kn300.c,v 1.2 1998/04/15 01:18:17 mjacob Exp $");
36 1.1 mjacob
37 1.1 mjacob #include <sys/types.h>
38 1.1 mjacob #include <sys/param.h>
39 1.1 mjacob #include <sys/time.h>
40 1.1 mjacob #include <sys/systm.h>
41 1.1 mjacob #include <sys/errno.h>
42 1.1 mjacob #include <sys/malloc.h>
43 1.1 mjacob #include <sys/device.h>
44 1.1 mjacob #include <sys/syslog.h>
45 1.1 mjacob
46 1.1 mjacob #include <vm/vm.h>
47 1.1 mjacob
48 1.1 mjacob #include <machine/autoconf.h>
49 1.1 mjacob
50 1.1 mjacob #include <dev/pci/pcireg.h>
51 1.1 mjacob #include <dev/pci/pcivar.h>
52 1.1 mjacob
53 1.1 mjacob #include <alpha/mcbus/mcbusvar.h>
54 1.1 mjacob #include <alpha/mcbus/mcbusreg.h>
55 1.1 mjacob #include <alpha/pci/mcpciareg.h>
56 1.1 mjacob #include <alpha/pci/mcpciavar.h>
57 1.1 mjacob #include <alpha/pci/pci_kn300.h>
58 1.1 mjacob
59 1.1 mjacob #ifndef EVCNT_COUNTERS
60 1.1 mjacob #include <machine/intrcnt.h>
61 1.1 mjacob #endif
62 1.1 mjacob
63 1.1 mjacob #include "sio.h"
64 1.1 mjacob #if NSIO
65 1.1 mjacob #include <alpha/pci/siovar.h>
66 1.1 mjacob #endif
67 1.1 mjacob
68 1.1 mjacob int
69 1.1 mjacob dec_kn300_intr_map __P((void *, pcitag_t, int, int, pci_intr_handle_t *));
70 1.1 mjacob const char *dec_kn300_intr_string __P((void *, pci_intr_handle_t));
71 1.1 mjacob void * dec_kn300_intr_establish __P((void *, pci_intr_handle_t,
72 1.1 mjacob int, int (*func)(void *), void *));
73 1.1 mjacob void dec_kn300_intr_disestablish __P((void *, void *));
74 1.1 mjacob
75 1.1 mjacob #define KN300_PCEB_IRQ 16
76 1.1 mjacob #define NPIN 4
77 1.1 mjacob
78 1.1 mjacob static struct vectab {
79 1.1 mjacob int (*func) __P((void *));
80 1.1 mjacob void *arg;
81 1.1 mjacob int irq;
82 1.1 mjacob } vectab[MAX_MC_BUS][MCPCIA_PER_MCBUS][MCPCIA_MAXSLOT][NPIN];
83 1.1 mjacob
84 1.1 mjacob struct mcpcia_config *mcpcia_eisaccp = NULL;
85 1.1 mjacob
86 1.1 mjacob #ifdef EVCNT_COUNTERS
87 1.1 mjacob struct evcnt kn300_intr_evcnt;
88 1.1 mjacob #endif
89 1.1 mjacob
90 1.1 mjacob int kn300_spurious __P((void *));
91 1.1 mjacob void kn300_iointr __P((void *, unsigned long));
92 1.1 mjacob void kn300_enable_intr __P((struct mcpcia_config *, int));
93 1.1 mjacob void kn300_disable_intr __P((struct mcpcia_config *, int));
94 1.1 mjacob
95 1.1 mjacob void
96 1.1 mjacob pci_kn300_pickintr(ccp, first)
97 1.1 mjacob struct mcpcia_config *ccp;
98 1.1 mjacob int first;
99 1.1 mjacob {
100 1.1 mjacob pci_chipset_tag_t pc = &ccp->cc_pc;
101 1.1 mjacob
102 1.1 mjacob if (first) {
103 1.1 mjacob int g;
104 1.1 mjacob for (g = 0; g < MAX_MC_BUS; g++) {
105 1.1 mjacob int m;
106 1.1 mjacob for (m = 0; m < MCPCIA_PER_MCBUS; m++) {
107 1.1 mjacob int s;
108 1.1 mjacob for (s = 0; s < MCPCIA_MAXSLOT; s++) {
109 1.1 mjacob int p;
110 1.1 mjacob for (p = 0; p < NPIN; p++) {
111 1.1 mjacob struct vectab *vp;
112 1.1 mjacob vp = &vectab[g][m][s][p];
113 1.1 mjacob vp->func = kn300_spurious;
114 1.1 mjacob vp->arg = (void *) ((long)
115 1.1 mjacob (g << 0) |
116 1.1 mjacob (m << 3) |
117 1.1 mjacob (s << 6) |
118 1.1 mjacob (p << 9));
119 1.1 mjacob vp->irq = -1;
120 1.1 mjacob }
121 1.1 mjacob }
122 1.1 mjacob }
123 1.1 mjacob }
124 1.1 mjacob set_iointr(kn300_iointr);
125 1.1 mjacob }
126 1.1 mjacob
127 1.1 mjacob pc->pc_intr_v = ccp;
128 1.1 mjacob pc->pc_intr_map = dec_kn300_intr_map;
129 1.1 mjacob pc->pc_intr_string = dec_kn300_intr_string;
130 1.1 mjacob pc->pc_intr_establish = dec_kn300_intr_establish;
131 1.1 mjacob pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
132 1.1 mjacob
133 1.1 mjacob #if NSIO
134 1.1 mjacob if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp->cc_sc)))) {
135 1.1 mjacob extern void dec_kn300_cons_init __P((void));
136 1.1 mjacob bus_space_tag_t iot = &ccp->cc_iot;
137 1.1 mjacob if (mcpcia_eisaccp) {
138 1.1 mjacob printf("Huh? There's only supposed to be one eisa!\n");
139 1.1 mjacob }
140 1.2 mjacob sio_intr_setup(pc, iot);
141 1.1 mjacob kn300_enable_intr(ccp, KN300_PCEB_IRQ);
142 1.1 mjacob mcpcia_eisaccp = ccp;
143 1.1 mjacob dec_kn300_cons_init(); /* XXXXXXXXXXXXXXXXXXX */
144 1.1 mjacob }
145 1.1 mjacob #endif
146 1.1 mjacob }
147 1.1 mjacob
148 1.1 mjacob int
149 1.1 mjacob dec_kn300_intr_map(ccv, bustag, buspin, line, ihp)
150 1.1 mjacob void *ccv;
151 1.1 mjacob pcitag_t bustag;
152 1.1 mjacob int buspin, line;
153 1.1 mjacob pci_intr_handle_t *ihp;
154 1.1 mjacob {
155 1.1 mjacob struct mcpcia_config *ccp = ccv;
156 1.1 mjacob pci_chipset_tag_t pc = &ccp->cc_pc;
157 1.1 mjacob int device;
158 1.1 mjacob int kn300_irq;
159 1.1 mjacob
160 1.1 mjacob if (buspin == 0) {
161 1.1 mjacob /* No IRQ used. */
162 1.1 mjacob return 1;
163 1.1 mjacob }
164 1.1 mjacob if (buspin > 4 || buspin < 0) {
165 1.1 mjacob printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
166 1.1 mjacob return 1;
167 1.1 mjacob }
168 1.1 mjacob
169 1.1 mjacob alpha_pci_decompose_tag(pc, bustag, NULL, &device, NULL);
170 1.1 mjacob
171 1.1 mjacob if (device == 1) {
172 1.1 mjacob /*
173 1.1 mjacob * This can only be the NCR810 SCSI.
174 1.1 mjacob * XXX: Do we need to check this more closely?
175 1.1 mjacob */
176 1.1 mjacob if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp->cc_sc)))) {
177 1.1 mjacob printf("XXXXX: How can you have an EISA in the spot "
178 1.1 mjacob "as an NCR 810?\n");
179 1.1 mjacob }
180 1.1 mjacob kn300_irq = 16;
181 1.1 mjacob } else if (device >= 2 && device <= 5) {
182 1.1 mjacob kn300_irq = (device - 2) * 4;
183 1.1 mjacob } else {
184 1.1 mjacob printf("dec_kn300_intr_map: weird device number %d\n", device);
185 1.1 mjacob return(1);
186 1.1 mjacob }
187 1.1 mjacob /*
188 1.1 mjacob * handle layout:
189 1.1 mjacob *
190 1.1 mjacob * bits 0..2 7-GID
191 1.1 mjacob * bits 3..5 MID-4
192 1.1 mjacob * bits 6..8 PCI Slot (0..7- yes, some don't exist)
193 1.1 mjacob * bits 9..10 buspin-1
194 1.1 mjacob * bits 11-15 IRQ
195 1.1 mjacob */
196 1.1 mjacob *ihp = (pci_intr_handle_t)
197 1.1 mjacob (7 - ccp->cc_sc->mcpcia_gid) |
198 1.1 mjacob ((ccp->cc_sc->mcpcia_mid - 4) << 3) |
199 1.1 mjacob (device << 6) |
200 1.1 mjacob ((buspin-1) << 9) |
201 1.1 mjacob (kn300_irq << 11);
202 1.1 mjacob return (0);
203 1.1 mjacob }
204 1.1 mjacob
205 1.1 mjacob const char *
206 1.1 mjacob dec_kn300_intr_string(ccv, ih)
207 1.1 mjacob void *ccv;
208 1.1 mjacob pci_intr_handle_t ih;
209 1.1 mjacob {
210 1.1 mjacob static char irqstr[64];
211 1.1 mjacob sprintf(irqstr, "kn300 irq %d PCI Interrupt Pin %c",
212 1.1 mjacob (ih >> 11) & 0x1f, ((ih >> 9) & 0x3) + 'A');
213 1.1 mjacob return (irqstr);
214 1.1 mjacob }
215 1.1 mjacob
216 1.1 mjacob void *
217 1.1 mjacob dec_kn300_intr_establish(ccv, ih, level, func, arg)
218 1.1 mjacob void *ccv;
219 1.1 mjacob pci_intr_handle_t ih;
220 1.1 mjacob int level;
221 1.1 mjacob int (*func) __P((void *));
222 1.1 mjacob void *arg;
223 1.1 mjacob {
224 1.1 mjacob struct vectab *vp;
225 1.1 mjacob int gidx, midx, slot, pidx, s;
226 1.1 mjacob void *cookie = NULL;
227 1.1 mjacob
228 1.1 mjacob gidx = ih & 0x7;
229 1.1 mjacob midx = (ih >> 3) & 0x7;
230 1.1 mjacob slot = (ih >> 6) & 0x7;
231 1.1 mjacob pidx = (ih >> 9) & 0x3;
232 1.1 mjacob
233 1.1 mjacob /*
234 1.1 mjacob * XXX: FIXME - for PCI bridges, the framework here is such that
235 1.1 mjacob * XXX: we're mapping the mapping for the bridge, not the
236 1.1 mjacob * XXX: device behind the bridge, so we're not cognizant of the
237 1.1 mjacob * XXX: pin swizzling that could go on. We really ought to fix
238 1.1 mjacob * XXX: this, but for a first cut, assuming all pins map to the
239 1.1 mjacob * XXX: same device and filling in all pin vectors is probably
240 1.1 mjacob * XXX: okay.
241 1.1 mjacob */
242 1.1 mjacob
243 1.1 mjacob for (pidx = 0; pidx < 4; pidx++) {
244 1.1 mjacob vp = &vectab[gidx][midx][slot][pidx];
245 1.1 mjacob if (vp->func != kn300_spurious) {
246 1.1 mjacob printf("dec_kn300_intr_establish: vector cookie 0x%x "
247 1.1 mjacob "already used\n", ih);
248 1.1 mjacob return (cookie);
249 1.1 mjacob }
250 1.1 mjacob s = splhigh();
251 1.1 mjacob vp->func = func;
252 1.1 mjacob vp->arg = arg;
253 1.1 mjacob vp->irq = (ih >> 11) & 0x1f;
254 1.1 mjacob (void) splx(s);
255 1.1 mjacob }
256 1.1 mjacob
257 1.1 mjacob kn300_enable_intr(ccv, (int)((ih >> 11) & 0x1f));
258 1.1 mjacob cookie = (void *) ih;
259 1.1 mjacob return (cookie);
260 1.1 mjacob }
261 1.1 mjacob
262 1.1 mjacob void
263 1.1 mjacob dec_kn300_intr_disestablish(ccv, cookie)
264 1.1 mjacob void *ccv, *cookie;
265 1.1 mjacob {
266 1.1 mjacob pci_intr_handle_t ih = (pci_intr_handle_t) cookie;
267 1.1 mjacob int gidx, midx, slot, pidx, s;
268 1.1 mjacob struct vectab *vp;
269 1.1 mjacob
270 1.1 mjacob gidx = ih & 0x7;
271 1.1 mjacob midx = (ih >> 3) & 0x7;
272 1.1 mjacob slot = (ih >> 6) & 0x7;
273 1.1 mjacob pidx = (ih >> 9) & 0x3;
274 1.1 mjacob vp = &vectab[gidx][midx][slot][pidx];
275 1.1 mjacob s = splhigh();
276 1.1 mjacob vp->func = kn300_spurious;
277 1.1 mjacob vp->arg = cookie;
278 1.1 mjacob vp->irq = -1;
279 1.1 mjacob (void) splx(s);
280 1.1 mjacob kn300_disable_intr(ccv, (int)((ih >> 11) & 0x1f));
281 1.1 mjacob }
282 1.1 mjacob
283 1.1 mjacob int
284 1.1 mjacob kn300_spurious(arg)
285 1.1 mjacob void *arg;
286 1.1 mjacob {
287 1.1 mjacob pci_intr_handle_t ih = (pci_intr_handle_t) arg;
288 1.1 mjacob int gidx, midx, slot, pidx;
289 1.1 mjacob
290 1.1 mjacob gidx = ih & 0x7;
291 1.1 mjacob midx = (ih >> 3) & 0x7;
292 1.1 mjacob slot = (ih >> 6) & 0x7;
293 1.1 mjacob pidx = (ih >> 9) & 0x3;
294 1.1 mjacob printf("Spurious Interrupt from mcbus%d MID %d Slot %d "
295 1.1 mjacob "PCI Interrupt Pin %c\n", gidx, midx + 4, slot, pidx + 'A');
296 1.1 mjacob /*
297 1.1 mjacob * XXX: We *could*, if we have recorded all the mcpcia softcs
298 1.1 mjacob * XXX: that we've configured, and this spurious interrupt
299 1.1 mjacob * XXX: falls within this range, disable this interrupt.
300 1.1 mjacob *
301 1.1 mjacob * XXX: Later.
302 1.1 mjacob */
303 1.1 mjacob return (-1);
304 1.1 mjacob }
305 1.1 mjacob
306 1.1 mjacob void
307 1.1 mjacob kn300_iointr(framep, vec)
308 1.1 mjacob void *framep;
309 1.1 mjacob unsigned long vec;
310 1.1 mjacob {
311 1.1 mjacob int base, gidx, midx, slot, pidx;
312 1.1 mjacob struct vectab *vp;
313 1.1 mjacob
314 1.1 mjacob if (vec >= MCPCIA_VEC_EISA && vec < MCPCIA_VEC_PCI) {
315 1.1 mjacob #if NSIO
316 1.1 mjacob sio_iointr(framep, vec);
317 1.1 mjacob return;
318 1.1 mjacob #else
319 1.1 mjacob printf("kn300_iointr: (E)ISA interrupt support not configured"
320 1.1 mjacob " for vector 0x%x", vec);
321 1.1 mjacob kn300_disable_intr(mcpcia_eisaccp, KN300_PCEB_IRQ);
322 1.1 mjacob #endif
323 1.1 mjacob }
324 1.1 mjacob
325 1.1 mjacob base = (int) vec - MCPCIA_VEC_PCI;
326 1.1 mjacob
327 1.1 mjacob midx = base / 0x200;
328 1.1 mjacob gidx = midx / 4;
329 1.1 mjacob midx = midx % 4;
330 1.1 mjacob slot = (base % 0x200) / 0x10;
331 1.1 mjacob pidx = ((slot / 4) & 0x3);
332 1.1 mjacob slot = slot / 4;
333 1.1 mjacob
334 1.1 mjacob if (gidx >= MAX_MC_BUS || midx >= MCPCIA_PER_MCBUS ||
335 1.1 mjacob slot >= MCPCIA_MAXSLOT || pidx >= NPIN) {
336 1.1 mjacob panic("kn300_iointr: vec 0x%x (mcbus%d mid%d slot %d pin%d)",
337 1.1 mjacob vec, gidx, midx+4, slot, pidx);
338 1.1 mjacob }
339 1.1 mjacob
340 1.1 mjacob #ifdef EVCNT_COUNTERS
341 1.1 mjacob kn300_intr_evcnt.ev_count++;
342 1.1 mjacob #endif
343 1.1 mjacob /*
344 1.1 mjacob * Check for i2c bus interrupts XXXXXXXXX
345 1.1 mjacob */
346 1.1 mjacob if (gidx == 0 && midx == 0 && vec == MCPCIA_I2C_CVEC) {
347 1.1 mjacob #ifndef EVCNT_COUNTERS
348 1.1 mjacob intrcnt[INTRCNT_KN300_I2C_CTRL]++;
349 1.1 mjacob #endif
350 1.1 mjacob printf("i2c: controller interrupt\n");
351 1.1 mjacob return;
352 1.1 mjacob }
353 1.1 mjacob if (gidx == 0 && midx == 0 && vec == MCPCIA_I2C_BVEC) {
354 1.1 mjacob #ifndef EVCNT_COUNTERS
355 1.1 mjacob intrcnt[INTRCNT_KN300_I2C_BUS]++;
356 1.1 mjacob #endif
357 1.1 mjacob printf("i2c: bus interrupt\n");
358 1.1 mjacob return;
359 1.1 mjacob }
360 1.1 mjacob
361 1.1 mjacob /*
362 1.1 mjacob * Now, for the main stuff...
363 1.1 mjacob */
364 1.1 mjacob vp = &vectab[gidx][midx][slot][pidx];
365 1.1 mjacob #ifndef EVCNT_COUNTERS
366 1.1 mjacob if (vp->irq >= 0 && vp->irq <= INTRCNT_KN300_NCR810) {
367 1.1 mjacob intrcnt[INTRCNT_KN300_IRQ + vp->irq]++;
368 1.1 mjacob }
369 1.1 mjacob #endif
370 1.1 mjacob if ((*vp->func)(vp->arg) == 0) {
371 1.1 mjacob #if 0
372 1.1 mjacob printf("Unclaimed interrupt from mcbus%d MID %d Slot %d "
373 1.1 mjacob "PCI Interrupt Pin %c\n", gidx, midx + 4, slot, pidx + 'A');
374 1.1 mjacob #endif
375 1.1 mjacob }
376 1.1 mjacob }
377 1.1 mjacob
378 1.1 mjacob void
379 1.1 mjacob kn300_enable_intr(ccp, irq)
380 1.1 mjacob struct mcpcia_config *ccp;
381 1.1 mjacob int irq;
382 1.1 mjacob {
383 1.1 mjacob alpha_mb();
384 1.1 mjacob REGVAL(MCPCIA_INT_MASK0(ccp->cc_sc)) |= (1 << irq);
385 1.1 mjacob alpha_mb();
386 1.1 mjacob }
387 1.1 mjacob
388 1.1 mjacob void
389 1.1 mjacob kn300_disable_intr(ccp, irq)
390 1.1 mjacob struct mcpcia_config *ccp;
391 1.1 mjacob int irq;
392 1.1 mjacob {
393 1.1 mjacob alpha_mb();
394 1.1 mjacob REGVAL(MCPCIA_INT_MASK0(ccp->cc_sc)) &= ~(1 << irq);
395 1.1 mjacob alpha_mb();
396 1.1 mjacob }
397