pci_kn20aa.c revision 1.22 1 /* $NetBSD: pci_kn20aa.c,v 1.22 1997/04/07 02:01:25 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <machine/options.h> /* Pull in config options headers */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 #include <sys/syslog.h>
40
41 #include <vm/vm.h>
42
43 #include <machine/autoconf.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47
48 #include <alpha/pci/ciareg.h>
49 #include <alpha/pci/ciavar.h>
50
51 #include <alpha/pci/pci_kn20aa.h>
52
53 #ifndef EVCNT_COUNTERS
54 #include <machine/intrcnt.h>
55 #endif
56
57 #include "sio.h"
58 #if NSIO
59 #include <alpha/pci/siovar.h>
60 #endif
61
62 int dec_kn20aa_intr_map __P((void *, pcitag_t, int, int,
63 pci_intr_handle_t *));
64 const char *dec_kn20aa_intr_string __P((void *, pci_intr_handle_t));
65 void *dec_kn20aa_intr_establish __P((void *, pci_intr_handle_t,
66 int, int (*func)(void *), void *));
67 void dec_kn20aa_intr_disestablish __P((void *, void *));
68
69 #define KN20AA_PCEB_IRQ 31
70 #define KN20AA_MAX_IRQ 32
71 #define PCI_STRAY_MAX 5
72
73 struct alpha_shared_intr *kn20aa_pci_intr;
74 #ifdef EVCNT_COUNTERS
75 struct evcnt kn20aa_intr_evcnt;
76 #endif
77
78 void kn20aa_iointr __P((void *framep, unsigned long vec));
79 void kn20aa_enable_intr __P((int irq));
80 void kn20aa_disable_intr __P((int irq));
81
82 void
83 pci_kn20aa_pickintr(ccp)
84 struct cia_config *ccp;
85 {
86 int i;
87 bus_space_tag_t iot = ccp->cc_iot;
88 pci_chipset_tag_t pc = &ccp->cc_pc;
89
90 pc->pc_intr_v = ccp;
91 pc->pc_intr_map = dec_kn20aa_intr_map;
92 pc->pc_intr_string = dec_kn20aa_intr_string;
93 pc->pc_intr_establish = dec_kn20aa_intr_establish;
94 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
95
96 kn20aa_pci_intr = alpha_shared_intr_alloc(KN20AA_MAX_IRQ);
97 for (i = 0; i < KN20AA_MAX_IRQ; i++)
98 alpha_shared_intr_set_maxstrays(kn20aa_pci_intr, i,
99 PCI_STRAY_MAX);
100
101 #if NSIO
102 sio_intr_setup(iot);
103 kn20aa_enable_intr(KN20AA_PCEB_IRQ);
104 #endif
105
106 set_iointr(kn20aa_iointr);
107 }
108
109 int
110 dec_kn20aa_intr_map(ccv, bustag, buspin, line, ihp)
111 void *ccv;
112 pcitag_t bustag;
113 int buspin, line;
114 pci_intr_handle_t *ihp;
115 {
116 struct cia_config *ccp = ccv;
117 pci_chipset_tag_t pc = &ccp->cc_pc;
118 int device;
119 int kn20aa_irq;
120
121 if (buspin == 0) {
122 /* No IRQ used. */
123 return 1;
124 }
125 if (buspin > 4) {
126 printf("pci_map_int: bad interrupt pin %d\n", buspin);
127 return 1;
128 }
129
130 /*
131 * Slot->interrupt translation. Appears to work, though it
132 * may not hold up forever.
133 *
134 * The DEC engineers who did this hardware obviously engaged
135 * in random drug testing.
136 */
137 pci_decompose_tag(pc, bustag, NULL, &device, NULL);
138 switch (device) {
139 case 11:
140 case 12:
141 kn20aa_irq = ((device - 11) + 0) * 4;
142 break;
143
144 case 7:
145 kn20aa_irq = 8;
146 break;
147
148 case 9:
149 kn20aa_irq = 12;
150 break;
151
152 case 6: /* 21040 on AlphaStation 500 */
153 kn20aa_irq = 13;
154 break;
155
156 case 8:
157 kn20aa_irq = 16;
158 break;
159
160 default:
161 printf("dec_kn20aa_intr_map: weird device number %d\n",
162 device);
163 return 1;
164 }
165
166 kn20aa_irq += buspin - 1;
167 if (kn20aa_irq > KN20AA_MAX_IRQ)
168 panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
169 kn20aa_irq);
170
171 *ihp = kn20aa_irq;
172 return (0);
173 }
174
175 const char *
176 dec_kn20aa_intr_string(ccv, ih)
177 void *ccv;
178 pci_intr_handle_t ih;
179 {
180 #if 0
181 struct cia_config *ccp = ccv;
182 #endif
183 static char irqstr[15]; /* 11 + 2 + NULL + sanity */
184
185 if (ih > KN20AA_MAX_IRQ)
186 panic("dec_kn20aa_intr_string: bogus kn20aa IRQ 0x%x\n",
187 ih);
188
189 sprintf(irqstr, "kn20aa irq %ld", ih);
190 return (irqstr);
191 }
192
193 void *
194 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
195 void *ccv, *arg;
196 pci_intr_handle_t ih;
197 int level;
198 int (*func) __P((void *));
199 {
200 #if 0
201 struct cia_config *ccp = ccv;
202 #endif
203 void *cookie;
204
205 if (ih > KN20AA_MAX_IRQ)
206 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
207 ih);
208
209 cookie = alpha_shared_intr_establish(kn20aa_pci_intr, ih, IST_LEVEL,
210 level, func, arg, "kn20aa irq");
211
212 if (cookie != NULL &&
213 alpha_shared_intr_isactive(kn20aa_pci_intr, ih))
214 kn20aa_enable_intr(ih);
215 return (cookie);
216 }
217
218 void
219 dec_kn20aa_intr_disestablish(ccv, cookie)
220 void *ccv, *cookie;
221 {
222 #if 0
223 struct cia_config *ccp = ccv;
224 #endif
225
226 panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
227 }
228
229 void
230 kn20aa_iointr(framep, vec)
231 void *framep;
232 unsigned long vec;
233 {
234 int irq;
235
236 if (vec >= 0x900) {
237 if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
238 panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
239 irq = (vec - 0x900) >> 4;
240
241 #ifdef EVCNT_COUNTERS
242 kn20aa_intr_evcnt.ev_count++;
243 #else
244 if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
245 panic("kn20aa interrupt counter sizes inconsistent");
246 intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
247 #endif
248
249 if (!alpha_shared_intr_dispatch(kn20aa_pci_intr, irq)) {
250 alpha_shared_intr_stray(kn20aa_pci_intr, irq,
251 "kn20aa irq");
252 if (kn20aa_pci_intr[irq].intr_nstrays ==
253 kn20aa_pci_intr[irq].intr_maxstrays)
254 kn20aa_disable_intr(irq);
255 }
256 return;
257 }
258 #if NSIO
259 if (vec >= 0x800) {
260 sio_iointr(framep, vec);
261 return;
262 }
263 #endif
264 panic("kn20aa_iointr: weird vec 0x%x\n", vec);
265 }
266
267 void
268 kn20aa_enable_intr(irq)
269 int irq;
270 {
271
272 /*
273 * From disassembling small bits of the OSF/1 kernel:
274 * the following appears to enable a given interrupt request.
275 * "blech." I'd give valuable body parts for better docs or
276 * for a good decompiler.
277 */
278 alpha_mb();
279 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */
280 alpha_mb();
281 }
282
283 void
284 kn20aa_disable_intr(irq)
285 int irq;
286 {
287
288 alpha_mb();
289 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */
290 alpha_mb();
291 }
292