pci_kn300.c revision 1.19 1 /* $NetBSD: pci_kn300.c,v 1.19 2000/06/04 19:14:24 cgd 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.19 2000/06/04 19:14:24 cgd 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 > 0 || NPCEB > 0
65 #include <alpha/pci/siovar.h>
66 #endif
67
68 int dec_kn300_intr_map __P((void *, pcitag_t, int, int,
69 pci_intr_handle_t *));
70 const char *dec_kn300_intr_string __P((void *, pci_intr_handle_t));
71 const struct evcnt *dec_kn300_intr_evcnt __P((void *, pci_intr_handle_t));
72 void *dec_kn300_intr_establish __P((void *, pci_intr_handle_t,
73 int, int (*func)(void *), void *));
74 void dec_kn300_intr_disestablish __P((void *, void *));
75
76 #define KN300_PCEB_IRQ 16
77 #define NPIN 4
78
79 #define NIRQ (MAX_MC_BUS * MCPCIA_PER_MCBUS * MCPCIA_MAXSLOT * NPIN)
80 static int savirqs[NIRQ];
81
82 static struct alpha_shared_intr *kn300_pci_intr;
83
84 static struct mcpcia_config *mcpcia_eisaccp = NULL;
85
86 #ifdef EVCNT_COUNTERS
87 struct evcnt kn300_intr_evcnt;
88 #endif
89
90 void kn300_iointr __P((void *, unsigned long));
91 void kn300_enable_intr __P((struct mcpcia_config *, int));
92 void kn300_disable_intr __P((struct mcpcia_config *, int));
93
94 void
95 pci_kn300_pickintr(ccp, first)
96 struct mcpcia_config *ccp;
97 int first;
98 {
99 pci_chipset_tag_t pc = &ccp->cc_pc;
100
101 if (first) {
102 int g;
103
104 kn300_pci_intr = alpha_shared_intr_alloc(NIRQ);
105 for (g = 0; g < NIRQ; g++) {
106 alpha_shared_intr_set_maxstrays(kn300_pci_intr, g, 25);
107 savirqs[g] = (char) -1;
108 }
109 set_iointr(kn300_iointr);
110 }
111
112 pc->pc_intr_v = ccp;
113 pc->pc_intr_map = dec_kn300_intr_map;
114 pc->pc_intr_string = dec_kn300_intr_string;
115 pc->pc_intr_evcnt = dec_kn300_intr_evcnt;
116 pc->pc_intr_establish = dec_kn300_intr_establish;
117 pc->pc_intr_disestablish = dec_kn300_intr_disestablish;
118
119 /* Not supported on KN300. */
120 pc->pc_pciide_compat_intr_establish = NULL;
121
122 if (EISA_PRESENT(REGVAL(MCPCIA_PCI_REV(ccp)))) {
123 mcpcia_eisaccp = ccp;
124 #if NSIO > 0 || NPCEB > 0
125 sio_intr_setup(pc, &ccp->cc_iot);
126 kn300_enable_intr(ccp, KN300_PCEB_IRQ);
127 #endif
128 }
129 }
130
131 int
132 dec_kn300_intr_map(ccv, bustag, buspin, line, ihp)
133 void *ccv;
134 pcitag_t bustag;
135 int buspin, line;
136 pci_intr_handle_t *ihp;
137 {
138 struct mcpcia_config *ccp = ccv;
139 pci_chipset_tag_t pc = &ccp->cc_pc;
140 int device;
141 int mcpcia_irq;
142
143 if (buspin == 0) {
144 /* No IRQ used. */
145 return 1;
146 }
147 if (buspin > 4 || buspin < 0) {
148 printf("dec_kn300_intr_map: bad interrupt pin %d\n", buspin);
149 return 1;
150 }
151
152 alpha_pci_decompose_tag(pc, bustag, NULL, &device, NULL);
153
154 /*
155 * On MID 5 device 1 is the internal NCR 53c810.
156 */
157 if (ccp->cc_mid == 5 && device == 1) {
158 mcpcia_irq = 16;
159 } else if (device >= 2 && device <= 5) {
160 mcpcia_irq = (device - 2) * 4;
161 } else {
162 printf("dec_kn300_intr_map: weird device number %d\n", device);
163 return(1);
164 }
165
166 /*
167 * handle layout:
168 *
169 * Determine kn300 IRQ (encoded in SCB vector):
170 * bits 0..1 buspin-1
171 * bits 2..4 PCI Slot (0..7- yes, some don't exist)
172 * bits 5..7 MID-4
173 * bits 8..10 7-GID
174 *
175 * Software only:
176 * bits 11-15 MCPCIA IRQ
177 */
178 *ihp = (pci_intr_handle_t)
179 (buspin - 1 ) |
180 ((device & 0x7) << 2) |
181 ((ccp->cc_mid - 4) << 5) |
182 ((7 - ccp->cc_gid) << 8) |
183 (mcpcia_irq << 11);
184 return (0);
185 }
186
187 const char *
188 dec_kn300_intr_string(ccv, ih)
189 void *ccv;
190 pci_intr_handle_t ih;
191 {
192 static char irqstr[64];
193
194 sprintf(irqstr, "kn300 irq %ld", ih & 0x3ff);
195 return (irqstr);
196 }
197
198 const struct evcnt *
199 dec_kn300_intr_evcnt(ccv, ih)
200 void *ccv;
201 pci_intr_handle_t ih;
202 {
203
204 /* XXX for now, no evcnt parent reported */
205 return (NULL);
206 }
207
208 void *
209 dec_kn300_intr_establish(ccv, ih, level, func, arg)
210 void *ccv;
211 pci_intr_handle_t ih;
212 int level;
213 int (*func) __P((void *));
214 void *arg;
215 {
216 struct mcpcia_config *ccp = ccv;
217 void *cookie;
218 int irq;
219
220 irq = ih & 0x3ff;
221 cookie = alpha_shared_intr_establish(kn300_pci_intr, irq, IST_LEVEL,
222 level, func, arg, "kn300 irq");
223
224 if (cookie != NULL && alpha_shared_intr_isactive(kn300_pci_intr, irq)) {
225 alpha_shared_intr_set_private(kn300_pci_intr, irq, ccp);
226 savirqs[irq] = (ih >> 11) & 0x1f;
227 kn300_enable_intr(ccp, savirqs[irq]);
228 alpha_mb();
229 }
230 return (cookie);
231 }
232
233 void
234 dec_kn300_intr_disestablish(ccv, cookie)
235 void *ccv, *cookie;
236 {
237 panic("dec_kn300_intr_disestablish not implemented");
238 }
239
240 void
241 kn300_iointr(framep, vec)
242 void *framep;
243 unsigned long vec;
244 {
245 struct mcpcia_softc *mcp;
246 u_long irq;
247
248 if (vec >= MCPCIA_VEC_EISA && vec < MCPCIA_VEC_PCI) {
249 #if NSIO > 0 || NPCEB > 0
250 sio_iointr(framep, vec);
251 return;
252 #else
253 static const char *plaint = "kn300_iointr: (E)ISA interrupt "
254 "support not configured for vector 0x%x";
255 if (mcpcia_eisaccp) {
256 kn300_disable_intr(mcpcia_eisaccp, KN300_PCEB_IRQ);
257 printf(plaint, vec);
258 } else {
259 panic(plaint, vec);
260 }
261 #endif
262 }
263
264 #ifdef EVCNT_COUNTERS
265 kn300_intr_evcnt.ev_count++;
266 #endif
267
268 irq = (vec - MCPCIA_VEC_PCI) >> 4;
269
270 /*
271 * Check for I2C interrupts. These are technically within
272 * the PCI vector range, but no PCI device should ever map
273 * to them.
274 */
275 if (vec == MCPCIA_I2C_CVEC) {
276 #ifndef EVCNT_COUNTERS
277 intrcnt[INTRCNT_KN300_I2C_CTRL]++;
278 #endif
279 printf("i2c: controller interrupt\n");
280 return;
281 }
282 if (vec == MCPCIA_I2C_BVEC) {
283 #ifndef EVCNT_COUNTERS
284 intrcnt[INTRCNT_KN300_I2C_BUS]++;
285 #endif
286 printf("i2c: bus interrupt\n");
287 return;
288 }
289
290 #ifndef EVCNT_COUNTERS
291 if (savirqs[irq] >= 0 && savirqs[irq] <= INTRCNT_KN300_NCR810) {
292 intrcnt[INTRCNT_KN300_IRQ + savirqs[irq]]++;
293 }
294 #endif
295
296 if (alpha_shared_intr_dispatch(kn300_pci_intr, irq)) {
297 /*
298 * Any claim of an interrupt at this level is a hint to
299 * reset the stray interrupt count- elsewise a slow leak
300 * over time will cause this level to be shutdown.
301 */
302 alpha_shared_intr_set_maxstrays(kn300_pci_intr, irq, 25);
303 return;
304 }
305
306 /*
307 * If we haven't finished configuring yet, or there is no mcp
308 * registered for this level yet, just return.
309 */
310 mcp = alpha_shared_intr_get_private(kn300_pci_intr, irq);
311 if (mcp == NULL || mcp->mcpcia_cc == NULL)
312 return;
313
314 /*
315 * We're getting an interrupt for a device we haven't enabled.
316 * We had better not try and use -1 to find the right bit to disable.
317 */
318 if (savirqs[irq] == -1) {
319 printf("kn300_iointr: stray interrupt vector 0x%lx\n", vec);
320 return;
321 }
322
323 /*
324 * Stray interrupt; disable the IRQ on the appropriate MCPCIA
325 * if we've reached the limit.
326 */
327 alpha_shared_intr_stray(kn300_pci_intr, irq, "kn300 irq");
328 if (ALPHA_SHARED_INTR_DISABLE(kn300_pci_intr, irq) == 0)
329 return;
330 kn300_disable_intr(mcp->mcpcia_cc, savirqs[irq]);
331 }
332
333 void
334 kn300_enable_intr(ccp, irq)
335 struct mcpcia_config *ccp;
336 int irq;
337 {
338 alpha_mb();
339 REGVAL(MCPCIA_INT_MASK0(ccp)) |= (1 << irq);
340 alpha_mb();
341 }
342
343 void
344 kn300_disable_intr(ccp, irq)
345 struct mcpcia_config *ccp;
346 int irq;
347 {
348 alpha_mb();
349 REGVAL(MCPCIA_INT_MASK0(ccp)) &= ~(1 << irq);
350 alpha_mb();
351 }
352