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