Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.3
      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.3 1993/09/30 20:21:17 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(" XXX");
    227 	else
    228 #endif
    229 	if (ia->ia_irq != IRQNONE)
    230 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    231 	if (ia->ia_drq != DRQUNK)
    232 		printf(" drq %d", ia->ia_drq);
    233 	if (ia->ia_msize)
    234 		printf(" iomem 0x%x", ia->ia_maddr);
    235 	if (ia->ia_msize > 1)
    236 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    237 	/* XXXX need to print flags */
    238 	return QUIET;	/* actually, our return value is irrelevant. */
    239 }
    240 
    241 
    242 #define	IDTVEC(name)	__CONCAT(X,name)
    243 /* default interrupt vector table entries */
    244 extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    245 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    246 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    247 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    248 
    249 static *defvec[16] = {
    250 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    251 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    252 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    253 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    254 
    255 /* out of range default interrupt vector gate entry */
    256 extern	IDTVEC(intrdefault);
    257 
    258 /*
    259  * Fill in default interrupt table, and mask all interrupts.
    260  */
    261 void
    262 isa_defaultirq()
    263 {
    264 	int i;
    265 
    266 	/* icu vectors */
    267 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    268 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    269 
    270 	/* out of range vectors */
    271 	for (; i < NIDT; i++)
    272 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    273 
    274 	/* initialize 8259's */
    275 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    276 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    277 	outb(IO_ICU1+1, IRQ_SLAVE);
    278 #ifdef AUTO_EOI_1
    279 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    280 #else
    281 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    282 #endif
    283 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    284 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    285 #ifdef REORDER_IRQ
    286 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    287 #endif
    288 
    289 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    290 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    291 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    292 #ifdef AUTO_EOI_2
    293 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    294 #else
    295 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    296 #endif
    297 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    298 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    299 }
    300 
    301 
    302 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
    303 
    304 int
    305 config_isadev(isdp, mp)
    306 	struct isa_device *isdp;
    307 	u_int *mp;
    308 {
    309 	if (isdp->id_irq) {
    310 		int intrno;
    311 
    312 		intrno = ffs(isdp->id_irq)-1;
    313 		setidt(ICU_OFFSET+intrno, isdp->id_intr,
    314 			 SDT_SYS386IGT, SEL_KPL);
    315 		if(mp)
    316 			INTRMASK(*mp,isdp->id_irq);
    317 		INTREN(isdp->id_irq);
    318 	}
    319 }
    320 
    321 
    322 /* region of physical memory known to be contiguous */
    323 vm_offset_t isaphysmem;
    324 static caddr_t bouncebuf[8];		/* XXX */
    325 static caddr_t bounced[8];		/* XXX */
    326 static vm_size_t bouncesize[8];		/* XXX */
    327 #define MAXDMASZ 512			/* XXX */
    328 
    329 /* high byte of address is stored in this port for i-th dma channel */
    330 static short dmapageport[8] =
    331 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    332 
    333 /*
    334  * isa_dmacascade(): program 8237 DMA controller channel to accept
    335  * external dma control by a board.
    336  */
    337 void
    338 at_dma_cascade(chan)
    339 	unsigned chan;
    340 {
    341 #ifdef DEBUG
    342 	if (chan > 7)
    343 		panic("at_dma_cascade: impossible request");
    344 #endif
    345 
    346 	/* set dma channel mode, and set dma channel mode */
    347 	if ((chan & 4) == 0) {
    348 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    349 		outb(DMA1_SMSK, chan);
    350 	} else {
    351 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    352 		outb(DMA2_SMSK, chan & 3);
    353 	}
    354 }
    355 
    356 /*
    357  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    358  * problems by using a bounce buffer.
    359  */
    360 void
    361 at_dma(flags, addr, nbytes, chan)
    362 	int read;
    363 	caddr_t addr;
    364 	vm_size_t nbytes;
    365 	unsigned chan;
    366 {
    367 	vm_offset_t phys;
    368 	int waport;
    369 	caddr_t newaddr;
    370 
    371 ppp	if (    chan > 7
    372 	    || (chan < 4 && nbytes > (1<<16))
    373 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    374 		panic("isa_dmastart: impossible request");
    375 
    376 	if (at_dma_rangecheck(addr, nbytes, chan)) {
    377 		panic("bounce buffers don't work yet\n");
    378 		/* XXX totally braindead; NBPG is not enough */
    379 		if (bouncebuf[chan] == 0)
    380 			bouncebuf[chan] =
    381 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    382 				(caddr_t) isaphysmem + NBPG*chan;
    383 		bouncesize[chan] = nbytes;
    384 		newaddr = bouncebuf[chan];
    385 		/* copy bounce buffer on write */
    386 		if (!read)
    387 			bcopy(addr, newaddr, nbytes);
    388 		else
    389 			bounced[chan] = addr;
    390 		addr = newaddr;
    391 	}
    392 
    393 	/* translate to physical */
    394 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    395 
    396 	if ((chan & 4) == 0) {
    397 		/*
    398 		 * Program one of DMA channels 0..3.  These are
    399 		 * byte mode channels.
    400 		 */
    401 		/* set dma channel mode, and reset address ff */
    402 		if (read)
    403 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    404 		else
    405 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    406 		outb(DMA1_FFC, 0);
    407 
    408 		/* send start address */
    409 		waport =  DMA1_CHN(chan);
    410 		outb(waport, phys);
    411 		outb(waport, phys>>8);
    412 		outb(dmapageport[chan], phys>>16);
    413 
    414 		/* send count */
    415 		outb(waport + 1, --nbytes);
    416 		outb(waport + 1, nbytes>>8);
    417 
    418 		/* unmask channel */
    419 		outb(DMA1_SMSK, chan);
    420 	} else {
    421 		/*
    422 		 * Program one of DMA channels 4..7.  These are
    423 		 * word mode channels.
    424 		 */
    425 		/* set dma channel mode, and reset address ff */
    426 		if (read)
    427 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    428 		else
    429 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    430 		outb(DMA2_FFC, 0);
    431 
    432 		/* send start address */
    433 		waport = DMA2_CHN(chan - 4);
    434 		outb(waport, phys>>1);
    435 		outb(waport, phys>>9);
    436 		outb(dmapageport[chan], phys>>16);
    437 
    438 		/* send count */
    439 		nbytes >>= 1;
    440 		outb(waport + 2, --nbytes);
    441 		outb(waport + 2, nbytes>>8);
    442 
    443 		/* unmask channel */
    444 		outb(DMA2_SMSK, chan & 3);
    445 	}
    446 }
    447 
    448 void
    449 at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
    450 {
    451 
    452 	if (bounced[chan]) {
    453 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    454 		bounced[chan] = 0;
    455 	}
    456 }
    457 
    458 /*
    459  * Check for problems with the address range of a DMA transfer
    460  * (non-contiguous physical pages, outside of bus address space,
    461  * crossing DMA page boundaries).
    462  * Return true if special handling needed.
    463  */
    464 
    465 int
    466 at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
    467 	vm_offset_t phys, priorpage = 0, endva;
    468 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    469 
    470 	endva = (vm_offset_t)round_page(va + length);
    471 	for (; va < (caddr_t) endva ; va += NBPG) {
    472 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    473 		if (phys == 0)
    474 			panic("isa_dmacheck: no physical page present");
    475 		if (phys > physmem)
    476 			return (1);
    477 		if (priorpage) {
    478 			if (priorpage + NBPG != phys)
    479 				return (1);
    480 			/* check if crossing a DMA page boundary */
    481 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    482 				return (1);
    483 		}
    484 		priorpage = phys;
    485 	}
    486 	return (0);
    487 }
    488 
    489 /* head of queue waiting for physmem to become available */
    490 struct buf isa_physmemq;
    491 
    492 /* blocked waiting for resource to become free for exclusive use */
    493 static isaphysmemflag;
    494 /* if waited for and call requested when free (B_CALL) */
    495 static void (*isaphysmemunblock)(); /* XXX needs to be a list */
    496 
    497 /*
    498  * Allocate contiguous physical memory for transfer, returning
    499  * a *virtual* address to region. May block waiting for resource.
    500  * (assumed to be called at splbio())
    501  */
    502 caddr_t
    503 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    504 
    505 	isaphysmemunblock = func;
    506 	while (isaphysmemflag & B_BUSY) {
    507 		isaphysmemflag |= B_WANTED;
    508 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    509 	}
    510 	isaphysmemflag |= B_BUSY;
    511 
    512 	return((caddr_t)isaphysmem);
    513 }
    514 
    515 /*
    516  * Free contiguous physical memory used for transfer.
    517  * (assumed to be called at splbio())
    518  */
    519 void
    520 isa_freephysmem(caddr_t va, unsigned length) {
    521 
    522 	isaphysmemflag &= ~B_BUSY;
    523 	if (isaphysmemflag & B_WANTED) {
    524 		isaphysmemflag &= B_WANTED;
    525 		wakeup((caddr_t)&isaphysmemflag);
    526 		if (isaphysmemunblock)
    527 			(*isaphysmemunblock)();
    528 	}
    529 }
    530 
    531 /*
    532  * Handle a NMI, possibly a machine check.
    533  * return true to panic system, false to ignore.
    534  */
    535 int
    536 isa_nmi(cd) {
    537 
    538 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    539 	return(0);
    540 }
    541 
    542 /*
    543  * Caught a stray interrupt, notify
    544  */
    545 void
    546 isa_strayintr(d)
    547 	int d;
    548 {
    549 
    550 	/*
    551 	 * Stray level 7 interrupts occur when someone raises an interrupt
    552 	 * and then drops it before the CPU acknowledges it.  This means
    553 	 * either the device is screwed or something is cli'ing too long.
    554 	 */
    555 	extern u_long intrcnt_stray;
    556 
    557 	intrcnt_stray++;
    558 	if (intrcnt_stray <= 5)
    559 		log(LOG_ERR, "stray interrupt %d\n", d);
    560 	if (intrcnt_stray == 5)
    561 		log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
    562 }
    563 
    564 static beeping;
    565 static void
    566 sysbeepstop(int f)
    567 {
    568 	int s = splhigh();
    569 
    570 	/* disable counter 2 */
    571 	disable_intr();
    572 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    573 	enable_intr();
    574 	if (f)
    575 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    576 	else
    577 		beeping = 0;
    578 
    579 	splx(s);
    580 }
    581 
    582 void
    583 sysbeep(int pitch, int period)
    584 {
    585 	int s = splhigh();
    586 	static int last_pitch, last_period;
    587 
    588 	if (beeping) {
    589 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    590 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    591 	}
    592 	if (!beeping || last_pitch != pitch) {
    593 		/*
    594 	 	* XXX - move timer stuff to clock.c.
    595 	 	*/
    596 		disable_intr();
    597 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    598 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    599 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    600 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    601 		enable_intr();
    602 	}
    603 	last_pitch = pitch;
    604 	beeping = last_period = period;
    605 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    606 
    607 	splx(s);
    608 }
    609 
    610 /*
    611  * Pass command to keyboard controller (8042)
    612  */
    613 unsigned
    614 kbc_8042cmd(int val)
    615 {
    616 	while (inb(KBSTATP)&KBS_IBF);
    617 	if (val) outb(KBCMDP, val);
    618 	while (inb(KBSTATP)&KBS_IBF);
    619 	return (inb(KBDATAP));
    620 }
    621 
    622 /*
    623  * find an ISA device in a given isa_devtab_* table, given
    624  * the table to search, the expected id_driver entry, and the unit number.
    625  *
    626  * this function is defined in isa_device.h, and this location is debatable;
    627  * i put it there because it's useless w/o, and directly operates on
    628  * the other stuff in that file.
    629  *
    630  */
    631 
    632 struct isa_device *find_isadev(table, driverp, unit)
    633 	struct isa_device *table;
    634 	struct isa_driver *driverp;
    635 	int unit;
    636 {
    637 	if (driverp == NULL) /* sanity check */
    638 		return NULL;
    639 
    640 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    641 		if (table->id_driver == 0)
    642 			return NULL;
    643 
    644 		table++;
    645         }
    646 
    647 	return table;
    648 }
    649 
    650 /*
    651  * Return nonzero if a (masked) irq is pending for a given device.
    652  */
    653 int
    654 isa_irq_pending(dvp)
    655 	struct isa_device *dvp;
    656 {
    657 	unsigned id_irq;
    658 
    659 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    660 	if (id_irq & 0xff)
    661 		return (inb(IO_ICU1) & id_irq);
    662 	return (inb(IO_ICU2) & (id_irq >> 8));
    663 }
    664