sio_pic.c revision 1.20 1 /* $NetBSD: sio_pic.c,v 1.20 1998/04/14 22:31:17 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 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.
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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Author: Chris G. Demetriou
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
68
69 __KERNEL_RCSID(0, "$NetBSD: sio_pic.c,v 1.20 1998/04/14 22:31:17 thorpej Exp $");
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/device.h>
74 #include <sys/malloc.h>
75 #include <sys/syslog.h>
76
77 #include <machine/intr.h>
78 #include <machine/bus.h>
79
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcivar.h>
82 #include <dev/pci/pcidevs.h>
83
84 #include <dev/isa/isareg.h>
85 #include <dev/isa/isavar.h>
86 #include <alpha/pci/siovar.h>
87
88 #ifndef EVCNT_COUNTERS
89 #include <machine/intrcnt.h>
90 #endif
91
92 #include "sio.h"
93
94 /*
95 * To add to the long history of wonderful PROM console traits,
96 * AlphaStation PROMs don't reset themselves completely on boot!
97 * Therefore, if an interrupt was turned on when the kernel was
98 * started, we're not going to EVER turn it off... I don't know
99 * what will happen if new interrupts (that the PROM console doesn't
100 * want) are turned on. I'll burn that bridge when I come to it.
101 */
102 #define BROKEN_PROM_CONSOLE
103
104 /*
105 * Private functions and variables.
106 */
107
108 bus_space_tag_t sio_iot;
109 pci_chipset_tag_t sio_pc;
110 bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2, sio_ioh_elcr;
111
112 #define ICU_LEN 16 /* number of ISA IRQs */
113
114 static struct alpha_shared_intr *sio_intr;
115 #ifdef EVCNT_COUNTERS
116 struct evcnt sio_intr_evcnt;
117 #endif
118
119 #ifndef STRAY_MAX
120 #ifdef BROKEN_PROM_CONSOLE
121 /*
122 * If prom console is broken, because initial interrupt settings
123 * must be kept, there's no way to escape stray interrupts.
124 */
125 #define STRAY_MAX 0
126 #else
127 #define STRAY_MAX 5
128 #endif
129 #endif
130
131 #ifdef BROKEN_PROM_CONSOLE
132 /*
133 * If prom console is broken, must remember the initial interrupt
134 * settings and enforce them. WHEE!
135 */
136 u_int8_t initial_ocw1[2];
137 u_int8_t initial_elcr[2];
138 #define INITIALLY_ENABLED(irq) \
139 ((initial_ocw1[(irq) / 8] & (1 << ((irq) % 8))) == 0)
140 #define INITIALLY_LEVEL_TRIGGERED(irq) \
141 ((initial_elcr[(irq) / 8] & (1 << ((irq) % 8))) != 0)
142 #else
143 #define INITIALLY_ENABLED(irq) ((irq) == 2 ? 1 : 0)
144 #define INITIALLY_LEVEL_TRIGGERED(irq) 0
145 #endif
146
147 void sio_setirqstat __P((int, int, int));
148
149 u_int8_t (*sio_read_elcr) __P((int));
150 void (*sio_write_elcr) __P((int, u_int8_t));
151
152 /******************** i82378 SIO ELCR functions ********************/
153
154 int i82378_setup_elcr __P((void));
155 u_int8_t i82378_read_elcr __P((int));
156 void i82378_write_elcr __P((int, u_int8_t));
157
158 int
159 i82378_setup_elcr()
160 {
161 int rv;
162
163 /*
164 * We could probe configuration space to see that there's
165 * actually an SIO present, but we are using this as a
166 * fall-back in case nothing else matches.
167 */
168
169 rv = bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr);
170
171 if (rv == 0) {
172 sio_read_elcr = i82378_read_elcr;
173 sio_write_elcr = i82378_write_elcr;
174 }
175
176 return (rv);
177 }
178
179 u_int8_t
180 i82378_read_elcr(elcr)
181 int elcr;
182 {
183
184 return (bus_space_read_1(sio_iot, sio_ioh_elcr, elcr));
185 }
186
187 void
188 i82378_write_elcr(elcr, val)
189 int elcr;
190 u_int8_t val;
191 {
192
193 bus_space_write_1(sio_iot, sio_ioh_elcr, elcr, val);
194 }
195
196 /******************** Cypress CY82C693 ELCR functions ********************/
197
198 int cy82c693_setup_elcr __P((void));
199 u_int8_t cy82c693_read_elcr __P((int));
200 void cy82c693_write_elcr __P((int, u_int8_t));
201
202 int
203 cy82c693_setup_elcr()
204 {
205 int device, maxndevs;
206 pcitag_t tag;
207 pcireg_t id;
208
209 /*
210 * Search PCI configuration space for a Cypress CY82C693.
211 *
212 * Note we can make some assumptions about our bus number
213 * here, because:
214 *
215 * (1) there can be at most one ISA/EISA bridge per PCI bus, and
216 *
217 * (2) any ISA/EISA bridges must be attached to primary PCI
218 * busses (i.e. bus zero).
219 */
220
221 maxndevs = pci_bus_maxdevs(sio_pc, 0);
222
223 for (device = 0; device < maxndevs; device++) {
224 tag = pci_make_tag(sio_pc, 0, device, 0);
225 id = pci_conf_read(sio_pc, tag, PCI_ID_REG);
226
227 /* Invalid vendor ID value? */
228 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
229 continue;
230 /* XXX Not invalid, but we've done this ~forever. */
231 if (PCI_VENDOR(id) == 0)
232 continue;
233
234 if (PCI_VENDOR(id) != PCI_VENDOR_CONTAQ ||
235 PCI_PRODUCT(id) != PCI_PRODUCT_CONTAQ_82C693)
236 continue;
237
238 /*
239 * Found one!
240 */
241
242 #if 0
243 printf("cy82c693_setup_elcr: found 82C693 at device %d\n",
244 device);
245 #endif
246
247 /*
248 * The CY82C693's ELCR registers are accessed indirectly
249 * via (IO_ICU1 + 2) (address) and (IO_ICU1 + 3) (data).
250 */
251 sio_ioh_elcr = sio_ioh_icu1;
252
253 sio_read_elcr = cy82c693_read_elcr;
254 sio_write_elcr = cy82c693_write_elcr;
255
256 return (0);
257 }
258
259 /*
260 * Didn't find a CY82C693.
261 */
262 return (ENODEV);
263 }
264
265 u_int8_t
266 cy82c693_read_elcr(elcr)
267 int elcr;
268 {
269
270 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
271 return (bus_space_read_1(sio_iot, sio_ioh_elcr, 0x03));
272 }
273
274 void
275 cy82c693_write_elcr(elcr, val)
276 int elcr;
277 u_int8_t val;
278 {
279
280 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
281 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x03, val);
282 }
283
284 /******************** ELCR access function configuration ********************/
285
286 /*
287 * Put the Intel SIO at the end, so we fall back on it if we don't
288 * find anything else. If any of the non-Intel functions find a
289 * matching device, but are unable to map it for whatever reason,
290 * they should panic.
291 */
292
293 int (*sio_elcr_setup_funcs[]) __P((void)) = {
294 cy82c693_setup_elcr,
295 i82378_setup_elcr,
296 NULL,
297 };
298
299 /******************** Shared SIO/Cypress functions ********************/
300
301 void
302 sio_setirqstat(irq, enabled, type)
303 int irq, enabled;
304 int type;
305 {
306 u_int8_t ocw1[2], elcr[2];
307 int icu, bit;
308
309 #if 0
310 printf("sio_setirqstat: irq %d: %s, %s\n", irq,
311 enabled ? "enabled" : "disabled", isa_intr_typename(type));
312 #endif
313
314 icu = irq / 8;
315 bit = irq % 8;
316
317 ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
318 ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
319 elcr[0] = (*sio_read_elcr)(0); /* XXX */
320 elcr[1] = (*sio_read_elcr)(1); /* XXX */
321
322 /*
323 * interrupt enable: set bit to mask (disable) interrupt.
324 */
325 if (enabled)
326 ocw1[icu] &= ~(1 << bit);
327 else
328 ocw1[icu] |= 1 << bit;
329
330 /*
331 * interrupt type select: set bit to get level-triggered.
332 */
333 if (type == IST_LEVEL)
334 elcr[icu] |= 1 << bit;
335 else
336 elcr[icu] &= ~(1 << bit);
337
338 #ifdef not_here
339 /* see the init function... */
340 ocw1[0] &= ~0x04; /* always enable IRQ2 on first PIC */
341 elcr[0] &= ~0x07; /* IRQ[0-2] must be edge-triggered */
342 elcr[1] &= ~0x21; /* IRQ[13,8] must be edge-triggered */
343 #endif
344
345 #ifdef BROKEN_PROM_CONSOLE
346 /*
347 * make sure that the initially clear bits (unmasked interrupts)
348 * are never set, and that the initially-level-triggered
349 * intrrupts always remain level-triggered, to keep the prom happy.
350 */
351 if ((ocw1[0] & ~initial_ocw1[0]) != 0 ||
352 (ocw1[1] & ~initial_ocw1[1]) != 0 ||
353 (elcr[0] & initial_elcr[0]) != initial_elcr[0] ||
354 (elcr[1] & initial_elcr[1]) != initial_elcr[1]) {
355 printf("sio_sis: initial: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
356 initial_ocw1[0], initial_ocw1[1],
357 initial_elcr[0], initial_elcr[1]);
358 printf(" current: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
359 ocw1[0], ocw1[1], elcr[0], elcr[1]);
360 panic("sio_setirqstat: hosed");
361 }
362 #endif
363
364 bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
365 bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
366 (*sio_write_elcr)(0, elcr[0]); /* XXX */
367 (*sio_write_elcr)(1, elcr[1]); /* XXX */
368 }
369
370 void
371 sio_intr_setup(pc, iot)
372 pci_chipset_tag_t pc;
373 bus_space_tag_t iot;
374 {
375 int i;
376
377 sio_iot = iot;
378 sio_pc = pc;
379
380 if (bus_space_map(sio_iot, IO_ICU1, IO_ICUSIZE, 0, &sio_ioh_icu1) ||
381 bus_space_map(sio_iot, IO_ICU2, IO_ICUSIZE, 0, &sio_ioh_icu2))
382 panic("sio_intr_setup: can't map ICU I/O ports");
383
384 for (i = 0; sio_elcr_setup_funcs[i] != NULL; i++)
385 if ((*sio_elcr_setup_funcs[i])() == 0)
386 break;
387 if (sio_elcr_setup_funcs[i] == NULL)
388 panic("sio_intr_setup: can't map ELCR");
389
390 #ifdef BROKEN_PROM_CONSOLE
391 /*
392 * Remember the initial values, because the prom is stupid.
393 */
394 initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
395 initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
396 initial_elcr[0] = (*sio_read_elcr)(0); /* XXX */
397 initial_elcr[1] = (*sio_read_elcr)(1); /* XXX */
398 #if 0
399 printf("initial_ocw1[0] = 0x%x\n", initial_ocw1[0]);
400 printf("initial_ocw1[1] = 0x%x\n", initial_ocw1[1]);
401 printf("initial_elcr[0] = 0x%x\n", initial_elcr[0]);
402 printf("initial_elcr[1] = 0x%x\n", initial_elcr[1]);
403 #endif
404 #endif
405
406 sio_intr = alpha_shared_intr_alloc(ICU_LEN);
407
408 /*
409 * set up initial values for interrupt enables.
410 */
411 for (i = 0; i < ICU_LEN; i++) {
412 alpha_shared_intr_set_maxstrays(sio_intr, i, STRAY_MAX);
413
414 switch (i) {
415 case 0:
416 case 1:
417 case 8:
418 case 13:
419 /*
420 * IRQs 0, 1, 8, and 13 must always be
421 * edge-triggered.
422 */
423 if (INITIALLY_LEVEL_TRIGGERED(i))
424 printf("sio_intr_setup: %d LT!\n", i);
425 sio_setirqstat(i, INITIALLY_ENABLED(i), IST_EDGE);
426 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
427 IST_EDGE);
428 break;
429
430 case 2:
431 /*
432 * IRQ 2 must be edge-triggered, and should be
433 * enabled (otherwise IRQs 8-15 are ignored).
434 */
435 if (INITIALLY_LEVEL_TRIGGERED(i))
436 printf("sio_intr_setup: %d LT!\n", i);
437 if (!INITIALLY_ENABLED(i))
438 printf("sio_intr_setup: %d not enabled!\n", i);
439 sio_setirqstat(i, 1, IST_EDGE);
440 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
441 IST_UNUSABLE);
442 break;
443
444 default:
445 /*
446 * Otherwise, disable the IRQ and set its
447 * type to (effectively) "unknown."
448 */
449 sio_setirqstat(i, INITIALLY_ENABLED(i),
450 INITIALLY_LEVEL_TRIGGERED(i) ? IST_LEVEL :
451 IST_NONE);
452 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
453 INITIALLY_LEVEL_TRIGGERED(i) ? IST_LEVEL :
454 IST_NONE);
455 break;
456 }
457 }
458 }
459
460 const char *
461 sio_intr_string(v, irq)
462 void *v;
463 int irq;
464 {
465 static char irqstr[12]; /* 8 + 2 + NULL + sanity */
466
467 if (irq == 0 || irq >= ICU_LEN || irq == 2)
468 panic("sio_intr_string: bogus isa irq 0x%x\n", irq);
469
470 sprintf(irqstr, "isa irq %d", irq);
471 return (irqstr);
472 }
473
474 void *
475 sio_intr_establish(v, irq, type, level, fn, arg)
476 void *v, *arg;
477 int irq;
478 int type;
479 int level;
480 int (*fn)(void *);
481 {
482 void *cookie;
483
484 if (irq > ICU_LEN || type == IST_NONE)
485 panic("sio_intr_establish: bogus irq or type");
486
487 cookie = alpha_shared_intr_establish(sio_intr, irq, type, level, fn,
488 arg, "isa irq");
489
490 if (cookie)
491 sio_setirqstat(irq, alpha_shared_intr_isactive(sio_intr, irq),
492 alpha_shared_intr_get_sharetype(sio_intr, irq));
493
494 return (cookie);
495 }
496
497 void
498 sio_intr_disestablish(v, cookie)
499 void *v;
500 void *cookie;
501 {
502
503 printf("sio_intr_disestablish(%p)\n", cookie);
504 /* XXX */
505
506 /* XXX NEVER ALLOW AN INITIALLY-ENABLED INTERRUPT TO BE DISABLED */
507 /* XXX NEVER ALLOW AN INITIALLY-LT INTERRUPT TO BECOME UNTYPED */
508 }
509
510 void
511 sio_iointr(framep, vec)
512 void *framep;
513 unsigned long vec;
514 {
515 int irq;
516
517 irq = (vec - 0x800) >> 4;
518 #ifdef DIAGNOSTIC
519 if (irq > ICU_LEN || irq < 0)
520 panic("sio_iointr: irq out of range (%d)", irq);
521 #endif
522
523 #ifdef EVCNT_COUNTERS
524 sio_intr_evcnt.ev_count++;
525 #else
526 #ifdef DEBUG
527 if (ICU_LEN != INTRCNT_ISA_IRQ_LEN)
528 panic("sio interrupt counter sizes inconsistent");
529 #endif
530 intrcnt[INTRCNT_ISA_IRQ + irq]++;
531 #endif
532
533 if (!alpha_shared_intr_dispatch(sio_intr, irq))
534 alpha_shared_intr_stray(sio_intr, irq, "isa irq");
535
536 /*
537 * Some versions of the machines which use the SIO
538 * (or is it some PALcode revisions on those machines?)
539 * require the non-specific EOI to be fed to the PIC(s)
540 * by the interrupt handler.
541 */
542 if (irq > 7)
543 bus_space_write_1(sio_iot,
544 sio_ioh_icu2, 0, 0x20 | (irq & 0x07)); /* XXX */
545 bus_space_write_1(sio_iot,
546 sio_ioh_icu1, 0, 0x20 | (irq > 7 ? 2 : irq)); /* XXX */
547 }
548