Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.4
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * William Jolitz.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     37  *	$Id: isa.c,v 1.28.2.4 1993/10/06 12:09:12 mycroft Exp $
     38  */
     39 
     40 /*
     41  * code to manage AT bus
     42  *
     43  * 92/08/18  Frank P. MacLachlan (fpm (at) crash.cts.com):
     44  * Fixed uninitialized variable problem and added code to deal
     45  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
     46  * mode DMA count compution and reorganized DMA setup code in
     47  * isa_dmastart()
     48  */
     49 
     50 #include "param.h"
     51 #include "systm.h"
     52 #include "conf.h"
     53 #include "file.h"
     54 #include "buf.h"
     55 #include "uio.h"
     56 #include "syslog.h"
     57 #include "malloc.h"
     58 #include "machine/cpu.h"
     59 #include "machine/cpufunc.h"
     60 #include "sys/device.h"
     61 #include "vm/vm.h"
     62 #include "i386/isa/isa.h"
     63 #include "i386/isa/isavar.h"
     64 #include "i386/isa/icu.h"
     65 #include "i386/isa/ic/i8237.h"
     66 #include "i386/isa/ic/i8042.h"
     67 #include "i386/isa/timerreg.h"
     68 #include "i386/isa/spkr_reg.h"
     69 
     70 /*
     71 **  Register definitions for DMA controller 1 (channels 0..3):
     72 */
     73 #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     74 #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     75 #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     76 #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     77 
     78 /*
     79 **  Register definitions for DMA controller 2 (channels 4..7):
     80 */
     81 #define	DMA2_CHN(c)	(IO_DMA1 + 2*(2*(c)))	/* addr reg for channel c */
     82 #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     83 #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     84 #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     85 
     86 isa_type isa_bustype;				/* type of bus */
     87 
     88 static int isaprobe __P((struct device *, struct cfdata *, void *));
     89 static void isaattach __P((struct device *, struct device *, void *));
     90 static int isasubmatch __P((struct device *, struct cfdata *, void *));
     91 
     92 struct cfdriver isacd =
     93     { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
     94 
     95 void isa_defaultirq __P((void));
     96 static int isaprint __P((void *, char *));
     97 
     98 /*
     99  * We think there might be an ISA bus here.  Check it out.
    100  */
    101 static int
    102 isaprobe(parent, cf, aux)
    103 	struct device *parent;
    104 	struct cfdata *cf;
    105 	void *aux;
    106 {
    107 
    108 	/* XXX should do a real probe */
    109 	isa_bustype = BUS_ISA;
    110 	return 1;
    111 }
    112 
    113 /*
    114  * Probe succeeded, someone called config_attach.  Now we have to
    115  * probe the ISA bus itself in our turn.
    116  */
    117 static void
    118 isaattach(parent, self, aux)
    119 	struct device *parent, *self;
    120 	void *aux;
    121 {
    122 
    123 	/* should print ISA/EISA & bus clock frequency and anything else
    124 	   we can figure out? */
    125 	printf("\n");
    126 
    127 	isa_defaultirq();
    128 
    129 	enable_intr();
    130 	splhigh();
    131 	intr_enable(IRQ_SLAVE);
    132 
    133 	/* Iterate ``isasubmatch'' over all devices configured here. */
    134 	(void)config_search(isasubmatch, self, (void *)NULL);
    135 
    136 	/* and the problem is... if netmask == 0, then the loopback
    137 	 * code can do some really ugly things.
    138 	 * workaround for this: if netmask == 0, set it to 0x8000, which
    139 	 * is the value used by splsoftclock.  this is nasty, but it
    140 	 * should work until this interrupt system goes away. -- cgd
    141 	 */
    142 	if (netmask == 0)
    143 		netmask = 0x8000;	/* same as for softclock.  XXXX */
    144 
    145 	printf("biomask %x ttymask %x netmask %x impmask %x\n",
    146 	       biomask, ttymask, netmask, impmask);
    147 	splnone();
    148 }
    149 
    150 /*
    151  * isaattach (above) iterates this function over all the sub-devices that
    152  * were configured at the ``isa'' device.  Our job is to provide defaults
    153  * and do some internal/external representation conversion (some of which
    154  * is scheduled to get cleaned up later), then call the device's probe
    155  * function.  If this says the device is there, we try to reserve ISA
    156  * bus memory and ports for the device, and attach it.
    157  *
    158  * Note that we always return 0, but our ultimate caller (isaattach)
    159  * does not care that we never `match' anything.  (In fact, our return
    160  * value is entirely irrelevant; this function is run entirely for its
    161  * side effects.)
    162  */
    163 static int
    164 isasubmatch(isa, cf, aux)
    165 	struct device *isa;
    166 	struct cfdata *cf;
    167 	void *aux;
    168 {
    169 	struct isa_attach_args ia;
    170 
    171 #ifdef DIAGNOSTIC
    172 	if (cf->cf_driver->cd_match == NULL) {
    173 		/* we really ought to add printf formats to panic(). */
    174 		printf("isasubmatch: no match function for `%s' device\n",
    175 			cf->cf_driver->cd_name);
    176 		panic("isasubmatch: no match function\n");
    177 	}
    178 #endif
    179 
    180 	/* Init the info needed in the device probe routine. */
    181 	ia.ia_iobase = cf->cf_iobase;
    182 	ia.ia_iosize = cf->cf_iosize;
    183 	if (cf->cf_irq == -1)
    184 		ia.ia_irq = IRQUNK;
    185 	else
    186 		ia.ia_irq = 1 << cf->cf_irq;
    187 	ia.ia_drq = cf->cf_drq;
    188 	ia.ia_maddr = (caddr_t)cf->cf_maddr;
    189 	ia.ia_msize = cf->cf_msize;
    190 
    191 	/* If driver says it's not there, believe it. */
    192 	if (!cf->cf_driver->cd_match(isa, cf, &ia))
    193 		return 0;
    194 
    195 	/* Driver says it is there.  Try to reserve ports and memory. */
    196 	if (isa_reserveports(ia.ia_iobase, ia.ia_iosize)) {
    197 		if (isa_reservemem(ia.ia_maddr, ia.ia_msize))
    198 			config_attach(isa, cf, &ia, isaprint);	/* victory! */
    199 		else
    200 			isa_unreserveports(ia.ia_iobase, ia.ia_iosize);
    201 	}
    202 
    203 	/* In any case, move on to next config entry. */
    204 	return 0;
    205 }
    206 
    207 /*
    208  * Called indirectly via config_attach (see above).  Config has already
    209  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    210  * (it is always NULL, by definition) and just extend the line with
    211  * the standard info (port(s), irq, drq, and iomem).
    212  */
    213 static int
    214 isaprint(aux, isaname)
    215 	void *aux;
    216 	char *isaname;
    217 {
    218 	struct isa_attach_args *ia = aux;
    219 
    220 	if (ia->ia_iosize)
    221 		printf(" port 0x%x", ia->ia_iobase);
    222 	if (ia->ia_iosize > 1)
    223 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    224 #ifdef DIAGNOSTIC
    225 	if (ia->ia_irq == IRQUNK)
    226 		printf(" THIS IS A BUG ->");
    227 #endif
    228 	if (ia->ia_irq != IRQNONE)
    229 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    230 	if (ia->ia_drq != DRQUNK)
    231 		printf(" drq %d", ia->ia_drq);
    232 	if (ia->ia_msize)
    233 		printf(" iomem 0x%x", ia->ia_maddr);
    234 	if (ia->ia_msize > 1)
    235 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    236 	/* XXXX need to print flags */
    237 	return QUIET;	/* actually, our return value is irrelevant. */
    238 }
    239 
    240 
    241 #define	IDTVEC(name)	__CONCAT(X,name)
    242 /* default interrupt vector table entries */
    243 extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    244 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    245 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    246 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    247 
    248 static *defvec[16] = {
    249 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    250 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    251 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    252 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    253 
    254 /* out of range default interrupt vector gate entry */
    255 extern	IDTVEC(intrdefault);
    256 
    257 /*
    258  * Fill in default interrupt table, and mask all interrupts.
    259  */
    260 void
    261 isa_defaultirq()
    262 {
    263 	int i;
    264 
    265 	/* icu vectors */
    266 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    267 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    268 
    269 	/* out of range vectors */
    270 	for (; i < NIDT; i++)
    271 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    272 
    273 	/* initialize 8259's */
    274 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    275 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    276 	outb(IO_ICU1+1, IRQ_SLAVE);
    277 #ifdef AUTO_EOI_1
    278 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    279 #else
    280 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    281 #endif
    282 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    283 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    284 #ifdef REORDER_IRQ
    285 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    286 #endif
    287 
    288 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    289 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    290 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    291 #ifdef AUTO_EOI_2
    292 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    293 #else
    294 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    295 #endif
    296 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    297 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    298 }
    299 
    300 
    301 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
    302 
    303 int
    304 config_isadev(isdp, mp)
    305 	struct isa_device *isdp;
    306 	u_int *mp;
    307 {
    308 	if (isdp->id_irq) {
    309 		int intrno;
    310 
    311 		intrno = ffs(isdp->id_irq)-1;
    312 		setidt(ICU_OFFSET+intrno, isdp->id_intr,
    313 			 SDT_SYS386IGT, SEL_KPL);
    314 		if(mp)
    315 			INTRMASK(*mp,isdp->id_irq);
    316 		INTREN(isdp->id_irq);
    317 	}
    318 }
    319 
    320 
    321 /* region of physical memory known to be contiguous */
    322 vm_offset_t isaphysmem;
    323 static caddr_t bouncebuf[8];		/* XXX */
    324 static caddr_t bounced[8];		/* XXX */
    325 static vm_size_t bouncesize[8];		/* XXX */
    326 #define MAXDMASZ 512			/* XXX */
    327 
    328 /* high byte of address is stored in this port for i-th dma channel */
    329 static short dmapageport[8] =
    330 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    331 
    332 /*
    333  * isa_dmacascade(): program 8237 DMA controller channel to accept
    334  * external dma control by a board.
    335  */
    336 void
    337 at_dma_cascade(chan)
    338 	unsigned chan;
    339 {
    340 #ifdef DEBUG
    341 	if (chan > 7)
    342 		panic("at_dma_cascade: impossible request");
    343 #endif
    344 
    345 	/* set dma channel mode, and set dma channel mode */
    346 	if ((chan & 4) == 0) {
    347 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    348 		outb(DMA1_SMSK, chan);
    349 	} else {
    350 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    351 		outb(DMA2_SMSK, chan & 3);
    352 	}
    353 }
    354 
    355 /*
    356  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    357  * problems by using a bounce buffer.
    358  */
    359 void
    360 at_dma(flags, addr, nbytes, chan)
    361 	int read;
    362 	caddr_t addr;
    363 	vm_size_t nbytes;
    364 	unsigned chan;
    365 {
    366 	vm_offset_t phys;
    367 	int waport;
    368 	caddr_t newaddr;
    369 
    370 ppp	if (    chan > 7
    371 	    || (chan < 4 && nbytes > (1<<16))
    372 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    373 		panic("isa_dmastart: impossible request");
    374 
    375 	if (at_dma_rangecheck(addr, nbytes, chan)) {
    376 		panic("bounce buffers don't work yet\n");
    377 		/* XXX totally braindead; NBPG is not enough */
    378 		if (bouncebuf[chan] == 0)
    379 			bouncebuf[chan] =
    380 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    381 				(caddr_t) isaphysmem + NBPG*chan;
    382 		bouncesize[chan] = nbytes;
    383 		newaddr = bouncebuf[chan];
    384 		/* copy bounce buffer on write */
    385 		if (!read)
    386 			bcopy(addr, newaddr, nbytes);
    387 		else
    388 			bounced[chan] = addr;
    389 		addr = newaddr;
    390 	}
    391 
    392 	/* translate to physical */
    393 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    394 
    395 	if ((chan & 4) == 0) {
    396 		/*
    397 		 * Program one of DMA channels 0..3.  These are
    398 		 * byte mode channels.
    399 		 */
    400 		/* set dma channel mode, and reset address ff */
    401 		if (read)
    402 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    403 		else
    404 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    405 		outb(DMA1_FFC, 0);
    406 
    407 		/* send start address */
    408 		waport =  DMA1_CHN(chan);
    409 		outb(waport, phys);
    410 		outb(waport, phys>>8);
    411 		outb(dmapageport[chan], phys>>16);
    412 
    413 		/* send count */
    414 		outb(waport + 1, --nbytes);
    415 		outb(waport + 1, nbytes>>8);
    416 
    417 		/* unmask channel */
    418 		outb(DMA1_SMSK, chan);
    419 	} else {
    420 		/*
    421 		 * Program one of DMA channels 4..7.  These are
    422 		 * word mode channels.
    423 		 */
    424 		/* set dma channel mode, and reset address ff */
    425 		if (read)
    426 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    427 		else
    428 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    429 		outb(DMA2_FFC, 0);
    430 
    431 		/* send start address */
    432 		waport = DMA2_CHN(chan - 4);
    433 		outb(waport, phys>>1);
    434 		outb(waport, phys>>9);
    435 		outb(dmapageport[chan], phys>>16);
    436 
    437 		/* send count */
    438 		nbytes >>= 1;
    439 		outb(waport + 2, --nbytes);
    440 		outb(waport + 2, nbytes>>8);
    441 
    442 		/* unmask channel */
    443 		outb(DMA2_SMSK, chan & 3);
    444 	}
    445 }
    446 
    447 void
    448 at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
    449 {
    450 
    451 	if (bounced[chan]) {
    452 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    453 		bounced[chan] = 0;
    454 	}
    455 }
    456 
    457 /*
    458  * Check for problems with the address range of a DMA transfer
    459  * (non-contiguous physical pages, outside of bus address space,
    460  * crossing DMA page boundaries).
    461  * Return true if special handling needed.
    462  */
    463 
    464 int
    465 at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
    466 	vm_offset_t phys, priorpage = 0, endva;
    467 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    468 
    469 	endva = (vm_offset_t)round_page(va + length);
    470 	for (; va < (caddr_t) endva ; va += NBPG) {
    471 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    472 		if (phys == 0)
    473 			panic("isa_dmacheck: no physical page present");
    474 		if (phys > physmem)
    475 			return (1);
    476 		if (priorpage) {
    477 			if (priorpage + NBPG != phys)
    478 				return (1);
    479 			/* check if crossing a DMA page boundary */
    480 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    481 				return (1);
    482 		}
    483 		priorpage = phys;
    484 	}
    485 	return (0);
    486 }
    487 
    488 /* head of queue waiting for physmem to become available */
    489 struct buf isa_physmemq;
    490 
    491 /* blocked waiting for resource to become free for exclusive use */
    492 static isaphysmemflag;
    493 /* if waited for and call requested when free (B_CALL) */
    494 static void (*isaphysmemunblock)(); /* XXX needs to be a list */
    495 
    496 /*
    497  * Allocate contiguous physical memory for transfer, returning
    498  * a *virtual* address to region. May block waiting for resource.
    499  * (assumed to be called at splbio())
    500  */
    501 caddr_t
    502 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    503 
    504 	isaphysmemunblock = func;
    505 	while (isaphysmemflag & B_BUSY) {
    506 		isaphysmemflag |= B_WANTED;
    507 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    508 	}
    509 	isaphysmemflag |= B_BUSY;
    510 
    511 	return((caddr_t)isaphysmem);
    512 }
    513 
    514 /*
    515  * Free contiguous physical memory used for transfer.
    516  * (assumed to be called at splbio())
    517  */
    518 void
    519 isa_freephysmem(caddr_t va, unsigned length) {
    520 
    521 	isaphysmemflag &= ~B_BUSY;
    522 	if (isaphysmemflag & B_WANTED) {
    523 		isaphysmemflag &= B_WANTED;
    524 		wakeup((caddr_t)&isaphysmemflag);
    525 		if (isaphysmemunblock)
    526 			(*isaphysmemunblock)();
    527 	}
    528 }
    529 
    530 /*
    531  * Handle a NMI, possibly a machine check.
    532  * return true to panic system, false to ignore.
    533  */
    534 int
    535 isa_nmi(cd) {
    536 
    537 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    538 	return(0);
    539 }
    540 
    541 /*
    542  * Caught a stray interrupt, notify
    543  */
    544 void
    545 isa_strayintr(d)
    546 	int d;
    547 {
    548 
    549 	/*
    550 	 * Stray level 7 interrupts occur when someone raises an interrupt
    551 	 * and then drops it before the CPU acknowledges it.  This means
    552 	 * either the device is screwed or something is cli'ing too long.
    553 	 */
    554 	extern u_long intrcnt_stray;
    555 
    556 	intrcnt_stray++;
    557 	if (intrcnt_stray <= 5)
    558 		log(LOG_ERR, "stray interrupt %d\n", d);
    559 	if (intrcnt_stray == 5)
    560 		log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
    561 }
    562 
    563 static beeping;
    564 static void
    565 sysbeepstop(int f)
    566 {
    567 	int s = splhigh();
    568 
    569 	/* disable counter 2 */
    570 	disable_intr();
    571 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    572 	enable_intr();
    573 	if (f)
    574 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    575 	else
    576 		beeping = 0;
    577 
    578 	splx(s);
    579 }
    580 
    581 void
    582 sysbeep(int pitch, int period)
    583 {
    584 	int s = splhigh();
    585 	static int last_pitch, last_period;
    586 
    587 	if (beeping) {
    588 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    589 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    590 	}
    591 	if (!beeping || last_pitch != pitch) {
    592 		/*
    593 	 	* XXX - move timer stuff to clock.c.
    594 	 	*/
    595 		disable_intr();
    596 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    597 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    598 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    599 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    600 		enable_intr();
    601 	}
    602 	last_pitch = pitch;
    603 	beeping = last_period = period;
    604 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    605 
    606 	splx(s);
    607 }
    608 
    609 /*
    610  * Pass command to keyboard controller (8042)
    611  */
    612 unsigned
    613 kbc_8042cmd(int val)
    614 {
    615 	while (inb(KBSTATP)&KBS_IBF);
    616 	if (val) outb(KBCMDP, val);
    617 	while (inb(KBSTATP)&KBS_IBF);
    618 	return (inb(KBDATAP));
    619 }
    620 
    621 /*
    622  * find an ISA device in a given isa_devtab_* table, given
    623  * the table to search, the expected id_driver entry, and the unit number.
    624  *
    625  * this function is defined in isa_device.h, and this location is debatable;
    626  * i put it there because it's useless w/o, and directly operates on
    627  * the other stuff in that file.
    628  *
    629  */
    630 
    631 struct isa_device *find_isadev(table, driverp, unit)
    632 	struct isa_device *table;
    633 	struct isa_driver *driverp;
    634 	int unit;
    635 {
    636 	if (driverp == NULL) /* sanity check */
    637 		return NULL;
    638 
    639 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    640 		if (table->id_driver == 0)
    641 			return NULL;
    642 
    643 		table++;
    644         }
    645 
    646 	return table;
    647 }
    648 
    649 /*
    650  * Return nonzero if a (masked) irq is pending for a given device.
    651  */
    652 int
    653 isa_irq_pending(dvp)
    654 	struct isa_device *dvp;
    655 {
    656 	unsigned id_irq;
    657 
    658 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    659 	if (id_irq & 0xff)
    660 		return (inb(IO_ICU1) & id_irq);
    661 	return (inb(IO_ICU2) & (id_irq >> 8));
    662 }
    663