pci_kn20aa.c revision 1.53 1 /* $NetBSD: pci_kn20aa.c,v 1.53 2012/02/06 02:14:15 matt 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 <sys/cdefs.h> /* RCS ID & Copyright macro defns */
31
32 __KERNEL_RCSID(0, "$NetBSD: pci_kn20aa.c,v 1.53 2012/02/06 02:14:15 matt Exp $");
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 #include <sys/syslog.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 #include "sio.h"
54 #if NSIO > 0 || NPCEB > 0
55 #include <alpha/pci/siovar.h>
56 #endif
57
58 int dec_kn20aa_intr_map(const struct pci_attach_args *,
59 pci_intr_handle_t *);
60 const char *dec_kn20aa_intr_string(void *, pci_intr_handle_t);
61 const struct evcnt *dec_kn20aa_intr_evcnt(void *, pci_intr_handle_t);
62 void *dec_kn20aa_intr_establish(void *, pci_intr_handle_t,
63 int, int (*func)(void *), void *);
64 void dec_kn20aa_intr_disestablish(void *, void *);
65
66 #define KN20AA_PCEB_IRQ 31
67 #define KN20AA_MAX_IRQ 32
68 #define PCI_STRAY_MAX 5
69
70 struct alpha_shared_intr *kn20aa_pci_intr;
71
72 void kn20aa_iointr(void *arg, unsigned long vec);
73 void kn20aa_enable_intr(int irq);
74 void kn20aa_disable_intr(int irq);
75
76 void
77 pci_kn20aa_pickintr(struct cia_config *ccp)
78 {
79 int i;
80 #if NSIO > 0 || NPCEB > 0
81 bus_space_tag_t iot = &ccp->cc_iot;
82 #endif
83 pci_chipset_tag_t pc = &ccp->cc_pc;
84 char *cp;
85
86 pc->pc_intr_v = ccp;
87 pc->pc_intr_map = dec_kn20aa_intr_map;
88 pc->pc_intr_string = dec_kn20aa_intr_string;
89 pc->pc_intr_evcnt = dec_kn20aa_intr_evcnt;
90 pc->pc_intr_establish = dec_kn20aa_intr_establish;
91 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
92
93 /* Not supported on KN20AA. */
94 pc->pc_pciide_compat_intr_establish = NULL;
95
96 kn20aa_pci_intr = alpha_shared_intr_alloc(KN20AA_MAX_IRQ, 8);
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 cp = alpha_shared_intr_string(kn20aa_pci_intr, i);
102 sprintf(cp, "irq %d", i);
103 evcnt_attach_dynamic(alpha_shared_intr_evcnt(
104 kn20aa_pci_intr, i), EVCNT_TYPE_INTR, NULL,
105 "kn20aa", cp);
106 }
107
108 #if NSIO > 0 || NPCEB > 0
109 sio_intr_setup(pc, iot);
110 kn20aa_enable_intr(KN20AA_PCEB_IRQ);
111 #endif
112 }
113
114 int
115 dec_kn20aa_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
116 {
117 pcitag_t bustag = pa->pa_intrtag;
118 int buspin = pa->pa_intrpin;
119 pci_chipset_tag_t pc = pa->pa_pc;
120 int device;
121 int kn20aa_irq;
122
123 if (buspin == 0) {
124 /* No IRQ used. */
125 return 1;
126 }
127 if (buspin > 4) {
128 printf("dec_kn20aa_intr_map: bad interrupt pin %d\n", buspin);
129 return 1;
130 }
131
132 /*
133 * Slot->interrupt translation. Appears to work, though it
134 * may not hold up forever.
135 *
136 * The DEC engineers who did this hardware obviously engaged
137 * in random drug testing.
138 */
139 pci_decompose_tag(pc, bustag, NULL, &device, NULL);
140 switch (device) {
141 case 11:
142 case 12:
143 kn20aa_irq = ((device - 11) + 0) * 4;
144 break;
145
146 case 7:
147 kn20aa_irq = 8;
148 break;
149
150 case 9:
151 kn20aa_irq = 12;
152 break;
153
154 case 6: /* 21040 on AlphaStation 500 */
155 kn20aa_irq = 13;
156 break;
157
158 case 8:
159 kn20aa_irq = 16;
160 break;
161
162 default:
163 printf("dec_kn20aa_intr_map: weird device number %d\n",
164 device);
165 return 1;
166 }
167
168 kn20aa_irq += buspin - 1;
169 if (kn20aa_irq > KN20AA_MAX_IRQ)
170 panic("dec_kn20aa_intr_map: kn20aa_irq too large (%d)",
171 kn20aa_irq);
172
173 *ihp = kn20aa_irq;
174 return (0);
175 }
176
177 const char *
178 dec_kn20aa_intr_string(void *ccv, 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%lx",
187 ih);
188
189 sprintf(irqstr, "kn20aa irq %ld", ih);
190 return (irqstr);
191 }
192
193 const struct evcnt *
194 dec_kn20aa_intr_evcnt(void *ccv, pci_intr_handle_t ih)
195 {
196 #if 0
197 struct cia_config *ccp = ccv;
198 #endif
199
200 if (ih > KN20AA_MAX_IRQ)
201 panic("dec_kn20aa_intr_string: bogus kn20aa IRQ 0x%lx", ih);
202 return (alpha_shared_intr_evcnt(kn20aa_pci_intr, ih));
203 }
204
205 void *
206 dec_kn20aa_intr_establish(
207 void *ccv,
208 pci_intr_handle_t ih,
209 int level,
210 int (*func)(void *),
211 void *arg)
212 {
213 #if 0
214 struct cia_config *ccp = ccv;
215 #endif
216 void *cookie;
217
218 if (ih > KN20AA_MAX_IRQ)
219 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%lx",
220 ih);
221
222 cookie = alpha_shared_intr_establish(kn20aa_pci_intr, ih, IST_LEVEL,
223 level, func, arg, "kn20aa irq");
224
225 if (cookie != NULL &&
226 alpha_shared_intr_firstactive(kn20aa_pci_intr, ih)) {
227 scb_set(0x900 + SCB_IDXTOVEC(ih), kn20aa_iointr, NULL,
228 level);
229 kn20aa_enable_intr(ih);
230 }
231 return (cookie);
232 }
233
234 void
235 dec_kn20aa_intr_disestablish(void *ccv, void *cookie)
236 {
237 #if 0
238 struct cia_config *ccp = ccv;
239 #endif
240 struct alpha_shared_intrhand *ih = cookie;
241 unsigned int irq = ih->ih_num;
242 int s;
243
244 s = splhigh();
245
246 alpha_shared_intr_disestablish(kn20aa_pci_intr, cookie,
247 "kn20aa irq");
248 if (alpha_shared_intr_isactive(kn20aa_pci_intr, irq) == 0) {
249 kn20aa_disable_intr(irq);
250 alpha_shared_intr_set_dfltsharetype(kn20aa_pci_intr, irq,
251 IST_NONE);
252 scb_free(0x900 + SCB_IDXTOVEC(irq));
253 }
254
255 splx(s);
256 }
257
258 void
259 kn20aa_iointr(void *arg, unsigned long vec)
260 {
261 int irq;
262
263 irq = SCB_VECTOIDX(vec - 0x900);
264
265 if (!alpha_shared_intr_dispatch(kn20aa_pci_intr, irq)) {
266 alpha_shared_intr_stray(kn20aa_pci_intr, irq,
267 "kn20aa irq");
268 if (ALPHA_SHARED_INTR_DISABLE(kn20aa_pci_intr, irq))
269 kn20aa_disable_intr(irq);
270 } else
271 alpha_shared_intr_reset_strays(kn20aa_pci_intr, irq);
272 }
273
274 void
275 kn20aa_enable_intr(int irq)
276 {
277
278 /*
279 * From disassembling small bits of the OSF/1 kernel:
280 * the following appears to enable a given interrupt request.
281 * "blech." I'd give valuable body parts for better docs or
282 * for a good decompiler.
283 */
284 alpha_mb();
285 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */
286 alpha_mb();
287 }
288
289 void
290 kn20aa_disable_intr(int irq)
291 {
292
293 alpha_mb();
294 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */
295 alpha_mb();
296 }
297