pci_550.c revision 1.29 1 /* $NetBSD: pci_550.c,v 1.29 2009/03/14 14:45:53 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Gallatin.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
35 * All rights reserved.
36 *
37 * Author: Chris G. Demetriou
38 *
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
44 *
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * Carnegie Mellon requests users of this software to return to
50 *
51 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
55 *
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
58 */
59
60 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
61
62 __KERNEL_RCSID(0, "$NetBSD: pci_550.c,v 1.29 2009/03/14 14:45:53 dsl Exp $");
63
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/time.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/device.h>
71 #include <sys/syslog.h>
72
73 #include <uvm/uvm_extern.h>
74
75 #include <machine/autoconf.h>
76 #include <machine/rpb.h>
77
78 #include <dev/pci/pcireg.h>
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pciidereg.h>
81 #include <dev/pci/pciidevar.h>
82
83 #include <alpha/pci/ciareg.h>
84 #include <alpha/pci/ciavar.h>
85
86 #include <alpha/pci/pci_550.h>
87
88 #include "sio.h"
89 #if NSIO
90 #include <alpha/pci/siovar.h>
91 #endif
92
93 int dec_550_intr_map(struct pci_attach_args *,
94 pci_intr_handle_t *);
95 const char *dec_550_intr_string(void *, pci_intr_handle_t);
96 const struct evcnt *dec_550_intr_evcnt(void *, pci_intr_handle_t);
97 void *dec_550_intr_establish(void *, pci_intr_handle_t,
98 int, int (*func)(void *), void *);
99 void dec_550_intr_disestablish(void *, void *);
100
101 void *dec_550_pciide_compat_intr_establish(void *, struct device *,
102 struct pci_attach_args *, int, int (*)(void *), void *);
103
104 #define DEC_550_PCI_IRQ_BEGIN 8
105 #define DEC_550_MAX_IRQ (64 - DEC_550_PCI_IRQ_BEGIN)
106
107 /*
108 * The Miata has a Pyxis, which seems to have problems with stray
109 * interrupts. Work around this by just ignoring strays.
110 */
111 #define PCI_STRAY_MAX 0
112
113 /*
114 * Some Miata models, notably models with a Cypress PCI-ISA bridge, have
115 * a PCI device (the OHCI USB controller) with interrupts tied to ISA IRQ
116 * lines. This IRQ is encoded as: line = FLAG | isa_irq. Usually FLAG
117 * is 0xe0, however, it can be 0xf0. We don't allow 0xf0 | irq15.
118 */
119 #define DEC_550_LINE_IS_ISA(line) ((line) >= 0xe0 && (line) <= 0xfe)
120 #define DEC_550_LINE_ISA_IRQ(line) ((line) & 0x0f)
121
122 struct alpha_shared_intr *dec_550_pci_intr;
123
124 void dec_550_iointr(void *arg, unsigned long vec);
125 void dec_550_intr_enable(int irq);
126 void dec_550_intr_disable(int irq);
127
128 void
129 pci_550_pickintr(ccp)
130 struct cia_config *ccp;
131 {
132 bus_space_tag_t iot = &ccp->cc_iot;
133 pci_chipset_tag_t pc = &ccp->cc_pc;
134 char *cp;
135 int i;
136
137 pc->pc_intr_v = ccp;
138 pc->pc_intr_map = dec_550_intr_map;
139 pc->pc_intr_string = dec_550_intr_string;
140 pc->pc_intr_evcnt = dec_550_intr_evcnt;
141 pc->pc_intr_establish = dec_550_intr_establish;
142 pc->pc_intr_disestablish = dec_550_intr_disestablish;
143
144 pc->pc_pciide_compat_intr_establish =
145 dec_550_pciide_compat_intr_establish;
146
147 /*
148 * DEC 550's interrupts are enabled via the Pyxis interrupt
149 * mask register. Nothing to map.
150 */
151
152 for (i = 0; i < DEC_550_MAX_IRQ; i++)
153 dec_550_intr_disable(i);
154
155 dec_550_pci_intr = alpha_shared_intr_alloc(DEC_550_MAX_IRQ, 8);
156 for (i = 0; i < DEC_550_MAX_IRQ; i++) {
157 alpha_shared_intr_set_maxstrays(dec_550_pci_intr, i,
158 PCI_STRAY_MAX);
159 alpha_shared_intr_set_private(dec_550_pci_intr, i, ccp);
160
161 cp = alpha_shared_intr_string(dec_550_pci_intr, i);
162 sprintf(cp, "irq %d", i);
163 evcnt_attach_dynamic(alpha_shared_intr_evcnt(
164 dec_550_pci_intr, i), EVCNT_TYPE_INTR, NULL,
165 "dec_550", cp);
166 }
167
168 #if NSIO
169 sio_intr_setup(pc, iot);
170 #endif
171 }
172
173 int
174 dec_550_intr_map(pa, ihp)
175 struct pci_attach_args *pa;
176 pci_intr_handle_t *ihp;
177 {
178 pcitag_t bustag = pa->pa_intrtag;
179 int buspin = pa->pa_intrpin, line = pa->pa_intrline;
180 pci_chipset_tag_t pc = pa->pa_pc;
181 int bus, device, function;
182
183 if (buspin == 0) {
184 /* No IRQ used. */
185 return 1;
186 }
187 if (buspin > 4) {
188 printf("dec_550_intr_map: bad interrupt pin %d\n", buspin);
189 return 1;
190 }
191
192 pci_decompose_tag(pc, bustag, &bus, &device, &function);
193
194 /*
195 * There are two main variants of Miata: Miata 1 (Intel SIO)
196 * and Miata {1.5,2} (Cypress).
197 *
198 * The Miata 1 has a CMD PCI IDE wired to compatibility mode at
199 * device 4 of bus 0. This variant apparently also has the
200 * Pyxis DMA bug.
201 *
202 * On the Miata 1.5 and Miata 2, the Cypress PCI-ISA bridge lives
203 * on device 7 of bus 0. This device has PCI IDE wired to
204 * compatibility mode on functions 1 and 2.
205 *
206 * There will be no interrupt mapping for these devices, so just
207 * bail out now.
208 */
209 if (bus == 0) {
210 if ((hwrpb->rpb_variation & SV_ST_MASK) < SV_ST_MIATA_1_5) {
211 /* Miata 1 */
212 if (device == 7)
213 panic("dec_550_intr_map: SIO device");
214 else if (device == 4)
215 return (1);
216 } else {
217 /* Miata 1.5 or Miata 2 */
218 if (device == 7) {
219 if (function == 0)
220 panic("dec_550_intr_map: SIO device");
221 if (function == 1 || function == 2)
222 return (1);
223 }
224 }
225 }
226
227 /*
228 * The console places the interrupt mapping in the "line" value.
229 * A value of (char)-1 indicates there is no mapping.
230 */
231 if (line == 0xff) {
232 printf("dec_550_intr_map: no mapping for %d/%d/%d\n",
233 bus, device, function);
234 return (1);
235 }
236
237 #if NSIO == 0
238 if (DEC_550_LINE_IS_ISA(line)) {
239 printf("dec_550_intr_map: ISA IRQ %d for %d/%d/%d\n",
240 DEC_550_LINE_ISA_IRQ(line), bus, device, function);
241 return (1);
242 }
243 #endif
244
245 if (DEC_550_LINE_IS_ISA(line) == 0 && line >= DEC_550_MAX_IRQ) {
246 printf("dec_550_intr_map: irq %d out of range %d/%d/%d\n",
247 line, bus, device, function);
248 return (1);
249 }
250 *ihp = line;
251 return (0);
252 }
253
254 const char *
255 dec_550_intr_string(ccv, ih)
256 void *ccv;
257 pci_intr_handle_t ih;
258 {
259 #if 0
260 struct cia_config *ccp = ccv;
261 #endif
262 static char irqstr[16]; /* 12 + 2 + NULL + sanity */
263
264 #if NSIO
265 if (DEC_550_LINE_IS_ISA(ih))
266 return (sio_intr_string(NULL /*XXX*/,
267 DEC_550_LINE_ISA_IRQ(ih)));
268 #endif
269
270 if (ih >= DEC_550_MAX_IRQ)
271 panic("dec_550_intr_string: bogus 550 IRQ 0x%lx", ih);
272 sprintf(irqstr, "dec 550 irq %ld", ih);
273 return (irqstr);
274 }
275
276 const struct evcnt *
277 dec_550_intr_evcnt(ccv, ih)
278 void *ccv;
279 pci_intr_handle_t ih;
280 {
281 #if 0
282 struct cia_config *ccp = ccv;
283 #endif
284
285 #if NSIO
286 if (DEC_550_LINE_IS_ISA(ih))
287 return (sio_intr_evcnt(NULL /*XXX*/,
288 DEC_550_LINE_ISA_IRQ(ih)));
289 #endif
290
291 if (ih >= DEC_550_MAX_IRQ)
292 panic("dec_550_intr_evcnt: bogus 550 IRQ 0x%lx", ih);
293
294 return (alpha_shared_intr_evcnt(dec_550_pci_intr, ih));
295 }
296
297 void *
298 dec_550_intr_establish(ccv, ih, level, func, arg)
299 void *ccv, *arg;
300 pci_intr_handle_t ih;
301 int level;
302 int (*func)(void *);
303 {
304 #if 0
305 struct cia_config *ccp = ccv;
306 #endif
307 void *cookie;
308
309 #if NSIO
310 if (DEC_550_LINE_IS_ISA(ih))
311 return (sio_intr_establish(NULL /*XXX*/,
312 DEC_550_LINE_ISA_IRQ(ih), IST_LEVEL, level, func, arg));
313 #endif
314
315 if (ih >= DEC_550_MAX_IRQ)
316 panic("dec_550_intr_establish: bogus dec 550 IRQ 0x%lx", ih);
317
318 cookie = alpha_shared_intr_establish(dec_550_pci_intr, ih, IST_LEVEL,
319 level, func, arg, "dec 550 irq");
320
321 if (cookie != NULL &&
322 alpha_shared_intr_firstactive(dec_550_pci_intr, ih)) {
323 scb_set(0x900 + SCB_IDXTOVEC(ih), dec_550_iointr, NULL,
324 level);
325 dec_550_intr_enable(ih);
326 }
327 return (cookie);
328 }
329
330 void
331 dec_550_intr_disestablish(ccv, cookie)
332 void *ccv, *cookie;
333 {
334 struct cia_config *ccp = ccv;
335 struct alpha_shared_intrhand *ih = cookie;
336 unsigned int irq = ih->ih_num;
337 int s;
338
339 #if NSIO
340 /*
341 * We have to determine if this is an ISA IRQ or not! We do this
342 * by checking to see if the intrhand points back to an intrhead
343 * that points to our cia_config. If not, it's an ISA IRQ. Pretty
344 * disgusting, eh?
345 */
346 if (ih->ih_intrhead->intr_private != ccp) {
347 sio_intr_disestablish(NULL /*XXX*/, cookie);
348 return;
349 }
350 #endif
351
352 s = splhigh();
353
354 alpha_shared_intr_disestablish(dec_550_pci_intr, cookie,
355 "dec 550 irq");
356 if (alpha_shared_intr_isactive(dec_550_pci_intr, irq) == 0) {
357 dec_550_intr_disable(irq);
358 alpha_shared_intr_set_dfltsharetype(dec_550_pci_intr, irq,
359 IST_NONE);
360 scb_free(0x900 + SCB_IDXTOVEC(irq));
361 }
362
363 splx(s);
364 }
365
366 void *
367 dec_550_pciide_compat_intr_establish(v, dev, pa, chan, func, arg)
368 void *v;
369 struct device *dev;
370 struct pci_attach_args *pa;
371 int chan;
372 int (*func)(void *);
373 void *arg;
374 {
375 pci_chipset_tag_t pc = pa->pa_pc;
376 void *cookie = NULL;
377 int bus, irq;
378
379 pci_decompose_tag(pc, pa->pa_tag, &bus, NULL, NULL);
380
381 /*
382 * If this isn't PCI bus #0, all bets are off.
383 */
384 if (bus != 0)
385 return (NULL);
386
387 irq = PCIIDE_COMPAT_IRQ(chan);
388 #if NSIO
389 cookie = sio_intr_establish(NULL /*XXX*/, irq, IST_EDGE, IPL_BIO,
390 func, arg);
391 if (cookie == NULL)
392 return (NULL);
393 printf("%s: %s channel interrupting at %s\n", dev->dv_xname,
394 PCIIDE_CHANNEL_NAME(chan), sio_intr_string(NULL /*XXX*/, irq));
395 #endif
396 return (cookie);
397 }
398
399 void
400 dec_550_iointr(arg, vec)
401 void *arg;
402 unsigned long vec;
403 {
404 int irq;
405
406 irq = SCB_VECTOIDX(vec - 0x900);
407
408 if (irq >= DEC_550_MAX_IRQ)
409 panic("550_iointr: vec 0x%lx out of range", vec);
410
411 if (!alpha_shared_intr_dispatch(dec_550_pci_intr, irq)) {
412 alpha_shared_intr_stray(dec_550_pci_intr, irq,
413 "dec 550 irq");
414 if (ALPHA_SHARED_INTR_DISABLE(dec_550_pci_intr, irq))
415 dec_550_intr_disable(irq);
416 } else
417 alpha_shared_intr_reset_strays(dec_550_pci_intr, irq);
418 }
419
420 void
421 dec_550_intr_enable(irq)
422 int irq;
423 {
424
425 cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 1);
426 }
427
428 void
429 dec_550_intr_disable(irq)
430 int irq;
431 {
432
433 cia_pyxis_intr_enable(irq + DEC_550_PCI_IRQ_BEGIN, 0);
434 }
435