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