Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.20
      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.20 1993/06/20 07:24:53 deraadt 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("-0x%x", kvtop(isdp->id_maddr) +
    172 					isdp->id_msize - 1);
    173 			if (isdp->id_flags != 0)
    174 				printf(" flags 0x%x", isdp->id_flags);
    175 			printf(" on isa\n");
    176 
    177 			config_attach(dp, isdp);
    178 
    179 			if (isdp->id_irq) {
    180 				int intrno;
    181 
    182 				intrno = ffs(isdp->id_irq)-1;
    183 				setidt(ICU_OFFSET+intrno, isdp->id_intr,
    184 					 SDT_SYS386IGT, SEL_KPL);
    185 				if(mp)
    186 					INTRMASK(*mp,isdp->id_irq);
    187 				INTREN(isdp->id_irq);
    188 			}
    189 		}
    190 		return (1);
    191 	} else	return(0);
    192 }
    193 
    194 void
    195 config_attach(struct isa_driver *dp, struct isa_device *isdp)
    196 {
    197 	extern struct isa_device isa_subdev[];
    198 	struct isa_device *dvp;
    199 
    200 	if(isdp->id_masunit==-1) {
    201 		(void)(*dp->attach)(isdp);
    202 		return;
    203 	}
    204 
    205 	if(isdp->id_masunit==0) {
    206 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    207 			if (dvp->id_driver != dp)
    208 				continue;
    209 			if (dvp->id_masunit != isdp->id_unit)
    210 				continue;
    211 			if (dvp->id_physid == -1)
    212 				continue;
    213 			dvp->id_alive = (*dp->attach)(dvp);
    214 		}
    215 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    216 			if (dvp->id_driver != dp)
    217 				continue;
    218 			if (dvp->id_masunit != isdp->id_unit)
    219 				continue;
    220 			if (dvp->id_physid != -1)
    221 				continue;
    222 			dvp->id_alive = (*dp->attach)(dvp);
    223 		}
    224 		return;
    225 	}
    226 	printf("id_masunit has weird value\n");
    227 }
    228 
    229 
    230 #define	IDTVEC(name)	__CONCAT(X,name)
    231 /* default interrupt vector table entries */
    232 extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    233 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    234 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    235 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    236 
    237 static *defvec[16] = {
    238 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    239 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    240 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    241 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    242 
    243 /* out of range default interrupt vector gate entry */
    244 extern	IDTVEC(intrdefault);
    245 
    246 /*
    247  * Fill in default interrupt table (in case of spuruious interrupt
    248  * during configuration of kernel, setup interrupt control unit
    249  */
    250 isa_defaultirq() {
    251 	int i;
    252 
    253 	/* icu vectors */
    254 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    255 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    256 
    257 	/* out of range vectors */
    258 	for (i = NRSVIDT; i < NIDT; i++)
    259 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    260 
    261 	/* initialize 8259's */
    262 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    263 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    264 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    265 #ifdef AUTO_EOI_1
    266 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    267 #else
    268 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    269 #endif
    270 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    271 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    272 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    273 
    274 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    275 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    276 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    277 #ifdef AUTO_EOI_2
    278 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    279 #else
    280 	outb(IO_ICU2+1,1);		/* 8086 mode */
    281 #endif
    282 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    283 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    284 }
    285 
    286 /* region of physical memory known to be contiguous */
    287 vm_offset_t isaphysmem;
    288 static caddr_t dma_bounce[8];		/* XXX */
    289 static char bounced[8];		/* XXX */
    290 #define MAXDMASZ 512		/* XXX */
    291 
    292 /* high byte of address is stored in this port for i-th dma channel */
    293 static short dmapageport[8] =
    294 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    295 
    296 /*
    297  * isa_dmacascade(): program 8237 DMA controller channel to accept
    298  * external dma control by a board.
    299  */
    300 void isa_dmacascade(unsigned chan)
    301 {
    302 	if (chan > 7)
    303 		panic("isa_dmacascade: impossible request");
    304 
    305 	/* set dma channel mode, and set dma channel mode */
    306 	if ((chan & 4) == 0) {
    307 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    308 		outb(DMA1_SMSK, chan);
    309 	} else {
    310 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    311 		outb(DMA2_SMSK, chan & 3);
    312 	}
    313 }
    314 
    315 /*
    316  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    317  * problems by using a bounce buffer.
    318  */
    319 void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
    320 {	vm_offset_t phys;
    321 	int waport;
    322 	caddr_t newaddr;
    323 
    324 	if (    chan > 7
    325 	    || (chan < 4 && nbytes > (1<<16))
    326 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    327 		panic("isa_dmastart: impossible request");
    328 
    329 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    330 		if (dma_bounce[chan] == 0)
    331 			dma_bounce[chan] =
    332 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    333 				(caddr_t) isaphysmem + NBPG*chan;
    334 		bounced[chan] = 1;
    335 		newaddr = dma_bounce[chan];
    336 		*(int *) newaddr = 0;	/* XXX */
    337 
    338 		/* copy bounce buffer on write */
    339 		if (!(flags & B_READ))
    340 			bcopy(addr, newaddr, nbytes);
    341 		addr = newaddr;
    342 	}
    343 
    344 	/* translate to physical */
    345 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    346 
    347 	if ((chan & 4) == 0) {
    348 		/*
    349 		 * Program one of DMA channels 0..3.  These are
    350 		 * byte mode channels.
    351 		 */
    352 		/* set dma channel mode, and reset address ff */
    353 		if (flags & B_READ)
    354 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    355 		else
    356 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    357 		outb(DMA1_FFC, 0);
    358 
    359 		/* send start address */
    360 		waport =  DMA1_CHN(chan);
    361 		outb(waport, phys);
    362 		outb(waport, phys>>8);
    363 		outb(dmapageport[chan], phys>>16);
    364 
    365 		/* send count */
    366 		outb(waport + 1, --nbytes);
    367 		outb(waport + 1, nbytes>>8);
    368 
    369 		/* unmask channel */
    370 		outb(DMA1_SMSK, chan);
    371 	} else {
    372 		/*
    373 		 * Program one of DMA channels 4..7.  These are
    374 		 * word mode channels.
    375 		 */
    376 		/* set dma channel mode, and reset address ff */
    377 		if (flags & B_READ)
    378 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    379 		else
    380 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    381 		outb(DMA2_FFC, 0);
    382 
    383 		/* send start address */
    384 		waport = DMA2_CHN(chan - 4);
    385 		outb(waport, phys>>1);
    386 		outb(waport, phys>>9);
    387 		outb(dmapageport[chan], phys>>16);
    388 
    389 		/* send count */
    390 		nbytes >>= 1;
    391 		outb(waport + 2, --nbytes);
    392 		outb(waport + 2, nbytes>>8);
    393 
    394 		/* unmask channel */
    395 		outb(DMA2_SMSK, chan & 3);
    396 	}
    397 }
    398 
    399 void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
    400 {
    401 
    402 	/* copy bounce buffer on read */
    403 	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
    404 	if (bounced[chan]) {
    405 		bcopy(dma_bounce[chan], addr, nbytes);
    406 		bounced[chan] = 0;
    407 	}
    408 }
    409 
    410 /*
    411  * Check for problems with the address range of a DMA transfer
    412  * (non-contiguous physical pages, outside of bus address space,
    413  * crossing DMA page boundaries).
    414  * Return true if special handling needed.
    415  */
    416 
    417 isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
    418 	vm_offset_t phys, priorpage = 0, endva;
    419 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    420 
    421 	endva = (vm_offset_t)round_page(va + length);
    422 	for (; va < (caddr_t) endva ; va += NBPG) {
    423 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    424 #define ISARAM_END	RAM_END
    425 		if (phys == 0)
    426 			panic("isa_dmacheck: no physical page present");
    427 		if (phys > ISARAM_END)
    428 			return (1);
    429 		if (priorpage) {
    430 			if (priorpage + NBPG != phys)
    431 				return (1);
    432 			/* check if crossing a DMA page boundary */
    433 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    434 				return (1);
    435 		}
    436 		priorpage = phys;
    437 	}
    438 	return (0);
    439 }
    440 
    441 /* head of queue waiting for physmem to become available */
    442 struct buf isa_physmemq;
    443 
    444 /* blocked waiting for resource to become free for exclusive use */
    445 static isaphysmemflag;
    446 /* if waited for and call requested when free (B_CALL) */
    447 static void (*isaphysmemunblock)(); /* needs to be a list */
    448 
    449 /*
    450  * Allocate contiguous physical memory for transfer, returning
    451  * a *virtual* address to region. May block waiting for resource.
    452  * (assumed to be called at splbio())
    453  */
    454 caddr_t
    455 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    456 
    457 	isaphysmemunblock = func;
    458 	while (isaphysmemflag & B_BUSY) {
    459 		isaphysmemflag |= B_WANTED;
    460 		sleep(&isaphysmemflag, PRIBIO);
    461 	}
    462 	isaphysmemflag |= B_BUSY;
    463 
    464 	return((caddr_t)isaphysmem);
    465 }
    466 
    467 /*
    468  * Free contiguous physical memory used for transfer.
    469  * (assumed to be called at splbio())
    470  */
    471 void
    472 isa_freephysmem(caddr_t va, unsigned length) {
    473 
    474 	isaphysmemflag &= ~B_BUSY;
    475 	if (isaphysmemflag & B_WANTED) {
    476 		isaphysmemflag &= B_WANTED;
    477 		wakeup(&isaphysmemflag);
    478 		if (isaphysmemunblock)
    479 			(*isaphysmemunblock)();
    480 	}
    481 }
    482 
    483 /*
    484  * Handle a NMI, possibly a machine check.
    485  * return true to panic system, false to ignore.
    486  */
    487 isa_nmi(cd) {
    488 
    489 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    490 	return(0);
    491 }
    492 
    493 /*
    494  * Caught a stray interrupt, notify
    495  */
    496 isa_strayintr(d) {
    497 
    498 	/* DON'T BOTHER FOR NOW! */
    499 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    500 	/*
    501 	 * Well the reason you got bursts of intr #7 is because someone
    502 	 * raised an interrupt line and dropped it before the 8259 could
    503 	 * prioritize it.  This is documented in the intel data book.  This
    504 	 * means you have BAD hardware!  I have changed this so that only
    505 	 * the first 5 get logged, then it quits logging them, and puts
    506 	 * out a special message. rgrimes 3/25/1993
    507 	 */
    508 	extern u_long intrcnt_stray;
    509 
    510 	intrcnt_stray++;
    511 	if (intrcnt_stray <= 5)
    512 		log(LOG_ERR,"ISA strayintr %x\n", d);
    513 	if (intrcnt_stray == 5)
    514 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    515 }
    516 
    517 /*
    518  * Wait "n" microseconds.
    519  * Relies on timer 1 counting down from (TIMER_FREQ / hz) at
    520  * (1 * TIMER_FREQ) Hz.
    521  * Note: timer had better have been programmed before this is first used!
    522  * (Note that we use `rate generator' mode, which counts at 1:1; `square
    523  * wave' mode counts at 2:1).
    524  */
    525 #define       CF              (1 * TIMER_FREQ)
    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, TIMER_DIV(pitch)%256);
    652 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    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