Home | History | Annotate | Line # | Download | only in pci
sio_pic.c revision 1.31.6.2
      1  1.31.6.1     skrll /* $NetBSD: sio_pic.c,v 1.31.6.2 2004/09/18 14:31:13 skrll Exp $ */
      2      1.20   thorpej 
      3      1.20   thorpej /*-
      4      1.28   thorpej  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5      1.20   thorpej  * All rights reserved.
      6      1.20   thorpej  *
      7      1.20   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.20   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.20   thorpej  * NASA Ames Research Center.
     10      1.20   thorpej  *
     11      1.20   thorpej  * Redistribution and use in source and binary forms, with or without
     12      1.20   thorpej  * modification, are permitted provided that the following conditions
     13      1.20   thorpej  * are met:
     14      1.20   thorpej  * 1. Redistributions of source code must retain the above copyright
     15      1.20   thorpej  *    notice, this list of conditions and the following disclaimer.
     16      1.20   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.20   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18      1.20   thorpej  *    documentation and/or other materials provided with the distribution.
     19      1.20   thorpej  * 3. All advertising materials mentioning features or use of this software
     20      1.20   thorpej  *    must display the following acknowledgement:
     21      1.20   thorpej  *	This product includes software developed by the NetBSD
     22      1.20   thorpej  *	Foundation, Inc. and its contributors.
     23      1.20   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.20   thorpej  *    contributors may be used to endorse or promote products derived
     25      1.20   thorpej  *    from this software without specific prior written permission.
     26      1.20   thorpej  *
     27      1.20   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.20   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.20   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.20   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.20   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.20   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.20   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.20   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.20   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.20   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.20   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38      1.20   thorpej  */
     39       1.1       cgd 
     40       1.1       cgd /*
     41       1.6       cgd  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     42       1.1       cgd  * All rights reserved.
     43       1.1       cgd  *
     44       1.1       cgd  * Author: Chris G. Demetriou
     45       1.1       cgd  *
     46       1.1       cgd  * Permission to use, copy, modify and distribute this software and
     47       1.1       cgd  * its documentation is hereby granted, provided that both the copyright
     48       1.1       cgd  * notice and this permission notice appear in all copies of the
     49       1.1       cgd  * software, derivative works or modified versions, and any portions
     50       1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     51       1.1       cgd  *
     52       1.1       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     53       1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     54       1.1       cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     55       1.1       cgd  *
     56       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     57       1.1       cgd  *
     58       1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     59       1.1       cgd  *  School of Computer Science
     60       1.1       cgd  *  Carnegie Mellon University
     61       1.1       cgd  *  Pittsburgh PA 15213-3890
     62       1.1       cgd  *
     63       1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     64       1.1       cgd  * rights to redistribute these changes.
     65       1.1       cgd  */
     66      1.17       cgd 
     67      1.18       cgd #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     68      1.18       cgd 
     69  1.31.6.1     skrll __KERNEL_RCSID(0, "$NetBSD: sio_pic.c,v 1.31.6.2 2004/09/18 14:31:13 skrll Exp $");
     70       1.1       cgd 
     71       1.1       cgd #include <sys/param.h>
     72       1.1       cgd #include <sys/systm.h>
     73       1.1       cgd #include <sys/device.h>
     74       1.1       cgd #include <sys/malloc.h>
     75       1.1       cgd #include <sys/syslog.h>
     76       1.1       cgd 
     77       1.4       cgd #include <machine/intr.h>
     78       1.4       cgd #include <machine/bus.h>
     79       1.4       cgd 
     80      1.20   thorpej #include <dev/pci/pcireg.h>
     81      1.20   thorpej #include <dev/pci/pcivar.h>
     82      1.20   thorpej #include <dev/pci/pcidevs.h>
     83      1.20   thorpej 
     84      1.28   thorpej #include <dev/pci/cy82c693reg.h>
     85      1.28   thorpej #include <dev/pci/cy82c693var.h>
     86      1.28   thorpej 
     87       1.2       cgd #include <dev/isa/isareg.h>
     88       1.1       cgd #include <dev/isa/isavar.h>
     89       1.2       cgd #include <alpha/pci/siovar.h>
     90       1.2       cgd 
     91       1.2       cgd #include "sio.h"
     92       1.1       cgd 
     93       1.1       cgd /*
     94       1.1       cgd  * To add to the long history of wonderful PROM console traits,
     95       1.1       cgd  * AlphaStation PROMs don't reset themselves completely on boot!
     96       1.1       cgd  * Therefore, if an interrupt was turned on when the kernel was
     97       1.1       cgd  * started, we're not going to EVER turn it off...  I don't know
     98       1.1       cgd  * what will happen if new interrupts (that the PROM console doesn't
     99       1.1       cgd  * want) are turned on.  I'll burn that bridge when I come to it.
    100       1.1       cgd  */
    101       1.1       cgd #define	BROKEN_PROM_CONSOLE
    102       1.1       cgd 
    103       1.2       cgd /*
    104       1.2       cgd  * Private functions and variables.
    105       1.2       cgd  */
    106       1.4       cgd 
    107      1.14       cgd bus_space_tag_t sio_iot;
    108      1.20   thorpej pci_chipset_tag_t sio_pc;
    109      1.28   thorpej bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2;
    110       1.1       cgd 
    111       1.1       cgd #define	ICU_LEN		16		/* number of ISA IRQs */
    112       1.1       cgd 
    113      1.16       cgd static struct alpha_shared_intr *sio_intr;
    114       1.1       cgd 
    115       1.1       cgd #ifndef STRAY_MAX
    116       1.1       cgd #define	STRAY_MAX	5
    117       1.1       cgd #endif
    118       1.1       cgd 
    119       1.1       cgd #ifdef BROKEN_PROM_CONSOLE
    120       1.1       cgd /*
    121       1.1       cgd  * If prom console is broken, must remember the initial interrupt
    122       1.1       cgd  * settings and enforce them.  WHEE!
    123       1.1       cgd  */
    124       1.1       cgd u_int8_t initial_ocw1[2];
    125       1.1       cgd u_int8_t initial_elcr[2];
    126       1.1       cgd #endif
    127       1.1       cgd 
    128      1.20   thorpej void		sio_setirqstat __P((int, int, int));
    129      1.20   thorpej 
    130      1.20   thorpej u_int8_t	(*sio_read_elcr) __P((int));
    131      1.20   thorpej void		(*sio_write_elcr) __P((int, u_int8_t));
    132      1.24      ross static void	specific_eoi __P((int));
    133      1.25   mycroft #ifdef BROKEN_PROM_CONSOLE
    134      1.25   mycroft void		sio_intr_shutdown __P((void *));
    135      1.25   mycroft #endif
    136      1.20   thorpej 
    137      1.20   thorpej /******************** i82378 SIO ELCR functions ********************/
    138      1.20   thorpej 
    139      1.20   thorpej int		i82378_setup_elcr __P((void));
    140      1.20   thorpej u_int8_t	i82378_read_elcr __P((int));
    141      1.20   thorpej void		i82378_write_elcr __P((int, u_int8_t));
    142      1.20   thorpej 
    143      1.28   thorpej bus_space_handle_t sio_ioh_elcr;
    144      1.28   thorpej 
    145      1.20   thorpej int
    146      1.20   thorpej i82378_setup_elcr()
    147      1.20   thorpej {
    148      1.20   thorpej 	int rv;
    149      1.20   thorpej 
    150      1.20   thorpej 	/*
    151      1.20   thorpej 	 * We could probe configuration space to see that there's
    152      1.20   thorpej 	 * actually an SIO present, but we are using this as a
    153      1.20   thorpej 	 * fall-back in case nothing else matches.
    154      1.20   thorpej 	 */
    155      1.20   thorpej 
    156      1.20   thorpej 	rv = bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr);
    157      1.20   thorpej 
    158      1.20   thorpej 	if (rv == 0) {
    159      1.20   thorpej 		sio_read_elcr = i82378_read_elcr;
    160      1.20   thorpej 		sio_write_elcr = i82378_write_elcr;
    161      1.20   thorpej 	}
    162      1.20   thorpej 
    163      1.20   thorpej 	return (rv);
    164      1.20   thorpej }
    165      1.20   thorpej 
    166      1.20   thorpej u_int8_t
    167      1.20   thorpej i82378_read_elcr(elcr)
    168      1.20   thorpej 	int elcr;
    169      1.20   thorpej {
    170      1.20   thorpej 
    171      1.20   thorpej 	return (bus_space_read_1(sio_iot, sio_ioh_elcr, elcr));
    172      1.20   thorpej }
    173      1.20   thorpej 
    174      1.20   thorpej void
    175      1.20   thorpej i82378_write_elcr(elcr, val)
    176      1.20   thorpej 	int elcr;
    177      1.20   thorpej 	u_int8_t val;
    178      1.20   thorpej {
    179      1.20   thorpej 
    180      1.20   thorpej 	bus_space_write_1(sio_iot, sio_ioh_elcr, elcr, val);
    181      1.20   thorpej }
    182      1.20   thorpej 
    183      1.20   thorpej /******************** Cypress CY82C693 ELCR functions ********************/
    184      1.20   thorpej 
    185      1.20   thorpej int		cy82c693_setup_elcr __P((void));
    186      1.20   thorpej u_int8_t	cy82c693_read_elcr __P((int));
    187      1.20   thorpej void		cy82c693_write_elcr __P((int, u_int8_t));
    188      1.20   thorpej 
    189      1.28   thorpej const struct cy82c693_handle *sio_cy82c693_handle;
    190      1.28   thorpej 
    191      1.20   thorpej int
    192      1.20   thorpej cy82c693_setup_elcr()
    193      1.20   thorpej {
    194      1.20   thorpej 	int device, maxndevs;
    195      1.20   thorpej 	pcitag_t tag;
    196      1.20   thorpej 	pcireg_t id;
    197      1.20   thorpej 
    198      1.20   thorpej 	/*
    199      1.20   thorpej 	 * Search PCI configuration space for a Cypress CY82C693.
    200      1.20   thorpej 	 *
    201      1.20   thorpej 	 * Note we can make some assumptions about our bus number
    202      1.20   thorpej 	 * here, because:
    203      1.20   thorpej 	 *
    204      1.20   thorpej 	 *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
    205      1.20   thorpej 	 *
    206      1.20   thorpej 	 *	(2) any ISA/EISA bridges must be attached to primary PCI
    207      1.20   thorpej 	 *	    busses (i.e. bus zero).
    208      1.20   thorpej 	 */
    209      1.20   thorpej 
    210      1.20   thorpej 	maxndevs = pci_bus_maxdevs(sio_pc, 0);
    211      1.20   thorpej 
    212      1.20   thorpej 	for (device = 0; device < maxndevs; device++) {
    213      1.20   thorpej 		tag = pci_make_tag(sio_pc, 0, device, 0);
    214      1.20   thorpej 		id = pci_conf_read(sio_pc, tag, PCI_ID_REG);
    215      1.20   thorpej 
    216      1.20   thorpej 		/* Invalid vendor ID value? */
    217      1.20   thorpej 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    218      1.20   thorpej 			continue;
    219      1.20   thorpej 		/* XXX Not invalid, but we've done this ~forever. */
    220      1.20   thorpej 		if (PCI_VENDOR(id) == 0)
    221      1.20   thorpej 			continue;
    222      1.20   thorpej 
    223      1.20   thorpej 		if (PCI_VENDOR(id) != PCI_VENDOR_CONTAQ ||
    224      1.20   thorpej 		    PCI_PRODUCT(id) != PCI_PRODUCT_CONTAQ_82C693)
    225      1.20   thorpej 			continue;
    226      1.20   thorpej 
    227      1.20   thorpej 		/*
    228      1.20   thorpej 		 * Found one!
    229      1.20   thorpej 		 */
    230      1.20   thorpej 
    231      1.20   thorpej #if 0
    232      1.20   thorpej 		printf("cy82c693_setup_elcr: found 82C693 at device %d\n",
    233      1.20   thorpej 		    device);
    234      1.20   thorpej #endif
    235      1.20   thorpej 
    236      1.28   thorpej 		sio_cy82c693_handle = cy82c693_init(sio_iot);
    237      1.20   thorpej 		sio_read_elcr = cy82c693_read_elcr;
    238      1.20   thorpej 		sio_write_elcr = cy82c693_write_elcr;
    239      1.20   thorpej 
    240      1.20   thorpej 		return (0);
    241      1.20   thorpej 	}
    242      1.20   thorpej 
    243      1.20   thorpej 	/*
    244      1.20   thorpej 	 * Didn't find a CY82C693.
    245      1.20   thorpej 	 */
    246      1.20   thorpej 	return (ENODEV);
    247      1.20   thorpej }
    248      1.20   thorpej 
    249      1.20   thorpej u_int8_t
    250      1.20   thorpej cy82c693_read_elcr(elcr)
    251      1.20   thorpej 	int elcr;
    252      1.20   thorpej {
    253      1.20   thorpej 
    254      1.28   thorpej 	return (cy82c693_read(sio_cy82c693_handle, CONFIG_ELCR1 + elcr));
    255      1.20   thorpej }
    256      1.20   thorpej 
    257      1.20   thorpej void
    258      1.20   thorpej cy82c693_write_elcr(elcr, val)
    259      1.20   thorpej 	int elcr;
    260      1.20   thorpej 	u_int8_t val;
    261      1.20   thorpej {
    262      1.20   thorpej 
    263      1.28   thorpej 	cy82c693_write(sio_cy82c693_handle, CONFIG_ELCR1 + elcr, val);
    264      1.20   thorpej }
    265      1.20   thorpej 
    266      1.20   thorpej /******************** ELCR access function configuration ********************/
    267      1.20   thorpej 
    268      1.20   thorpej /*
    269      1.20   thorpej  * Put the Intel SIO at the end, so we fall back on it if we don't
    270      1.20   thorpej  * find anything else.  If any of the non-Intel functions find a
    271      1.20   thorpej  * matching device, but are unable to map it for whatever reason,
    272      1.20   thorpej  * they should panic.
    273      1.20   thorpej  */
    274      1.20   thorpej 
    275      1.20   thorpej int (*sio_elcr_setup_funcs[]) __P((void)) = {
    276      1.20   thorpej 	cy82c693_setup_elcr,
    277      1.20   thorpej 	i82378_setup_elcr,
    278      1.20   thorpej 	NULL,
    279      1.20   thorpej };
    280      1.20   thorpej 
    281      1.20   thorpej /******************** Shared SIO/Cypress functions ********************/
    282      1.15       cgd 
    283       1.1       cgd void
    284       1.1       cgd sio_setirqstat(irq, enabled, type)
    285       1.1       cgd 	int irq, enabled;
    286       1.3   mycroft 	int type;
    287       1.1       cgd {
    288       1.1       cgd 	u_int8_t ocw1[2], elcr[2];
    289       1.1       cgd 	int icu, bit;
    290       1.1       cgd 
    291       1.1       cgd #if 0
    292      1.13  christos 	printf("sio_setirqstat: irq %d: %s, %s\n", irq,
    293       1.1       cgd 	    enabled ? "enabled" : "disabled", isa_intr_typename(type));
    294       1.1       cgd #endif
    295       1.1       cgd 
    296       1.1       cgd 	icu = irq / 8;
    297       1.1       cgd 	bit = irq % 8;
    298       1.1       cgd 
    299      1.14       cgd 	ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
    300      1.14       cgd 	ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
    301      1.20   thorpej 	elcr[0] = (*sio_read_elcr)(0);				/* XXX */
    302      1.20   thorpej 	elcr[1] = (*sio_read_elcr)(1);				/* XXX */
    303       1.1       cgd 
    304       1.1       cgd 	/*
    305       1.1       cgd 	 * interrupt enable: set bit to mask (disable) interrupt.
    306       1.1       cgd 	 */
    307       1.1       cgd 	if (enabled)
    308       1.1       cgd 		ocw1[icu] &= ~(1 << bit);
    309       1.1       cgd 	else
    310       1.1       cgd 		ocw1[icu] |= 1 << bit;
    311       1.1       cgd 
    312       1.1       cgd 	/*
    313       1.1       cgd 	 * interrupt type select: set bit to get level-triggered.
    314       1.1       cgd 	 */
    315       1.3   mycroft 	if (type == IST_LEVEL)
    316       1.1       cgd 		elcr[icu] |= 1 << bit;
    317       1.1       cgd 	else
    318       1.1       cgd 		elcr[icu] &= ~(1 << bit);
    319       1.1       cgd 
    320       1.1       cgd #ifdef not_here
    321       1.1       cgd 	/* see the init function... */
    322       1.1       cgd 	ocw1[0] &= ~0x04;		/* always enable IRQ2 on first PIC */
    323       1.1       cgd 	elcr[0] &= ~0x07;		/* IRQ[0-2] must be edge-triggered */
    324       1.1       cgd 	elcr[1] &= ~0x21;		/* IRQ[13,8] must be edge-triggered */
    325       1.1       cgd #endif
    326       1.1       cgd 
    327      1.14       cgd 	bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
    328      1.14       cgd 	bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
    329      1.20   thorpej 	(*sio_write_elcr)(0, elcr[0]);				/* XXX */
    330      1.20   thorpej 	(*sio_write_elcr)(1, elcr[1]);				/* XXX */
    331       1.1       cgd }
    332       1.1       cgd 
    333       1.1       cgd void
    334      1.20   thorpej sio_intr_setup(pc, iot)
    335      1.20   thorpej 	pci_chipset_tag_t pc;
    336      1.14       cgd 	bus_space_tag_t iot;
    337       1.1       cgd {
    338      1.27   thorpej 	char *cp;
    339       1.1       cgd 	int i;
    340       1.1       cgd 
    341      1.14       cgd 	sio_iot = iot;
    342      1.20   thorpej 	sio_pc = pc;
    343       1.4       cgd 
    344      1.28   thorpej 	if (bus_space_map(sio_iot, IO_ICU1, 2, 0, &sio_ioh_icu1) ||
    345      1.28   thorpej 	    bus_space_map(sio_iot, IO_ICU2, 2, 0, &sio_ioh_icu2))
    346      1.20   thorpej 		panic("sio_intr_setup: can't map ICU I/O ports");
    347      1.20   thorpej 
    348      1.20   thorpej 	for (i = 0; sio_elcr_setup_funcs[i] != NULL; i++)
    349      1.20   thorpej 		if ((*sio_elcr_setup_funcs[i])() == 0)
    350      1.20   thorpej 			break;
    351      1.20   thorpej 	if (sio_elcr_setup_funcs[i] == NULL)
    352      1.20   thorpej 		panic("sio_intr_setup: can't map ELCR");
    353       1.2       cgd 
    354       1.1       cgd #ifdef BROKEN_PROM_CONSOLE
    355       1.1       cgd 	/*
    356      1.25   mycroft 	 * Remember the initial values, so we can restore them later.
    357       1.1       cgd 	 */
    358      1.14       cgd 	initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
    359      1.14       cgd 	initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
    360      1.20   thorpej 	initial_elcr[0] = (*sio_read_elcr)(0);			/* XXX */
    361      1.20   thorpej 	initial_elcr[1] = (*sio_read_elcr)(1);			/* XXX */
    362      1.25   mycroft 	shutdownhook_establish(sio_intr_shutdown, 0);
    363       1.1       cgd #endif
    364       1.1       cgd 
    365      1.27   thorpej 	sio_intr = alpha_shared_intr_alloc(ICU_LEN, 8);
    366      1.16       cgd 
    367       1.1       cgd 	/*
    368       1.1       cgd 	 * set up initial values for interrupt enables.
    369       1.1       cgd 	 */
    370       1.1       cgd 	for (i = 0; i < ICU_LEN; i++) {
    371      1.16       cgd 		alpha_shared_intr_set_maxstrays(sio_intr, i, STRAY_MAX);
    372      1.16       cgd 
    373      1.27   thorpej 		cp = alpha_shared_intr_string(sio_intr, i);
    374      1.27   thorpej 		sprintf(cp, "irq %d", i);
    375      1.27   thorpej 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(sio_intr, i),
    376      1.27   thorpej 		    EVCNT_TYPE_INTR, NULL, "isa", cp);
    377      1.27   thorpej 
    378       1.1       cgd 		switch (i) {
    379       1.1       cgd 		case 0:
    380       1.1       cgd 		case 1:
    381       1.1       cgd 		case 8:
    382       1.1       cgd 		case 13:
    383       1.1       cgd 			/*
    384       1.1       cgd 			 * IRQs 0, 1, 8, and 13 must always be
    385       1.1       cgd 			 * edge-triggered.
    386       1.1       cgd 			 */
    387  1.31.6.1     skrll 			sio_setirqstat(i, 0, IST_EDGE);
    388      1.16       cgd 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    389      1.16       cgd 			    IST_EDGE);
    390      1.24      ross 			specific_eoi(i);
    391       1.1       cgd 			break;
    392       1.1       cgd 
    393       1.1       cgd 		case 2:
    394       1.1       cgd 			/*
    395       1.1       cgd 			 * IRQ 2 must be edge-triggered, and should be
    396       1.1       cgd 			 * enabled (otherwise IRQs 8-15 are ignored).
    397       1.1       cgd 			 */
    398       1.3   mycroft 			sio_setirqstat(i, 1, IST_EDGE);
    399      1.16       cgd 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    400      1.16       cgd 			    IST_UNUSABLE);
    401       1.1       cgd 			break;
    402       1.1       cgd 
    403       1.1       cgd 		default:
    404       1.1       cgd 			/*
    405       1.1       cgd 			 * Otherwise, disable the IRQ and set its
    406       1.1       cgd 			 * type to (effectively) "unknown."
    407       1.1       cgd 			 */
    408  1.31.6.1     skrll 			sio_setirqstat(i, 0, IST_NONE);
    409      1.16       cgd 			alpha_shared_intr_set_dfltsharetype(sio_intr, i,
    410  1.31.6.1     skrll 			    IST_NONE);
    411      1.24      ross 			specific_eoi(i);
    412       1.1       cgd 			break;
    413       1.1       cgd 		}
    414       1.1       cgd 	}
    415       1.1       cgd }
    416       1.1       cgd 
    417      1.25   mycroft #ifdef BROKEN_PROM_CONSOLE
    418      1.25   mycroft void
    419      1.25   mycroft sio_intr_shutdown(arg)
    420      1.25   mycroft 	void *arg;
    421      1.25   mycroft {
    422      1.25   mycroft 	/*
    423      1.25   mycroft 	 * Restore the initial values, to make the PROM happy.
    424      1.25   mycroft 	 */
    425      1.25   mycroft 	bus_space_write_1(sio_iot, sio_ioh_icu1, 1, initial_ocw1[0]);
    426      1.25   mycroft 	bus_space_write_1(sio_iot, sio_ioh_icu2, 1, initial_ocw1[1]);
    427      1.25   mycroft 	(*sio_write_elcr)(0, initial_elcr[0]);			/* XXX */
    428      1.25   mycroft 	(*sio_write_elcr)(1, initial_elcr[1]);			/* XXX */
    429      1.25   mycroft }
    430      1.25   mycroft #endif
    431      1.25   mycroft 
    432       1.4       cgd const char *
    433       1.4       cgd sio_intr_string(v, irq)
    434       1.4       cgd 	void *v;
    435       1.4       cgd 	int irq;
    436       1.4       cgd {
    437       1.7       cgd 	static char irqstr[12];		/* 8 + 2 + NULL + sanity */
    438       1.4       cgd 
    439       1.4       cgd 	if (irq == 0 || irq >= ICU_LEN || irq == 2)
    440      1.31    provos 		panic("sio_intr_string: bogus isa irq 0x%x", irq);
    441       1.4       cgd 
    442      1.13  christos 	sprintf(irqstr, "isa irq %d", irq);
    443       1.4       cgd 	return (irqstr);
    444      1.26       cgd }
    445      1.26       cgd 
    446      1.26       cgd const struct evcnt *
    447      1.26       cgd sio_intr_evcnt(v, irq)
    448      1.26       cgd 	void *v;
    449      1.26       cgd 	int irq;
    450      1.26       cgd {
    451      1.26       cgd 
    452      1.27   thorpej 	if (irq == 0 || irq >= ICU_LEN || irq == 2)
    453      1.31    provos 		panic("sio_intr_evcnt: bogus isa irq 0x%x", irq);
    454      1.27   thorpej 
    455      1.27   thorpej 	return (alpha_shared_intr_evcnt(sio_intr, irq));
    456       1.4       cgd }
    457       1.4       cgd 
    458       1.1       cgd void *
    459      1.16       cgd sio_intr_establish(v, irq, type, level, fn, arg)
    460      1.16       cgd 	void *v, *arg;
    461       1.4       cgd         int irq;
    462       1.3   mycroft         int type;
    463       1.3   mycroft         int level;
    464      1.16       cgd         int (*fn)(void *);
    465       1.1       cgd {
    466      1.16       cgd 	void *cookie;
    467       1.1       cgd 
    468       1.3   mycroft 	if (irq > ICU_LEN || type == IST_NONE)
    469       1.1       cgd 		panic("sio_intr_establish: bogus irq or type");
    470       1.1       cgd 
    471      1.16       cgd 	cookie = alpha_shared_intr_establish(sio_intr, irq, type, level, fn,
    472      1.16       cgd 	    arg, "isa irq");
    473       1.1       cgd 
    474      1.30   thorpej 	if (cookie != NULL &&
    475      1.30   thorpej 	    alpha_shared_intr_firstactive(sio_intr, irq)) {
    476      1.30   thorpej 		scb_set(0x800 + SCB_IDXTOVEC(irq), sio_iointr, NULL);
    477      1.30   thorpej 		sio_setirqstat(irq, 1,
    478      1.16       cgd 		    alpha_shared_intr_get_sharetype(sio_intr, irq));
    479      1.30   thorpej 	}
    480       1.1       cgd 
    481      1.16       cgd 	return (cookie);
    482       1.1       cgd }
    483       1.1       cgd 
    484       1.1       cgd void
    485       1.4       cgd sio_intr_disestablish(v, cookie)
    486       1.4       cgd 	void *v;
    487       1.4       cgd 	void *cookie;
    488       1.1       cgd {
    489      1.22   thorpej 	struct alpha_shared_intrhand *ih = cookie;
    490      1.23   thorpej 	int s, ist, irq = ih->ih_num;
    491       1.1       cgd 
    492      1.22   thorpej 	s = splhigh();
    493       1.1       cgd 
    494      1.22   thorpej 	/* Remove it from the link. */
    495      1.22   thorpej 	alpha_shared_intr_disestablish(sio_intr, cookie, "isa irq");
    496      1.22   thorpej 
    497      1.22   thorpej 	/*
    498      1.22   thorpej 	 * Decide if we should disable the interrupt.  We must ensure
    499      1.22   thorpej 	 * that:
    500      1.22   thorpej 	 *
    501      1.22   thorpej 	 *	- An initially-enabled interrupt is never disabled.
    502      1.22   thorpej 	 *	- An initially-LT interrupt is never untyped.
    503      1.22   thorpej 	 */
    504      1.22   thorpej 	if (alpha_shared_intr_isactive(sio_intr, irq) == 0) {
    505      1.23   thorpej 		/*
    506      1.23   thorpej 		 * IRQs 0, 1, 8, and 13 must always be edge-triggered
    507      1.23   thorpej 		 * (see setup).
    508      1.23   thorpej 		 */
    509      1.23   thorpej 		switch (irq) {
    510      1.23   thorpej 		case 0:
    511      1.23   thorpej 		case 1:
    512      1.23   thorpej 		case 8:
    513      1.23   thorpej 		case 13:
    514      1.23   thorpej 			/*
    515      1.23   thorpej 			 * If the interrupt was initially level-triggered
    516      1.23   thorpej 			 * a warning was printed in setup.
    517      1.23   thorpej 			 */
    518      1.23   thorpej 			ist = IST_EDGE;
    519      1.23   thorpej 			break;
    520      1.23   thorpej 
    521      1.23   thorpej 		default:
    522  1.31.6.1     skrll 			ist = IST_NONE;
    523      1.23   thorpej 			break;
    524      1.23   thorpej 		}
    525  1.31.6.1     skrll 		sio_setirqstat(irq, 0, ist);
    526      1.22   thorpej 		alpha_shared_intr_set_dfltsharetype(sio_intr, irq, ist);
    527      1.30   thorpej 
    528      1.30   thorpej 		/* Release our SCB vector. */
    529      1.30   thorpej 		scb_free(0x800 + SCB_IDXTOVEC(irq));
    530      1.22   thorpej 	}
    531      1.22   thorpej 
    532      1.22   thorpej 	splx(s);
    533       1.1       cgd }
    534       1.1       cgd 
    535       1.1       cgd void
    536      1.30   thorpej sio_iointr(arg, vec)
    537      1.30   thorpej 	void *arg;
    538      1.10       cgd 	unsigned long vec;
    539       1.1       cgd {
    540      1.16       cgd 	int irq;
    541       1.1       cgd 
    542      1.30   thorpej 	irq = SCB_VECTOIDX(vec - 0x800);
    543      1.30   thorpej 
    544       1.1       cgd #ifdef DIAGNOSTIC
    545       1.1       cgd 	if (irq > ICU_LEN || irq < 0)
    546       1.1       cgd 		panic("sio_iointr: irq out of range (%d)", irq);
    547       1.2       cgd #endif
    548       1.2       cgd 
    549      1.16       cgd 	if (!alpha_shared_intr_dispatch(sio_intr, irq))
    550      1.16       cgd 		alpha_shared_intr_stray(sio_intr, irq, "isa irq");
    551       1.1       cgd 
    552       1.1       cgd 	/*
    553       1.1       cgd 	 * Some versions of the machines which use the SIO
    554       1.1       cgd 	 * (or is it some PALcode revisions on those machines?)
    555       1.1       cgd 	 * require the non-specific EOI to be fed to the PIC(s)
    556       1.1       cgd 	 * by the interrupt handler.
    557       1.1       cgd 	 */
    558      1.24      ross 	specific_eoi(irq);
    559      1.21      matt }
    560      1.21      matt 
    561      1.21      matt #define	LEGAL_IRQ(x)	((x) >= 0 && (x) < ICU_LEN && (x) != 2)
    562      1.21      matt 
    563      1.21      matt int
    564      1.21      matt sio_intr_alloc(v, mask, type, irq)
    565      1.21      matt 	void *v;
    566      1.21      matt 	int mask;
    567      1.21      matt 	int type;
    568      1.21      matt 	int *irq;
    569      1.21      matt {
    570      1.21      matt 	int i, tmp, bestirq, count;
    571      1.21      matt 	struct alpha_shared_intrhand **p, *q;
    572      1.21      matt 
    573      1.21      matt 	if (type == IST_NONE)
    574      1.21      matt 		panic("intr_alloc: bogus type");
    575      1.21      matt 
    576      1.21      matt 	bestirq = -1;
    577      1.21      matt 	count = -1;
    578      1.21      matt 
    579      1.21      matt 	/* some interrupts should never be dynamically allocated */
    580      1.21      matt 	mask &= 0xdef8;
    581      1.21      matt 
    582      1.21      matt 	/*
    583      1.21      matt 	 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
    584      1.21      matt 	 * the right answer is to do "breadth-first" searching of devices.
    585      1.21      matt 	 */
    586      1.21      matt 	mask &= 0xefbf;
    587      1.21      matt 
    588      1.21      matt 	for (i = 0; i < ICU_LEN; i++) {
    589      1.21      matt 		if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
    590      1.21      matt 			continue;
    591      1.21      matt 
    592      1.21      matt 		switch(sio_intr[i].intr_sharetype) {
    593      1.21      matt 		case IST_NONE:
    594      1.21      matt 			/*
    595      1.21      matt 			 * if nothing's using the irq, just return it
    596      1.21      matt 			 */
    597      1.21      matt 			*irq = i;
    598      1.21      matt 			return (0);
    599      1.21      matt 
    600      1.21      matt 		case IST_EDGE:
    601      1.21      matt 		case IST_LEVEL:
    602      1.21      matt 			if (type != sio_intr[i].intr_sharetype)
    603      1.21      matt 				continue;
    604      1.21      matt 			/*
    605      1.21      matt 			 * if the irq is shareable, count the number of other
    606      1.21      matt 			 * handlers, and if it's smaller than the last irq like
    607      1.21      matt 			 * this, remember it
    608      1.21      matt 			 *
    609      1.21      matt 			 * XXX We should probably also consider the
    610      1.21      matt 			 * interrupt level and stick IPL_TTY with other
    611      1.21      matt 			 * IPL_TTY, etc.
    612      1.21      matt 			 */
    613      1.21      matt 			for (p = &TAILQ_FIRST(&sio_intr[i].intr_q), tmp = 0;
    614      1.21      matt 			     (q = *p) != NULL; p = &TAILQ_NEXT(q, ih_q), tmp++)
    615      1.21      matt 				;
    616      1.21      matt 			if ((bestirq == -1) || (count > tmp)) {
    617      1.21      matt 				bestirq = i;
    618      1.21      matt 				count = tmp;
    619      1.21      matt 			}
    620      1.21      matt 			break;
    621      1.21      matt 
    622      1.21      matt 		case IST_PULSE:
    623      1.21      matt 			/* this just isn't shareable */
    624      1.21      matt 			continue;
    625      1.21      matt 		}
    626      1.21      matt 	}
    627      1.21      matt 
    628      1.21      matt 	if (bestirq == -1)
    629      1.21      matt 		return (1);
    630      1.21      matt 
    631      1.21      matt 	*irq = bestirq;
    632      1.21      matt 
    633      1.21      matt 	return (0);
    634      1.24      ross }
    635      1.24      ross 
    636      1.24      ross static void
    637      1.24      ross specific_eoi(irq)
    638      1.24      ross 	int irq;
    639      1.24      ross {
    640      1.24      ross 	if (irq > 7)
    641      1.24      ross 		bus_space_write_1(sio_iot,
    642  1.31.6.1     skrll 		    sio_ioh_icu2, 0, 0x60 | (irq & 0x07));	/* XXX */
    643  1.31.6.1     skrll 	bus_space_write_1(sio_iot, sio_ioh_icu1, 0, 0x60 | (irq > 7 ? 2 : irq));
    644       1.1       cgd }
    645