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