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