Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.33
      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.33 1994/02/16 07:26:52 hpeyerl 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 <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/conf.h>
     53 #include <sys/file.h>
     54 #include <sys/buf.h>
     55 #include <sys/uio.h>
     56 #include <sys/syslog.h>
     57 #include <sys/malloc.h>
     58 
     59 #include <vm/vm.h>
     60 
     61 #include <machine/segments.h>
     62 #include <machine/pio.h>
     63 #include <machine/cpufunc.h>
     64 
     65 #include <i386/isa/isa_device.h>
     66 #include <i386/isa/isa.h>
     67 #include <i386/isa/icu.h>
     68 #include <i386/isa/ic/i8237.h>
     69 #include <i386/isa/ic/i8042.h>
     70 #include <i386/isa/timerreg.h>
     71 #include <i386/isa/spkr_reg.h>
     72 
     73 /* sorry, has to be here, no place else really suitable */
     74 #include <machine/pc/display.h>
     75 u_short *Crtat = (u_short *)MONO_BUF;
     76 
     77 /*
     78 **  Register definitions for DMA controller 1 (channels 0..3):
     79 */
     80 #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     81 #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     82 #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     83 #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     84 
     85 /*
     86 **  Register definitions for DMA controller 2 (channels 4..7):
     87 */
     88 #define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
     89 #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     90 #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     91 #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     92 
     93 int config_isadev(struct isa_device *, u_int *);
     94 void config_attach(struct isa_driver *, struct isa_device *);
     95 static void sysbeepstop(int);
     96 
     97 /*
     98  * elink_reset: This is the reset code for the dumb 3c50[79] cards
     99  * which is required during probe.  The problem is that the two cards
    100  * use the same reset to the ID_PORT and hence the two drivers will
    101  * reset each others cards. This is notably non-optimal.
    102  */
    103 
    104 void
    105 elink_reset()
    106 {
    107 	static x;
    108 
    109 	if(x == 0)
    110 		outb(ELINK_ID_PORT, ELINK_RESET);
    111 	x = 1;
    112 }
    113 
    114 /*
    115  * Configure all ISA devices
    116  */
    117 void
    118 isa_configure()
    119 {
    120 	struct isa_device *dvp;
    121 	struct isa_driver *dp;
    122 
    123 	splhigh();
    124 	INTREN(IRQ_SLAVE);
    125 	enable_intr();
    126 
    127 	for (dvp = isa_devtab_tty; config_isadev(dvp,&ttymask); dvp++)
    128 		;
    129 	for (dvp = isa_devtab_bio; config_isadev(dvp,&biomask); dvp++)
    130 		;
    131 	for (dvp = isa_devtab_net; config_isadev(dvp,&netmask); dvp++)
    132 		;
    133 	for (dvp = isa_devtab_null; config_isadev(dvp, (u_int *) NULL); dvp++)
    134 		;
    135 
    136 	printf("biomask %x ttymask %x netmask %x\n",
    137 	       biomask, ttymask, netmask);
    138 
    139 	biomask |= astmask;
    140 	ttymask |= astmask;
    141 	netmask |= astmask;
    142 	impmask = netmask | ttymask;
    143 
    144 	spl0();
    145 }
    146 
    147 /*
    148  * Configure an ISA device.
    149  */
    150 int
    151 config_isadev(isdp, mp)
    152 	struct isa_device *isdp;
    153 	u_int *mp;
    154 {
    155 	struct isa_driver *dp;
    156 
    157 	if (dp = isdp->id_driver) {
    158 		if (isdp->id_maddr) {
    159 			extern u_int atdevbase;
    160 
    161 			isdp->id_maddr -= 0xa0000; /* XXX should be a define */
    162 			isdp->id_maddr += atdevbase;
    163 		}
    164 		isdp->id_alive = (*dp->probe)(isdp);
    165 		if (isdp->id_irq == (u_short)-1)
    166 			isdp->id_alive = 0;
    167 		/*
    168 		 * Only print the I/O address range if id_alive != -1
    169 		 * Right now this is a temporary fix just for the new
    170 		 * NPX code so that if it finds a 486 that can use trap
    171 		 * 16 it will not report I/O addresses.
    172 		 * Rod Grimes 04/26/94
    173 		 *
    174 		 * XXX -- cgd
    175 		 */
    176 		if (isdp->id_alive) {
    177 			printf("%s%d", dp->name, isdp->id_unit);
    178 			if (isdp->id_iobase) {
    179 				printf(" at 0x%x", isdp->id_iobase);
    180 				if ((isdp->id_iobase + isdp->id_alive - 1) !=
    181 				    isdp->id_iobase)
    182 					printf("-0x%x", isdp->id_iobase +
    183 					    isdp->id_alive - 1);
    184 			}
    185 			if (isdp->id_irq != 0)
    186 				printf(" irq %d", ffs(isdp->id_irq)-1);
    187 			if (isdp->id_drq != -1)
    188 				printf(" drq %d", isdp->id_drq);
    189 			if (isdp->id_maddr != 0)
    190 				printf(" maddr 0x%x", kvtop(isdp->id_maddr));
    191 			if (isdp->id_msize != 0)
    192 				printf("-0x%x", kvtop(isdp->id_maddr) +
    193 					isdp->id_msize - 1);
    194 			if (isdp->id_flags != 0)
    195 				printf(" flags 0x%x", isdp->id_flags);
    196 			printf(" on isa\n");
    197 
    198 			config_attach(dp, isdp);
    199 
    200 			if (isdp->id_irq) {
    201 				int intrno;
    202 
    203 				intrno = ffs(isdp->id_irq)-1;
    204 				setidt(ICU_OFFSET+intrno, isdp->id_intr,
    205 					 SDT_SYS386IGT, SEL_KPL);
    206 				if(mp)
    207 					INTRMASK(*mp,isdp->id_irq);
    208 				INTREN(isdp->id_irq);
    209 			}
    210 		}
    211 		return (1);
    212 	} else	return(0);
    213 }
    214 
    215 void
    216 config_attach(struct isa_driver *dp, struct isa_device *isdp)
    217 {
    218 	extern struct isa_device isa_subdev[];
    219 	struct isa_device *dvp;
    220 
    221 	if(isdp->id_masunit==-1) {
    222 		(void)(*dp->attach)(isdp);
    223 		return;
    224 	}
    225 
    226 	if(isdp->id_masunit==0) {
    227 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    228 			if (dvp->id_driver != dp)
    229 				continue;
    230 			if (dvp->id_masunit != isdp->id_unit)
    231 				continue;
    232 			if (dvp->id_physid == -1)
    233 				continue;
    234 			dvp->id_alive = (*dp->attach)(dvp);
    235 		}
    236 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    237 			if (dvp->id_driver != dp)
    238 				continue;
    239 			if (dvp->id_masunit != isdp->id_unit)
    240 				continue;
    241 			if (dvp->id_physid != -1)
    242 				continue;
    243 			dvp->id_alive = (*dp->attach)(dvp);
    244 		}
    245 		return;
    246 	}
    247 	printf("id_masunit has weird value\n");
    248 }
    249 
    250 
    251 #define	IDTVEC(name)	__CONCAT(X,name)
    252 /* default interrupt vector table entries */
    253 extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    254 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    255 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    256 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    257 
    258 static *defvec[16] = {
    259 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    260 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    261 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    262 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    263 
    264 /* out of range default interrupt vector gate entry */
    265 extern	IDTVEC(intrdefault);
    266 
    267 /*
    268  * Fill in default interrupt table (in case of spuruious interrupt
    269  * during configuration of kernel, setup interrupt control unit
    270  */
    271 void
    272 isa_defaultirq() {
    273 	int i;
    274 
    275 	/* icu vectors */
    276 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    277 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    278 
    279 	/* out of range vectors */
    280 	for (i = NRSVIDT; i < NIDT; i++)
    281 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    282 
    283 	/* initialize 8259's */
    284 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    285 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    286 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    287 #ifdef AUTO_EOI_1
    288 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    289 #else
    290 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    291 #endif
    292 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    293 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    294 #ifdef REORDER_IRQ
    295 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    296 #endif
    297 
    298 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    299 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    300 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    301 #ifdef AUTO_EOI_2
    302 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    303 #else
    304 	outb(IO_ICU2+1,1);		/* 8086 mode */
    305 #endif
    306 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    307 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    308 }
    309 
    310 /* region of physical memory known to be contiguous */
    311 vm_offset_t isaphysmem;
    312 static caddr_t dma_bounce[8];		/* XXX */
    313 static char bounced[8];		/* XXX */
    314 #define MAXDMASZ 512		/* XXX */
    315 
    316 /* high byte of address is stored in this port for i-th dma channel */
    317 static short dmapageport[8] =
    318 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    319 
    320 /*
    321  * isa_dmacascade(): program 8237 DMA controller channel to accept
    322  * external dma control by a board.
    323  */
    324 void
    325 isa_dmacascade(unsigned chan)
    326 {
    327 	if (chan > 7)
    328 		panic("isa_dmacascade: impossible request");
    329 
    330 	/* set dma channel mode, and set dma channel mode */
    331 	if ((chan & 4) == 0) {
    332 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    333 		outb(DMA1_SMSK, chan);
    334 	} else {
    335 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    336 		outb(DMA2_SMSK, chan & 3);
    337 	}
    338 }
    339 
    340 /*
    341  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    342  * problems by using a bounce buffer.
    343  */
    344 void
    345 isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
    346 {	vm_offset_t phys;
    347 	int waport;
    348 	caddr_t newaddr;
    349 
    350 	if (    chan > 7
    351 	    || (chan < 4 && nbytes > (1<<16))
    352 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    353 		panic("isa_dmastart: impossible request");
    354 
    355 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    356 		if (dma_bounce[chan] == 0)
    357 			dma_bounce[chan] =
    358 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    359 				(caddr_t) isaphysmem + NBPG*chan;
    360 		bounced[chan] = 1;
    361 		newaddr = dma_bounce[chan];
    362 		*(int *) newaddr = 0;	/* XXX */
    363 
    364 		/* copy bounce buffer on write */
    365 		if (!(flags & B_READ))
    366 			bcopy(addr, newaddr, nbytes);
    367 		addr = newaddr;
    368 	}
    369 
    370 	/* translate to physical */
    371 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    372 
    373 	if ((chan & 4) == 0) {
    374 		/*
    375 		 * Program one of DMA channels 0..3.  These are
    376 		 * byte mode channels.
    377 		 */
    378 		/* set dma channel mode, and reset address ff */
    379 		if (flags & B_READ)
    380 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    381 		else
    382 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    383 		outb(DMA1_FFC, 0);
    384 
    385 		/* send start address */
    386 		waport =  DMA1_CHN(chan);
    387 		outb(waport, phys);
    388 		outb(waport, phys>>8);
    389 		outb(dmapageport[chan], phys>>16);
    390 
    391 		/* send count */
    392 		outb(waport + 1, --nbytes);
    393 		outb(waport + 1, nbytes>>8);
    394 
    395 		/* unmask channel */
    396 		outb(DMA1_SMSK, chan);
    397 	} else {
    398 		/*
    399 		 * Program one of DMA channels 4..7.  These are
    400 		 * word mode channels.
    401 		 */
    402 		/* set dma channel mode, and reset address ff */
    403 		if (flags & B_READ)
    404 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    405 		else
    406 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    407 		outb(DMA2_FFC, 0);
    408 
    409 		/* send start address */
    410 		waport = DMA2_CHN(chan - 4);
    411 		outb(waport, phys>>1);
    412 		outb(waport, phys>>9);
    413 		outb(dmapageport[chan], phys>>16);
    414 
    415 		/* send count */
    416 		nbytes >>= 1;
    417 		outb(waport + 2, --nbytes);
    418 		outb(waport + 2, nbytes>>8);
    419 
    420 		/* unmask channel */
    421 		outb(DMA2_SMSK, chan & 3);
    422 	}
    423 }
    424 
    425 void
    426 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
    427 {
    428 
    429 	/* copy bounce buffer on read */
    430 	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
    431 	if (bounced[chan]) {
    432 		bcopy(dma_bounce[chan], addr, nbytes);
    433 		bounced[chan] = 0;
    434 	}
    435 }
    436 
    437 /*
    438  * Check for problems with the address range of a DMA transfer
    439  * (non-contiguous physical pages, outside of bus address space,
    440  * crossing DMA page boundaries).
    441  * Return true if special handling needed.
    442  */
    443 
    444 int
    445 isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
    446 	vm_offset_t phys, priorpage = 0, endva;
    447 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    448 
    449 	endva = (vm_offset_t)round_page(va + length);
    450 	for (; va < (caddr_t) endva ; va += NBPG) {
    451 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    452 #define ISARAM_END	RAM_END
    453 		if (phys == 0)
    454 			panic("isa_dmacheck: no physical page present");
    455 		if (phys > ISARAM_END)
    456 			return (1);
    457 		if (priorpage) {
    458 			if (priorpage + NBPG != phys)
    459 				return (1);
    460 			/* check if crossing a DMA page boundary */
    461 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    462 				return (1);
    463 		}
    464 		priorpage = phys;
    465 	}
    466 	return (0);
    467 }
    468 
    469 /* head of queue waiting for physmem to become available */
    470 struct buf isa_physmemq;
    471 
    472 /* blocked waiting for resource to become free for exclusive use */
    473 static isaphysmemflag;
    474 /* if waited for and call requested when free (B_CALL) */
    475 static void (*isaphysmemunblock)(); /* needs to be a list */
    476 
    477 /*
    478  * Allocate contiguous physical memory for transfer, returning
    479  * a *virtual* address to region. May block waiting for resource.
    480  * (assumed to be called at splbio())
    481  */
    482 caddr_t
    483 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    484 
    485 	isaphysmemunblock = func;
    486 	while (isaphysmemflag & B_BUSY) {
    487 		isaphysmemflag |= B_WANTED;
    488 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    489 	}
    490 	isaphysmemflag |= B_BUSY;
    491 
    492 	return((caddr_t)isaphysmem);
    493 }
    494 
    495 /*
    496  * Free contiguous physical memory used for transfer.
    497  * (assumed to be called at splbio())
    498  */
    499 void
    500 isa_freephysmem(caddr_t va, unsigned length) {
    501 
    502 	isaphysmemflag &= ~B_BUSY;
    503 	if (isaphysmemflag & B_WANTED) {
    504 		isaphysmemflag &= B_WANTED;
    505 		wakeup((caddr_t)&isaphysmemflag);
    506 		if (isaphysmemunblock)
    507 			(*isaphysmemunblock)();
    508 	}
    509 }
    510 
    511 /*
    512  * Handle a NMI, possibly a machine check.
    513  * return true to panic system, false to ignore.
    514  */
    515 int
    516 isa_nmi(cd) {
    517 
    518 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    519 	return(0);
    520 }
    521 
    522 /*
    523  * Caught a stray interrupt, notify
    524  */
    525 void
    526 isa_strayintr(d) {
    527 
    528 	/* DON'T BOTHER FOR NOW! */
    529 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    530 	/*
    531 	 * Well the reason you got bursts of intr #7 is because someone
    532 	 * raised an interrupt line and dropped it before the 8259 could
    533 	 * prioritize it.  This is documented in the intel data book.  This
    534 	 * means you have BAD hardware!  I have changed this so that only
    535 	 * the first 5 get logged, then it quits logging them, and puts
    536 	 * out a special message. rgrimes 3/25/1993
    537 	 */
    538 	extern u_long intrcnt_stray;
    539 
    540 	intrcnt_stray++;
    541 	if (intrcnt_stray <= 5)
    542 		log(LOG_ERR,"ISA strayintr %x\n", d);
    543 	if (intrcnt_stray == 5)
    544 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    545 }
    546 
    547 /*
    548  * Wait "n" microseconds.
    549  * Relies on timer 1 counting down from (TIMER_FREQ / hz) at
    550  * (1 * TIMER_FREQ) Hz.
    551  * Note: timer had better have been programmed before this is first used!
    552  * (Note that we use `rate generator' mode, which counts at 1:1; `square
    553  * wave' mode counts at 2:1).
    554  */
    555 #define       CF              (1 * TIMER_FREQ)
    556 
    557 extern int hz;                        /* XXX - should be elsewhere */
    558 
    559 void
    560 DELAY(n)
    561 	int n;
    562 {
    563 	int counter_limit;
    564 	int prev_tick;
    565 	int tick;
    566 	int ticks_left;
    567 	int sec;
    568 	int usec;
    569 
    570 #ifdef DELAYDEBUG
    571 	int gettick_calls = 1;
    572 	int n1;
    573 	static int state = 0;
    574 
    575 	if (state == 0) {
    576 		state = 1;
    577 		for (n1 = 1; n1 <= 10000000; n1 *= 10)
    578 			DELAY(n1);
    579 		state = 2;
    580 	}
    581 	if (state == 1)
    582 		printf("DELAY(%d)...", n);
    583 #endif
    584 
    585 	/*
    586 	 * Read the counter first, so that the rest of the setup overhead is
    587 	 * counted.  Guess the initial overhead is 20 usec (on most systems it
    588 	 * takes about 1.5 usec for each of the i/o's in gettick().  The loop
    589 	 * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
    590 	 * multiplications and divisions to scale the count take a while).
    591 	 */
    592 	prev_tick = gettick();
    593 	n -= 20;
    594 
    595 	/*
    596 	 * Calculate (n * (CF / 1e6)) without using floating point and without
    597 	 * any avoidable overflows.
    598 	 */
    599 	sec = n / 1000000;
    600 	usec = n - sec * 1000000;
    601 	ticks_left = sec * CF
    602 		+ usec * (CF / 1000000)
    603 		+ usec * ((CF % 1000000) / 1000) / 1000
    604 		+ usec * (CF % 1000) / 1000000;
    605 
    606 	counter_limit = TIMER_FREQ / hz;
    607 	while (ticks_left > 0) {
    608 		tick = gettick();
    609 #ifdef DELAYDEBUG
    610 		++gettick_calls;
    611 #endif
    612 		if (tick > prev_tick)
    613 			ticks_left -= prev_tick - (tick - counter_limit);
    614 		else
    615 			ticks_left -= prev_tick - tick;
    616 		prev_tick = tick;
    617 	}
    618 #ifdef DELAYDEBUG
    619 	if (state == 1)
    620 		printf(" %d calls to gettick() at %d usec each\n",
    621 			gettick_calls, (n + 5) / gettick_calls);
    622 #endif
    623 }
    624 
    625 int
    626 gettick() {
    627 	int high;
    628 	int low;
    629 
    630 	/*
    631 	 * Protect ourself against interrupts.
    632 	 */
    633 	disable_intr();
    634 	/*
    635 	 * Latch the count for 'timer' (cc00xxxx, c = counter, x = any).
    636 	 */
    637 	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
    638 	low = inb(TIMER_CNTR0);
    639 	high = inb(TIMER_CNTR0);
    640 	enable_intr();
    641 	return ((high << 8) | low);
    642 }
    643 
    644 static beeping;
    645 static void
    646 sysbeepstop(int f)
    647 {
    648 	int s = splhigh();
    649 
    650 	/* disable counter 2 */
    651 	disable_intr();
    652 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    653 	enable_intr();
    654 	if (f)
    655 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    656 	else
    657 		beeping = 0;
    658 
    659 	splx(s);
    660 }
    661 
    662 void
    663 sysbeep(int pitch, int period)
    664 {
    665 	int s = splhigh();
    666 	static int last_pitch, last_period;
    667 
    668 	if (beeping) {
    669 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    670 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    671 	}
    672 	if (!beeping || last_pitch != pitch) {
    673 		/*
    674 	 	* XXX - move timer stuff to clock.c.
    675 	 	*/
    676 		disable_intr();
    677 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    678 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    679 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    680 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    681 		enable_intr();
    682 	}
    683 	last_pitch = pitch;
    684 	beeping = last_period = period;
    685 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    686 
    687 	splx(s);
    688 }
    689 
    690 /*
    691  * Pass command to keyboard controller (8042)
    692  */
    693 unsigned
    694 kbc_8042cmd(int val)
    695 {
    696 	while (inb(KBSTATP)&KBS_IBF);
    697 	if (val) outb(KBCMDP, val);
    698 	while (inb(KBSTATP)&KBS_IBF);
    699 	return (inb(KBDATAP));
    700 }
    701 
    702 /*
    703  * find an ISA device in a given isa_devtab_* table, given
    704  * the table to search, the expected id_driver entry, and the unit number.
    705  *
    706  * this function is defined in isa_device.h, and this location is debatable;
    707  * i put it there because it's useless w/o, and directly operates on
    708  * the other stuff in that file.
    709  *
    710  */
    711 
    712 struct isa_device *find_isadev(table, driverp, unit)
    713 	struct isa_device *table;
    714 	struct isa_driver *driverp;
    715 	int unit;
    716 {
    717 	if (driverp == NULL) /* sanity check */
    718 		return NULL;
    719 
    720 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    721 		if (table->id_driver == 0)
    722 			return NULL;
    723 
    724 		table++;
    725         }
    726 
    727 	return table;
    728 }
    729 
    730 /*
    731  * Return nonzero if a (masked) irq is pending for a given device.
    732  */
    733 int
    734 isa_irq_pending(dvp)
    735 	struct isa_device *dvp;
    736 {
    737 	unsigned id_irq;
    738 
    739 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    740 	if (id_irq & 0xff)
    741 		return (inb(IO_ICU1) & id_irq);
    742 	return (inb(IO_ICU2) & (id_irq >> 8));
    743 }
    744