sio_pic.c revision 1.27 1 /* $NetBSD: sio_pic.c,v 1.27 2000/06/05 21:47:29 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.27 2000/06/05 21:47:29 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 #include "sio.h"
89
90 /*
91 * To add to the long history of wonderful PROM console traits,
92 * AlphaStation PROMs don't reset themselves completely on boot!
93 * Therefore, if an interrupt was turned on when the kernel was
94 * started, we're not going to EVER turn it off... I don't know
95 * what will happen if new interrupts (that the PROM console doesn't
96 * want) are turned on. I'll burn that bridge when I come to it.
97 */
98 #define BROKEN_PROM_CONSOLE
99
100 /*
101 * Private functions and variables.
102 */
103
104 bus_space_tag_t sio_iot;
105 pci_chipset_tag_t sio_pc;
106 bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2, sio_ioh_elcr;
107
108 #define ICU_LEN 16 /* number of ISA IRQs */
109
110 static struct alpha_shared_intr *sio_intr;
111
112 #ifndef STRAY_MAX
113 #define STRAY_MAX 5
114 #endif
115
116 #ifdef BROKEN_PROM_CONSOLE
117 /*
118 * If prom console is broken, must remember the initial interrupt
119 * settings and enforce them. WHEE!
120 */
121 u_int8_t initial_ocw1[2];
122 u_int8_t initial_elcr[2];
123 #endif
124
125 void sio_setirqstat __P((int, int, int));
126
127 u_int8_t (*sio_read_elcr) __P((int));
128 void (*sio_write_elcr) __P((int, u_int8_t));
129 static void specific_eoi __P((int));
130 #ifdef BROKEN_PROM_CONSOLE
131 void sio_intr_shutdown __P((void *));
132 #endif
133
134 /******************** i82378 SIO ELCR functions ********************/
135
136 int i82378_setup_elcr __P((void));
137 u_int8_t i82378_read_elcr __P((int));
138 void i82378_write_elcr __P((int, u_int8_t));
139
140 int
141 i82378_setup_elcr()
142 {
143 int rv;
144
145 /*
146 * We could probe configuration space to see that there's
147 * actually an SIO present, but we are using this as a
148 * fall-back in case nothing else matches.
149 */
150
151 rv = bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr);
152
153 if (rv == 0) {
154 sio_read_elcr = i82378_read_elcr;
155 sio_write_elcr = i82378_write_elcr;
156 }
157
158 return (rv);
159 }
160
161 u_int8_t
162 i82378_read_elcr(elcr)
163 int elcr;
164 {
165
166 return (bus_space_read_1(sio_iot, sio_ioh_elcr, elcr));
167 }
168
169 void
170 i82378_write_elcr(elcr, val)
171 int elcr;
172 u_int8_t val;
173 {
174
175 bus_space_write_1(sio_iot, sio_ioh_elcr, elcr, val);
176 }
177
178 /******************** Cypress CY82C693 ELCR functions ********************/
179
180 int cy82c693_setup_elcr __P((void));
181 u_int8_t cy82c693_read_elcr __P((int));
182 void cy82c693_write_elcr __P((int, u_int8_t));
183
184 int
185 cy82c693_setup_elcr()
186 {
187 int device, maxndevs;
188 pcitag_t tag;
189 pcireg_t id;
190
191 /*
192 * Search PCI configuration space for a Cypress CY82C693.
193 *
194 * Note we can make some assumptions about our bus number
195 * here, because:
196 *
197 * (1) there can be at most one ISA/EISA bridge per PCI bus, and
198 *
199 * (2) any ISA/EISA bridges must be attached to primary PCI
200 * busses (i.e. bus zero).
201 */
202
203 maxndevs = pci_bus_maxdevs(sio_pc, 0);
204
205 for (device = 0; device < maxndevs; device++) {
206 tag = pci_make_tag(sio_pc, 0, device, 0);
207 id = pci_conf_read(sio_pc, tag, PCI_ID_REG);
208
209 /* Invalid vendor ID value? */
210 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
211 continue;
212 /* XXX Not invalid, but we've done this ~forever. */
213 if (PCI_VENDOR(id) == 0)
214 continue;
215
216 if (PCI_VENDOR(id) != PCI_VENDOR_CONTAQ ||
217 PCI_PRODUCT(id) != PCI_PRODUCT_CONTAQ_82C693)
218 continue;
219
220 /*
221 * Found one!
222 */
223
224 #if 0
225 printf("cy82c693_setup_elcr: found 82C693 at device %d\n",
226 device);
227 #endif
228
229 /*
230 * The CY82C693's ELCR registers are accessed indirectly
231 * via (IO_ICU1 + 2) (address) and (IO_ICU1 + 3) (data).
232 */
233 sio_ioh_elcr = sio_ioh_icu1;
234
235 sio_read_elcr = cy82c693_read_elcr;
236 sio_write_elcr = cy82c693_write_elcr;
237
238 return (0);
239 }
240
241 /*
242 * Didn't find a CY82C693.
243 */
244 return (ENODEV);
245 }
246
247 u_int8_t
248 cy82c693_read_elcr(elcr)
249 int elcr;
250 {
251
252 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
253 return (bus_space_read_1(sio_iot, sio_ioh_elcr, 0x03));
254 }
255
256 void
257 cy82c693_write_elcr(elcr, val)
258 int elcr;
259 u_int8_t val;
260 {
261
262 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x02, 0x03 + elcr);
263 bus_space_write_1(sio_iot, sio_ioh_elcr, 0x03, val);
264 }
265
266 /******************** ELCR access function configuration ********************/
267
268 /*
269 * Put the Intel SIO at the end, so we fall back on it if we don't
270 * find anything else. If any of the non-Intel functions find a
271 * matching device, but are unable to map it for whatever reason,
272 * they should panic.
273 */
274
275 int (*sio_elcr_setup_funcs[]) __P((void)) = {
276 cy82c693_setup_elcr,
277 i82378_setup_elcr,
278 NULL,
279 };
280
281 /******************** Shared SIO/Cypress functions ********************/
282
283 void
284 sio_setirqstat(irq, enabled, type)
285 int irq, enabled;
286 int type;
287 {
288 u_int8_t ocw1[2], elcr[2];
289 int icu, bit;
290
291 #if 0
292 printf("sio_setirqstat: irq %d: %s, %s\n", irq,
293 enabled ? "enabled" : "disabled", isa_intr_typename(type));
294 #endif
295
296 icu = irq / 8;
297 bit = irq % 8;
298
299 ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
300 ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
301 elcr[0] = (*sio_read_elcr)(0); /* XXX */
302 elcr[1] = (*sio_read_elcr)(1); /* XXX */
303
304 /*
305 * interrupt enable: set bit to mask (disable) interrupt.
306 */
307 if (enabled)
308 ocw1[icu] &= ~(1 << bit);
309 else
310 ocw1[icu] |= 1 << bit;
311
312 /*
313 * interrupt type select: set bit to get level-triggered.
314 */
315 if (type == IST_LEVEL)
316 elcr[icu] |= 1 << bit;
317 else
318 elcr[icu] &= ~(1 << bit);
319
320 #ifdef not_here
321 /* see the init function... */
322 ocw1[0] &= ~0x04; /* always enable IRQ2 on first PIC */
323 elcr[0] &= ~0x07; /* IRQ[0-2] must be edge-triggered */
324 elcr[1] &= ~0x21; /* IRQ[13,8] must be edge-triggered */
325 #endif
326
327 bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
328 bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
329 (*sio_write_elcr)(0, elcr[0]); /* XXX */
330 (*sio_write_elcr)(1, elcr[1]); /* XXX */
331 }
332
333 void
334 sio_intr_setup(pc, iot)
335 pci_chipset_tag_t pc;
336 bus_space_tag_t iot;
337 {
338 char *cp;
339 int i;
340
341 sio_iot = iot;
342 sio_pc = pc;
343
344 if (bus_space_map(sio_iot, IO_ICU1, IO_ICUSIZE, 0, &sio_ioh_icu1) ||
345 bus_space_map(sio_iot, IO_ICU2, IO_ICUSIZE, 0, &sio_ioh_icu2))
346 panic("sio_intr_setup: can't map ICU I/O ports");
347
348 for (i = 0; sio_elcr_setup_funcs[i] != NULL; i++)
349 if ((*sio_elcr_setup_funcs[i])() == 0)
350 break;
351 if (sio_elcr_setup_funcs[i] == NULL)
352 panic("sio_intr_setup: can't map ELCR");
353
354 #ifdef BROKEN_PROM_CONSOLE
355 /*
356 * Remember the initial values, so we can restore them later.
357 */
358 initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
359 initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
360 initial_elcr[0] = (*sio_read_elcr)(0); /* XXX */
361 initial_elcr[1] = (*sio_read_elcr)(1); /* XXX */
362 shutdownhook_establish(sio_intr_shutdown, 0);
363 #endif
364
365 sio_intr = alpha_shared_intr_alloc(ICU_LEN, 8);
366
367 /*
368 * set up initial values for interrupt enables.
369 */
370 for (i = 0; i < ICU_LEN; i++) {
371 alpha_shared_intr_set_maxstrays(sio_intr, i, STRAY_MAX);
372
373 cp = alpha_shared_intr_string(sio_intr, i);
374 sprintf(cp, "irq %d", i);
375 evcnt_attach_dynamic(alpha_shared_intr_evcnt(sio_intr, i),
376 EVCNT_TYPE_INTR, NULL, "isa", cp);
377
378 switch (i) {
379 case 0:
380 case 1:
381 case 8:
382 case 13:
383 /*
384 * IRQs 0, 1, 8, and 13 must always be
385 * edge-triggered.
386 */
387 sio_setirqstat(i, 0, IST_EDGE);
388 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
389 IST_EDGE);
390 specific_eoi(i);
391 break;
392
393 case 2:
394 /*
395 * IRQ 2 must be edge-triggered, and should be
396 * enabled (otherwise IRQs 8-15 are ignored).
397 */
398 sio_setirqstat(i, 1, IST_EDGE);
399 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
400 IST_UNUSABLE);
401 break;
402
403 default:
404 /*
405 * Otherwise, disable the IRQ and set its
406 * type to (effectively) "unknown."
407 */
408 sio_setirqstat(i, 0, IST_NONE);
409 alpha_shared_intr_set_dfltsharetype(sio_intr, i,
410 IST_NONE);
411 specific_eoi(i);
412 break;
413 }
414 }
415 }
416
417 #ifdef BROKEN_PROM_CONSOLE
418 void
419 sio_intr_shutdown(arg)
420 void *arg;
421 {
422 /*
423 * Restore the initial values, to make the PROM happy.
424 */
425 bus_space_write_1(sio_iot, sio_ioh_icu1, 1, initial_ocw1[0]);
426 bus_space_write_1(sio_iot, sio_ioh_icu2, 1, initial_ocw1[1]);
427 (*sio_write_elcr)(0, initial_elcr[0]); /* XXX */
428 (*sio_write_elcr)(1, initial_elcr[1]); /* XXX */
429 }
430 #endif
431
432 const char *
433 sio_intr_string(v, irq)
434 void *v;
435 int irq;
436 {
437 static char irqstr[12]; /* 8 + 2 + NULL + sanity */
438
439 if (irq == 0 || irq >= ICU_LEN || irq == 2)
440 panic("sio_intr_string: bogus isa irq 0x%x\n", irq);
441
442 sprintf(irqstr, "isa irq %d", irq);
443 return (irqstr);
444 }
445
446 const struct evcnt *
447 sio_intr_evcnt(v, irq)
448 void *v;
449 int irq;
450 {
451
452 if (irq == 0 || irq >= ICU_LEN || irq == 2)
453 panic("sio_intr_evcnt: bogus isa irq 0x%x\n", irq);
454
455 return (alpha_shared_intr_evcnt(sio_intr, irq));
456 }
457
458 void *
459 sio_intr_establish(v, irq, type, level, fn, arg)
460 void *v, *arg;
461 int irq;
462 int type;
463 int level;
464 int (*fn)(void *);
465 {
466 void *cookie;
467
468 if (irq > ICU_LEN || type == IST_NONE)
469 panic("sio_intr_establish: bogus irq or type");
470
471 cookie = alpha_shared_intr_establish(sio_intr, irq, type, level, fn,
472 arg, "isa irq");
473
474 if (cookie)
475 sio_setirqstat(irq, alpha_shared_intr_isactive(sio_intr, irq),
476 alpha_shared_intr_get_sharetype(sio_intr, irq));
477
478 return (cookie);
479 }
480
481 void
482 sio_intr_disestablish(v, cookie)
483 void *v;
484 void *cookie;
485 {
486 struct alpha_shared_intrhand *ih = cookie;
487 int s, ist, irq = ih->ih_num;
488
489 s = splhigh();
490
491 /* Remove it from the link. */
492 alpha_shared_intr_disestablish(sio_intr, cookie, "isa irq");
493
494 /*
495 * Decide if we should disable the interrupt. We must ensure
496 * that:
497 *
498 * - An initially-enabled interrupt is never disabled.
499 * - An initially-LT interrupt is never untyped.
500 */
501 if (alpha_shared_intr_isactive(sio_intr, irq) == 0) {
502 /*
503 * IRQs 0, 1, 8, and 13 must always be edge-triggered
504 * (see setup).
505 */
506 switch (irq) {
507 case 0:
508 case 1:
509 case 8:
510 case 13:
511 /*
512 * If the interrupt was initially level-triggered
513 * a warning was printed in setup.
514 */
515 ist = IST_EDGE;
516 break;
517
518 default:
519 ist = IST_NONE;
520 break;
521 }
522 sio_setirqstat(irq, 0, ist);
523 alpha_shared_intr_set_dfltsharetype(sio_intr, irq, ist);
524 }
525
526 splx(s);
527 }
528
529 void
530 sio_iointr(framep, vec)
531 void *framep;
532 unsigned long vec;
533 {
534 int irq;
535
536 irq = (vec - 0x800) >> 4;
537 #ifdef DIAGNOSTIC
538 if (irq > ICU_LEN || irq < 0)
539 panic("sio_iointr: irq out of range (%d)", irq);
540 #endif
541
542 if (!alpha_shared_intr_dispatch(sio_intr, irq))
543 alpha_shared_intr_stray(sio_intr, irq, "isa irq");
544
545 /*
546 * Some versions of the machines which use the SIO
547 * (or is it some PALcode revisions on those machines?)
548 * require the non-specific EOI to be fed to the PIC(s)
549 * by the interrupt handler.
550 */
551 specific_eoi(irq);
552 }
553
554 #define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
555
556 int
557 sio_intr_alloc(v, mask, type, irq)
558 void *v;
559 int mask;
560 int type;
561 int *irq;
562 {
563 int i, tmp, bestirq, count;
564 struct alpha_shared_intrhand **p, *q;
565
566 if (type == IST_NONE)
567 panic("intr_alloc: bogus type");
568
569 bestirq = -1;
570 count = -1;
571
572 /* some interrupts should never be dynamically allocated */
573 mask &= 0xdef8;
574
575 /*
576 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
577 * the right answer is to do "breadth-first" searching of devices.
578 */
579 mask &= 0xefbf;
580
581 for (i = 0; i < ICU_LEN; i++) {
582 if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
583 continue;
584
585 switch(sio_intr[i].intr_sharetype) {
586 case IST_NONE:
587 /*
588 * if nothing's using the irq, just return it
589 */
590 *irq = i;
591 return (0);
592
593 case IST_EDGE:
594 case IST_LEVEL:
595 if (type != sio_intr[i].intr_sharetype)
596 continue;
597 /*
598 * if the irq is shareable, count the number of other
599 * handlers, and if it's smaller than the last irq like
600 * this, remember it
601 *
602 * XXX We should probably also consider the
603 * interrupt level and stick IPL_TTY with other
604 * IPL_TTY, etc.
605 */
606 for (p = &TAILQ_FIRST(&sio_intr[i].intr_q), tmp = 0;
607 (q = *p) != NULL; p = &TAILQ_NEXT(q, ih_q), tmp++)
608 ;
609 if ((bestirq == -1) || (count > tmp)) {
610 bestirq = i;
611 count = tmp;
612 }
613 break;
614
615 case IST_PULSE:
616 /* this just isn't shareable */
617 continue;
618 }
619 }
620
621 if (bestirq == -1)
622 return (1);
623
624 *irq = bestirq;
625
626 return (0);
627 }
628
629 static void
630 specific_eoi(irq)
631 int irq;
632 {
633 if (irq > 7)
634 bus_space_write_1(sio_iot,
635 sio_ioh_icu2, 0, 0x20 | (irq & 0x07)); /* XXX */
636 bus_space_write_1(sio_iot, sio_ioh_icu1, 0, 0x20 | (irq > 7 ? 2 : irq));
637 }
638