Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.16
      1        1.1      cgd /*-
      2   1.28.2.7  mycroft  * Copyright (c) 1993 Charles Hannum.
      3        1.1      cgd  * Copyright (c) 1991 The Regents of the University of California.
      4        1.1      cgd  * All rights reserved.
      5        1.1      cgd  *
      6        1.1      cgd  * This code is derived from software contributed to Berkeley by
      7        1.1      cgd  * William Jolitz.
      8        1.1      cgd  *
      9        1.1      cgd  * Redistribution and use in source and binary forms, with or without
     10        1.1      cgd  * modification, are permitted provided that the following conditions
     11        1.1      cgd  * are met:
     12        1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     13        1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     14        1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     16        1.1      cgd  *    documentation and/or other materials provided with the distribution.
     17        1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     18        1.1      cgd  *    must display the following acknowledgement:
     19        1.1      cgd  *	This product includes software developed by the University of
     20        1.1      cgd  *	California, Berkeley and its contributors.
     21        1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     22        1.1      cgd  *    may be used to endorse or promote products derived from this software
     23        1.1      cgd  *    without specific prior written permission.
     24        1.1      cgd  *
     25        1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26        1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27        1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28        1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29        1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30        1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31        1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32        1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33        1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34        1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35        1.1      cgd  * SUCH DAMAGE.
     36        1.1      cgd  *
     37       1.13      cgd  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     38  1.28.2.16  mycroft  *	$Id: isa.c,v 1.28.2.16 1993/10/18 09:30:37 mycroft Exp $
     39        1.1      cgd  */
     40        1.1      cgd 
     41        1.1      cgd /*
     42        1.1      cgd  * code to manage AT bus
     43        1.1      cgd  */
     44        1.1      cgd 
     45  1.28.2.15  mycroft #include <sys/param.h>
     46  1.28.2.15  mycroft #include <sys/systm.h>
     47  1.28.2.15  mycroft #include <sys/conf.h>
     48  1.28.2.15  mycroft #include <sys/file.h>
     49  1.28.2.15  mycroft #include <sys/syslog.h>
     50  1.28.2.15  mycroft #include <sys/device.h>
     51  1.28.2.15  mycroft #include <vm/vm.h>
     52  1.28.2.15  mycroft 
     53  1.28.2.15  mycroft #include <machine/cpu.h>
     54  1.28.2.15  mycroft #include <machine/pio.h>
     55  1.28.2.15  mycroft 
     56  1.28.2.15  mycroft #include <i386/isa/isa.h>
     57  1.28.2.15  mycroft #include <i386/isa/isavar.h>
     58  1.28.2.15  mycroft #include <i386/isa/icu.h>
     59  1.28.2.15  mycroft #include <i386/isa/timerreg.h>
     60  1.28.2.15  mycroft #include <i386/isa/spkr_reg.h>
     61       1.14  deraadt 
     62   1.28.2.1  mycroft isa_type isa_bustype;				/* type of bus */
     63        1.1      cgd 
     64   1.28.2.1  mycroft static int isaprobe __P((struct device *, struct cfdata *, void *));
     65   1.28.2.2  mycroft static void isaattach __P((struct device *, struct device *, void *));
     66   1.28.2.2  mycroft static int isasubmatch __P((struct device *, struct cfdata *, void *));
     67   1.28.2.1  mycroft 
     68   1.28.2.1  mycroft struct cfdriver isacd =
     69   1.28.2.2  mycroft     { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
     70   1.28.2.1  mycroft 
     71   1.28.2.9  mycroft static void isa_defaultirq __P((void));
     72   1.28.2.2  mycroft static int isaprint __P((void *, char *));
     73   1.28.2.9  mycroft static void isa_flushintrs __P((void));
     74  1.28.2.12  mycroft static void isa_intrmaskwickedness __P((void));
     75   1.28.2.2  mycroft 
     76   1.28.2.2  mycroft /*
     77   1.28.2.2  mycroft  * We think there might be an ISA bus here.  Check it out.
     78   1.28.2.2  mycroft  */
     79   1.28.2.2  mycroft static int
     80   1.28.2.1  mycroft isaprobe(parent, cf, aux)
     81   1.28.2.1  mycroft 	struct device *parent;
     82   1.28.2.1  mycroft 	struct cfdata *cf;
     83   1.28.2.1  mycroft 	void *aux;
     84   1.28.2.1  mycroft {
     85   1.28.2.1  mycroft 
     86   1.28.2.1  mycroft 	/* XXX should do a real probe */
     87   1.28.2.1  mycroft 	isa_bustype = BUS_ISA;
     88   1.28.2.1  mycroft 	return 1;
     89   1.28.2.1  mycroft }
     90   1.28.2.1  mycroft 
     91   1.28.2.2  mycroft /*
     92   1.28.2.2  mycroft  * Probe succeeded, someone called config_attach.  Now we have to
     93   1.28.2.2  mycroft  * probe the ISA bus itself in our turn.
     94   1.28.2.2  mycroft  */
     95   1.28.2.2  mycroft static void
     96   1.28.2.2  mycroft isaattach(parent, self, aux)
     97   1.28.2.2  mycroft 	struct device *parent, *self;
     98   1.28.2.2  mycroft 	void *aux;
     99   1.28.2.2  mycroft {
    100   1.28.2.2  mycroft 
    101   1.28.2.7  mycroft 	/* XXX should print ISA/EISA & bus clock frequency and anything else
    102   1.28.2.2  mycroft 	   we can figure out? */
    103   1.28.2.2  mycroft 	printf("\n");
    104   1.28.2.2  mycroft 
    105   1.28.2.2  mycroft 	isa_defaultirq();
    106   1.28.2.9  mycroft 	disable_intr();
    107   1.28.2.2  mycroft 	intr_enable(IRQ_SLAVE);
    108   1.28.2.2  mycroft 
    109   1.28.2.2  mycroft 	/* Iterate ``isasubmatch'' over all devices configured here. */
    110   1.28.2.2  mycroft 	(void)config_search(isasubmatch, self, (void *)NULL);
    111   1.28.2.2  mycroft 
    112  1.28.2.12  mycroft 	isa_intrmaskwickedness();
    113   1.28.2.9  mycroft 
    114   1.28.2.9  mycroft 	isa_flushintrs();
    115   1.28.2.9  mycroft 	enable_intr();
    116   1.28.2.2  mycroft }
    117   1.28.2.2  mycroft 
    118   1.28.2.2  mycroft /*
    119   1.28.2.2  mycroft  * isaattach (above) iterates this function over all the sub-devices that
    120   1.28.2.2  mycroft  * were configured at the ``isa'' device.  Our job is to provide defaults
    121   1.28.2.2  mycroft  * and do some internal/external representation conversion (some of which
    122   1.28.2.2  mycroft  * is scheduled to get cleaned up later), then call the device's probe
    123   1.28.2.2  mycroft  * function.  If this says the device is there, we try to reserve ISA
    124   1.28.2.2  mycroft  * bus memory and ports for the device, and attach it.
    125   1.28.2.2  mycroft  *
    126   1.28.2.2  mycroft  * Note that we always return 0, but our ultimate caller (isaattach)
    127   1.28.2.2  mycroft  * does not care that we never `match' anything.  (In fact, our return
    128   1.28.2.2  mycroft  * value is entirely irrelevant; this function is run entirely for its
    129   1.28.2.2  mycroft  * side effects.)
    130   1.28.2.2  mycroft  */
    131   1.28.2.1  mycroft static int
    132   1.28.2.2  mycroft isasubmatch(isa, cf, aux)
    133   1.28.2.2  mycroft 	struct device *isa;
    134   1.28.2.1  mycroft 	struct cfdata *cf;
    135   1.28.2.1  mycroft 	void *aux;
    136   1.28.2.1  mycroft {
    137   1.28.2.2  mycroft 	struct isa_attach_args ia;
    138   1.28.2.1  mycroft 
    139   1.28.2.1  mycroft #ifdef DIAGNOSTIC
    140   1.28.2.2  mycroft 	if (cf->cf_driver->cd_match == NULL) {
    141   1.28.2.2  mycroft 		/* we really ought to add printf formats to panic(). */
    142   1.28.2.1  mycroft 		printf("isasubmatch: no match function for `%s' device\n",
    143   1.28.2.1  mycroft 			cf->cf_driver->cd_name);
    144   1.28.2.1  mycroft 		panic("isasubmatch: no match function\n");
    145   1.28.2.1  mycroft 	}
    146   1.28.2.1  mycroft #endif
    147   1.28.2.1  mycroft 
    148  1.28.2.12  mycroft 	/* Init the info needed in the device probe and attach routines. */
    149   1.28.2.2  mycroft 	ia.ia_iobase = cf->cf_iobase;
    150   1.28.2.2  mycroft 	ia.ia_iosize = cf->cf_iosize;
    151   1.28.2.2  mycroft 	if (cf->cf_irq == -1)
    152   1.28.2.2  mycroft 		ia.ia_irq = IRQUNK;
    153   1.28.2.2  mycroft 	else
    154   1.28.2.2  mycroft 		ia.ia_irq = 1 << cf->cf_irq;
    155   1.28.2.2  mycroft 	ia.ia_drq = cf->cf_drq;
    156   1.28.2.2  mycroft 	ia.ia_maddr = (caddr_t)cf->cf_maddr;
    157   1.28.2.2  mycroft 	ia.ia_msize = cf->cf_msize;
    158   1.28.2.1  mycroft 
    159   1.28.2.2  mycroft 	/* If driver says it's not there, believe it. */
    160   1.28.2.2  mycroft 	if (!cf->cf_driver->cd_match(isa, cf, &ia))
    161   1.28.2.1  mycroft 		return 0;
    162   1.28.2.1  mycroft 
    163  1.28.2.12  mycroft 	/* Check for port or shared memory conflicts. */
    164  1.28.2.12  mycroft 	if (ia.ia_iosize && !isa_portcheck(ia.ia_iobase, ia.ia_iosize))
    165  1.28.2.12  mycroft 		return 0;
    166  1.28.2.12  mycroft 	if (ia.ia_msize && !isa_memcheck(ia.ia_maddr, ia.ia_msize))
    167  1.28.2.12  mycroft 		return 0;
    168  1.28.2.12  mycroft 
    169  1.28.2.12  mycroft 	/* Reserve I/O ports and/or shared memory. */
    170  1.28.2.12  mycroft 	if (ia.ia_iosize)
    171  1.28.2.11  mycroft 		isa_portalloc(ia.ia_iobase, ia.ia_iosize);
    172  1.28.2.12  mycroft 	if (ia.ia_msize)
    173  1.28.2.12  mycroft 		isa_memalloc(ia.ia_maddr, ia.ia_msize);
    174  1.28.2.12  mycroft 
    175  1.28.2.12  mycroft 	/* Configure device. */
    176  1.28.2.12  mycroft 	config_attach(isa, cf, &ia, isaprint);
    177   1.28.2.1  mycroft 
    178   1.28.2.2  mycroft 	return 0;
    179  1.28.2.14  mycroft }
    180  1.28.2.14  mycroft 
    181  1.28.2.14  mycroft /*
    182  1.28.2.14  mycroft  * XXX It's not clear to me why this is useful.
    183  1.28.2.14  mycroft  */
    184  1.28.2.14  mycroft void
    185  1.28.2.14  mycroft isa_establish(id, dv)
    186  1.28.2.14  mycroft 	struct isadev *id;
    187  1.28.2.14  mycroft 	struct device *dv;
    188  1.28.2.14  mycroft {
    189  1.28.2.14  mycroft 	struct isa_softc *idv = (struct isa_softc *)dv->dv_parent;
    190  1.28.2.14  mycroft 	id->id_dev = dv;
    191  1.28.2.14  mycroft 	id->id_bchain = idv->sc_isadev;
    192  1.28.2.14  mycroft 	idv->sc_isadev = id;
    193   1.28.2.1  mycroft }
    194   1.28.2.1  mycroft 
    195   1.28.2.2  mycroft /*
    196   1.28.2.2  mycroft  * Called indirectly via config_attach (see above).  Config has already
    197   1.28.2.2  mycroft  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    198   1.28.2.2  mycroft  * (it is always NULL, by definition) and just extend the line with
    199   1.28.2.2  mycroft  * the standard info (port(s), irq, drq, and iomem).
    200   1.28.2.2  mycroft  */
    201   1.28.2.2  mycroft static int
    202   1.28.2.1  mycroft isaprint(aux, isaname)
    203   1.28.2.1  mycroft 	void *aux;
    204   1.28.2.1  mycroft 	char *isaname;
    205   1.28.2.1  mycroft {
    206   1.28.2.1  mycroft 	struct isa_attach_args *ia = aux;
    207   1.28.2.1  mycroft 
    208   1.28.2.1  mycroft 	if (ia->ia_iosize)
    209   1.28.2.1  mycroft 		printf(" port 0x%x", ia->ia_iobase);
    210   1.28.2.1  mycroft 	if (ia->ia_iosize > 1)
    211   1.28.2.1  mycroft 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    212  1.28.2.12  mycroft 	if (ia->ia_msize)
    213  1.28.2.12  mycroft 		printf(" iomem 0x%x", ia->ia_maddr);
    214  1.28.2.12  mycroft 	if (ia->ia_msize > 1)
    215  1.28.2.12  mycroft 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    216   1.28.2.3  mycroft #ifdef DIAGNOSTIC
    217   1.28.2.3  mycroft 	if (ia->ia_irq == IRQUNK)
    218   1.28.2.4  mycroft 		printf(" THIS IS A BUG ->");
    219   1.28.2.3  mycroft #endif
    220   1.28.2.3  mycroft 	if (ia->ia_irq != IRQNONE)
    221   1.28.2.1  mycroft 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    222   1.28.2.1  mycroft 	if (ia->ia_drq != DRQUNK)
    223   1.28.2.1  mycroft 		printf(" drq %d", ia->ia_drq);
    224   1.28.2.7  mycroft 	/* XXXX print flags */
    225   1.28.2.2  mycroft 	return QUIET;	/* actually, our return value is irrelevant. */
    226        1.1      cgd }
    227        1.1      cgd 
    228  1.28.2.13  mycroft /*
    229  1.28.2.12  mycroft int isa_portcheck __P((u_int, u_int));
    230  1.28.2.12  mycroft int isa_memcheck __P((u_int, u_int));
    231  1.28.2.12  mycroft void isa_portalloc __P((u_int, u_int));
    232  1.28.2.12  mycroft void isa_memalloc __P((u_int, u_int));
    233  1.28.2.13  mycroft */
    234  1.28.2.12  mycroft 
    235  1.28.2.12  mycroft int	intrmask[NIRQ];
    236  1.28.2.12  mycroft struct	intrhand *intrhand[NIRQ];
    237  1.28.2.12  mycroft 
    238  1.28.2.12  mycroft /*
    239  1.28.2.12  mycroft  * Register an interrupt handler.
    240  1.28.2.12  mycroft  */
    241  1.28.2.12  mycroft void
    242  1.28.2.16  mycroft intr_establish(intr, ih, class)
    243  1.28.2.12  mycroft 	int intr;
    244  1.28.2.16  mycroft 	struct intrhand *ih;
    245  1.28.2.12  mycroft 	enum devclass class;
    246  1.28.2.12  mycroft {
    247  1.28.2.12  mycroft 	int irqnum = ffs(intr) - 1;
    248  1.28.2.16  mycroft 	register struct intrhand **p, *q;
    249  1.28.2.12  mycroft 
    250  1.28.2.12  mycroft #ifdef DIAGNOSTIC
    251  1.28.2.12  mycroft 	if (intr == IRQUNK || intr == IRQNONE ||
    252  1.28.2.12  mycroft 	    (intr &~ (1 << irqnum)) != IRQNONE)
    253  1.28.2.12  mycroft 		panic("intr_establish: weird irq");
    254  1.28.2.12  mycroft #endif
    255  1.28.2.12  mycroft 
    256  1.28.2.12  mycroft 	switch (class) {
    257  1.28.2.12  mycroft 	    case DV_DULL:
    258  1.28.2.12  mycroft 		break;
    259  1.28.2.12  mycroft 	    case DV_DISK:
    260  1.28.2.12  mycroft 	    case DV_TAPE:
    261  1.28.2.12  mycroft 		biomask |= intr;
    262  1.28.2.12  mycroft 		break;
    263  1.28.2.12  mycroft 	    case DV_IFNET:
    264  1.28.2.12  mycroft 		netmask |= intr;
    265  1.28.2.12  mycroft 		break;
    266  1.28.2.12  mycroft 	    case DV_TTY:
    267  1.28.2.12  mycroft 		ttymask |= intr;
    268  1.28.2.12  mycroft 		break;
    269  1.28.2.12  mycroft 	    case DV_CPU:
    270  1.28.2.12  mycroft 	    default:
    271  1.28.2.12  mycroft 		panic("intrhand: weird devclass");
    272  1.28.2.12  mycroft 	}
    273  1.28.2.13  mycroft 
    274  1.28.2.16  mycroft 	ih->ih_count = 0;
    275  1.28.2.16  mycroft 	ih->ih_next = NULL;
    276  1.28.2.16  mycroft 
    277  1.28.2.16  mycroft 	/*
    278  1.28.2.16  mycroft 	 * This is O(N^2), but we want to preserve the order, and N is
    279  1.28.2.16  mycroft 	 * always small.
    280  1.28.2.16  mycroft 	 */
    281  1.28.2.16  mycroft 	for (p = &intrhand[intr]; (q = *p) != NULL; p = &q->ih_next)
    282  1.28.2.16  mycroft 		;
    283  1.28.2.16  mycroft 	*p = ih;
    284  1.28.2.13  mycroft 
    285  1.28.2.13  mycroft 	intr_enable(intr);
    286  1.28.2.12  mycroft }
    287  1.28.2.12  mycroft 
    288  1.28.2.12  mycroft /*
    289  1.28.2.12  mycroft  * Set up the masks for each interrupt handler based on the information
    290  1.28.2.12  mycroft  * recorded by intr_establish().
    291  1.28.2.12  mycroft  */
    292  1.28.2.12  mycroft static void
    293  1.28.2.12  mycroft isa_intrmaskwickedness()
    294  1.28.2.12  mycroft {
    295  1.28.2.12  mycroft 	int irq, mask;
    296  1.28.2.12  mycroft 
    297  1.28.2.12  mycroft 	for (irq = 0; irq < NIRQ; irq++) {
    298  1.28.2.12  mycroft 		mask = (1 << irq) | astmask;
    299  1.28.2.12  mycroft 		if (biomask & (1 << irq))
    300  1.28.2.12  mycroft 			mask |= biomask;
    301  1.28.2.12  mycroft 		if (ttymask & (1 << irq))
    302  1.28.2.12  mycroft 			mask |= ttymask;
    303  1.28.2.12  mycroft 		if (netmask & (1 << irq))
    304  1.28.2.12  mycroft 			mask |= netmask;
    305  1.28.2.12  mycroft 		intrmask[irq] = mask;
    306  1.28.2.12  mycroft 	}
    307  1.28.2.12  mycroft 
    308  1.28.2.12  mycroft 	impmask = netmask | ttymask;
    309  1.28.2.12  mycroft 	printf("biomask %x ttymask %x netmask %x impmask %x astmask %s\n",
    310  1.28.2.12  mycroft 	       biomask, ttymask, netmask, impmask, astmask);
    311  1.28.2.12  mycroft }
    312        1.1      cgd 
    313        1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    314        1.1      cgd /* default interrupt vector table entries */
    315   1.28.2.7  mycroft extern	*IDTVEC(intr)[NIRQ];
    316        1.1      cgd /* out of range default interrupt vector gate entry */
    317   1.28.2.7  mycroft extern	IDTVEC(stray);
    318       1.15      cgd 
    319        1.1      cgd /*
    320   1.28.2.1  mycroft  * Fill in default interrupt table, and mask all interrupts.
    321        1.1      cgd  */
    322   1.28.2.9  mycroft static void
    323   1.28.2.1  mycroft isa_defaultirq()
    324   1.28.2.1  mycroft {
    325        1.1      cgd 	int i;
    326        1.1      cgd 
    327        1.1      cgd 	/* icu vectors */
    328   1.28.2.1  mycroft 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    329   1.28.2.7  mycroft 		setidt(i, IDTVEC(intr)[i],  SDT_SYS386IGT, SEL_KPL);
    330       1.15      cgd 
    331        1.1      cgd 	/* out of range vectors */
    332   1.28.2.1  mycroft 	for (; i < NIDT; i++)
    333   1.28.2.7  mycroft 		setidt(i, &IDTVEC(stray), SDT_SYS386IGT, SEL_KPL);
    334        1.1      cgd 
    335        1.1      cgd 	/* initialize 8259's */
    336        1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    337   1.28.2.1  mycroft 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    338   1.28.2.1  mycroft 	outb(IO_ICU1+1, IRQ_SLAVE);
    339       1.15      cgd #ifdef AUTO_EOI_1
    340       1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    341       1.15      cgd #else
    342        1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    343       1.15      cgd #endif
    344        1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    345   1.28.2.8  mycroft 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    346       1.21   andrew #ifdef REORDER_IRQ
    347       1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    348       1.21   andrew #endif
    349        1.1      cgd 
    350        1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    351   1.28.2.1  mycroft 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    352   1.28.2.1  mycroft 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    353       1.15      cgd #ifdef AUTO_EOI_2
    354       1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    355       1.15      cgd #else
    356   1.28.2.1  mycroft 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    357       1.15      cgd #endif
    358        1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    359       1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    360   1.28.2.8  mycroft }
    361   1.28.2.8  mycroft 
    362   1.28.2.8  mycroft /*
    363   1.28.2.9  mycroft  * Flush any pending interrupts.
    364   1.28.2.9  mycroft  */
    365   1.28.2.9  mycroft static void
    366   1.28.2.9  mycroft isa_flushintrs()
    367   1.28.2.9  mycroft {
    368   1.28.2.9  mycroft 	register int i;
    369   1.28.2.9  mycroft 
    370   1.28.2.9  mycroft 	/* clear any pending interrupts */
    371   1.28.2.9  mycroft 	for (i = 0; i < 8; i++) {
    372   1.28.2.9  mycroft 		outb(IO_ICU1, ICU_EOI);
    373   1.28.2.9  mycroft 		outb(IO_ICU2, ICU_EOI);
    374   1.28.2.9  mycroft 	}
    375   1.28.2.9  mycroft }
    376   1.28.2.9  mycroft 
    377   1.28.2.9  mycroft /*
    378   1.28.2.8  mycroft  * Determine what IRQ a device is using by trying to force it to generate an
    379   1.28.2.8  mycroft  * interrupt and seeing which IRQ line goes high.  It is not safe to call
    380   1.28.2.8  mycroft  * this function after autoconfig.
    381   1.28.2.8  mycroft  */
    382   1.28.2.8  mycroft u_short
    383   1.28.2.8  mycroft isa_discoverintr(force, aux)
    384   1.28.2.8  mycroft 	void (*force)();
    385   1.28.2.8  mycroft {
    386   1.28.2.8  mycroft 	int time = TIMER_FREQ * 1;	/* wait up to 1 second */
    387   1.28.2.8  mycroft 	u_int last, now;
    388   1.28.2.8  mycroft 	u_short iobase = IO_TIMER1;
    389   1.28.2.8  mycroft 
    390   1.28.2.9  mycroft 	isa_flushintrs();
    391   1.28.2.8  mycroft 	/* attempt to force interrupt */
    392   1.28.2.8  mycroft 	force(aux);
    393   1.28.2.8  mycroft 	/* set timer 2 to a known state */
    394   1.28.2.8  mycroft 	outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
    395   1.28.2.8  mycroft 	outb(iobase + TIMER_CNTR2, 0xff);
    396   1.28.2.8  mycroft 	outb(iobase + TIMER_CNTR2, 0xff);
    397   1.28.2.8  mycroft 	last = 0xffff;
    398   1.28.2.8  mycroft 	while (time > 0) {
    399   1.28.2.8  mycroft 		register u_char irr, lo, hi;
    400  1.28.2.10  mycroft 		irr = inb(IO_ICU1) & ~IRQ_SLAVE;
    401   1.28.2.8  mycroft 		if (irr)
    402   1.28.2.8  mycroft 			return 1 << (ffs(irr) - 1);
    403   1.28.2.8  mycroft 		irr = inb(IO_ICU2);
    404   1.28.2.8  mycroft 		if (irr)
    405   1.28.2.8  mycroft 			return 1 << (ffs(irr) + 7);
    406   1.28.2.8  mycroft 		outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_LATCH);
    407   1.28.2.8  mycroft 		lo = inb(iobase + TIMER_CNTR2);
    408   1.28.2.8  mycroft 		hi = inb(iobase + TIMER_CNTR2);
    409   1.28.2.8  mycroft 		now = (hi << 8) | lo;
    410   1.28.2.8  mycroft 		if (now <= last)
    411   1.28.2.8  mycroft 			time -= (last - now);
    412   1.28.2.8  mycroft 		else
    413  1.28.2.10  mycroft 			time -= 0x10000 - (now - last);
    414   1.28.2.8  mycroft 		last = now;
    415   1.28.2.8  mycroft 	}
    416   1.28.2.8  mycroft 	return IRQNONE;
    417        1.1      cgd }
    418        1.1      cgd 
    419        1.1      cgd /*
    420        1.1      cgd  * Handle a NMI, possibly a machine check.
    421        1.1      cgd  * return true to panic system, false to ignore.
    422   1.28.2.7  mycroft  *
    423   1.28.2.7  mycroft  * This implementation is hist[oe]rical.
    424        1.1      cgd  */
    425       1.21   andrew int
    426   1.28.2.7  mycroft isa_nmi()
    427   1.28.2.7  mycroft {
    428        1.1      cgd 
    429   1.28.2.7  mycroft 	log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    430   1.28.2.7  mycroft 	return 0;
    431        1.1      cgd }
    432        1.1      cgd 
    433        1.1      cgd /*
    434        1.1      cgd  * Caught a stray interrupt, notify
    435        1.1      cgd  */
    436       1.21   andrew void
    437   1.28.2.1  mycroft isa_strayintr(d)
    438   1.28.2.1  mycroft 	int d;
    439   1.28.2.1  mycroft {
    440   1.28.2.7  mycroft 	extern u_long intrcnt_stray;
    441        1.1      cgd 
    442        1.4      cgd 	/*
    443   1.28.2.1  mycroft 	 * Stray level 7 interrupts occur when someone raises an interrupt
    444   1.28.2.1  mycroft 	 * and then drops it before the CPU acknowledges it.  This means
    445   1.28.2.7  mycroft 	 * either the device is screwed or something is cli'ing too long
    446   1.28.2.7  mycroft 	 * and it's timing out.
    447   1.28.2.7  mycroft 	 *
    448   1.28.2.7  mycroft 	 * -1 is passed by the generic handler for out of range exceptions,
    449   1.28.2.7  mycroft 	 * since we don't really want 208 little vectors.  (It wouldn't be
    450   1.28.2.7  mycroft 	 * all that much code, but why bother?)
    451        1.4      cgd 	 */
    452   1.28.2.7  mycroft 	if (intrcnt_stray++ < 5)
    453   1.28.2.7  mycroft 		if (d == -1)
    454   1.28.2.7  mycroft 			log(LOG_ERR, "wild interrupt\n");
    455   1.28.2.7  mycroft 		else
    456   1.28.2.7  mycroft 			log(LOG_ERR, "stray interrupt %d\n", d);
    457       1.15      cgd 	if (intrcnt_stray == 5)
    458   1.28.2.7  mycroft 		log(LOG_ERR, "too many stray interrupts; stopped logging\n");
    459        1.1      cgd }
    460        1.1      cgd 
    461   1.28.2.7  mycroft static int beeping;
    462   1.28.2.7  mycroft 
    463       1.21   andrew static void
    464       1.21   andrew sysbeepstop(int f)
    465        1.1      cgd {
    466       1.16  mycroft 
    467        1.1      cgd 	/* disable counter 2 */
    468       1.16  mycroft 	disable_intr();
    469       1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    470       1.16  mycroft 	enable_intr();
    471        1.1      cgd 	if (f)
    472       1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    473        1.1      cgd 	else
    474        1.1      cgd 		beeping = 0;
    475        1.1      cgd }
    476        1.1      cgd 
    477       1.21   andrew void
    478       1.21   andrew sysbeep(int pitch, int period)
    479        1.1      cgd {
    480       1.16  mycroft 	static int last_pitch, last_period;
    481        1.1      cgd 
    482       1.16  mycroft 	if (beeping) {
    483       1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    484       1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    485        1.1      cgd 	}
    486       1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    487       1.16  mycroft 		/*
    488   1.28.2.7  mycroft 		 * XXX - move timer stuff to clock.c.
    489   1.28.2.7  mycroft 		 */
    490       1.16  mycroft 		disable_intr();
    491       1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    492   1.28.2.7  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch));
    493   1.28.2.7  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)>>8);
    494       1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    495       1.16  mycroft 		enable_intr();
    496       1.16  mycroft 	}
    497       1.16  mycroft 	last_pitch = pitch;
    498       1.16  mycroft 	beeping = last_period = period;
    499       1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    500        1.1      cgd }
    501