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