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