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