isa.c revision 1323
1214640Sdim/*-
2214640Sdim * Copyright (c) 1991 The Regents of the University of California.
3214640Sdim * All rights reserved.
4214640Sdim *
5214640Sdim * This code is derived from software contributed to Berkeley by
6214640Sdim * William Jolitz.
7214640Sdim *
8214640Sdim * Redistribution and use in source and binary forms, with or without
9214640Sdim * modification, are permitted provided that the following conditions
10214640Sdim * are met:
11214640Sdim * 1. Redistributions of source code must retain the above copyright
12214640Sdim *    notice, this list of conditions and the following disclaimer.
13214640Sdim * 2. Redistributions in binary form must reproduce the above copyright
14214640Sdim *    notice, this list of conditions and the following disclaimer in the
15214640Sdim *    documentation and/or other materials provided with the distribution.
16214640Sdim * 3. All advertising materials mentioning features or use of this software
17214640Sdim *    must display the following acknowledgement:
18214640Sdim *	This product includes software developed by the University of
19214640Sdim *	California, Berkeley and its contributors.
20214640Sdim * 4. Neither the name of the University nor the names of its contributors
21214640Sdim *    may be used to endorse or promote products derived from this software
22214640Sdim *    without specific prior written permission.
23214640Sdim *
24214640Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25214640Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26214640Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27214640Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28214640Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29214640Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30214640Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31214640Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32214640Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33214640Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34214640Sdim * SUCH DAMAGE.
35214640Sdim *
36214640Sdim *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
37214640Sdim *	$Id: isa.c,v 1.15 1994/04/02 07:00:46 davidg Exp $
38214640Sdim */
39214640Sdim
40214640Sdim/*
41214640Sdim * code to manage AT bus
42214640Sdim *
43214640Sdim * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44214640Sdim * Fixed uninitialized variable problem and added code to deal
45214640Sdim * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46214640Sdim * mode DMA count compution and reorganized DMA setup code in
47214640Sdim * isa_dmastart()
48214640Sdim */
49214640Sdim
50214640Sdim#include "param.h"
51214640Sdim#include "systm.h"		/* isn't it a joy */
52214640Sdim#include "kernel.h"		/* to have three of these */
53214640Sdim#include "conf.h"
54214640Sdim#include "file.h"
55214640Sdim#include "buf.h"
56214640Sdim#include "uio.h"
57214640Sdim#include "syslog.h"
58214640Sdim#include "malloc.h"
59214640Sdim#include "rlist.h"
60214640Sdim#include "machine/segments.h"
61214640Sdim#include "vm/vm.h"
62214640Sdim#include "i386/isa/isa_device.h"
63214640Sdim#include "i386/isa/isa.h"
64214640Sdim#include "i386/isa/icu.h"
65214640Sdim#include "i386/isa/ic/i8237.h"
66214640Sdim#include "i386/isa/ic/i8042.h"
67214640Sdim
68214640Sdim/*
69214640Sdim**  Register definitions for DMA controller 1 (channels 0..3):
70214640Sdim*/
71214640Sdim#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
72214640Sdim#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
73214640Sdim#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
74214640Sdim#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
75214640Sdim
76214640Sdim/*
77214640Sdim**  Register definitions for DMA controller 2 (channels 4..7):
78214640Sdim*/
79214640Sdim#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
80214640Sdim#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
81214640Sdim#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
82214640Sdim#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
83214640Sdim
84214640Sdimvoid config_isadev __P((struct isa_device *, u_int *));
85214640Sdim
86214640Sdim/*
87214640Sdim * print a conflict message
88214640Sdim */
89214640Sdimvoid
90214640Sdimconflict(dvp, tmpdvp, item, reason, format)
91214640Sdim	struct isa_device	*dvp, *tmpdvp;
92214640Sdim	int			item;
93214640Sdim	char			*reason;
94214640Sdim	char			*format;
95214640Sdim{
96214640Sdim	printf("%s%d not probed due to %s conflict with %s%d at ",
97214640Sdim		dvp->id_driver->name, dvp->id_unit, reason,
98214640Sdim		tmpdvp->id_driver->name, tmpdvp->id_unit);
99214640Sdim	printf(format, item);
100214640Sdim	printf("\n");
101214640Sdim}
102214640Sdim
103214640Sdim/*
104214640Sdim * Check to see if things are alread in use, like IRQ's, I/O addresses
105214640Sdim * and Memory addresses.
106214640Sdim */
107214640Sdimint
108214640Sdimhaveseen(dvp, tmpdvp)
109214640Sdim	struct	isa_device *dvp, *tmpdvp;
110214640Sdim{
111214640Sdim	int	status = 0;
112214640Sdim
113214640Sdim	/*
114214640Sdim	 * Only check against devices that have already been found
115214640Sdim	 */
116214640Sdim	if (tmpdvp->id_alive) {
117214640Sdim		/*
118214640Sdim		 * Check for I/O address conflict.  We can only check the
119214640Sdim		 * starting address of the device against the range of the
120214640Sdim		 * device that has already been probed since we do not
121214640Sdim		 * know how many I/O addresses this device uses.
122214640Sdim		 */
123214640Sdim		if (tmpdvp->id_alive != -1) {
124214640Sdim			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
125214640Sdim			    (dvp->id_iobase <=
126214640Sdim				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
127214640Sdim				conflict(dvp, tmpdvp, dvp->id_iobase,
128214640Sdim					 "I/O address", "0x%x");
129214640Sdim				status = 1;
130214640Sdim			}
131214640Sdim		}
132214640Sdim		/*
133214640Sdim		 * Check for Memory address conflict.  We can check for
134214640Sdim		 * range overlap, but it will not catch all cases since the
135214640Sdim		 * driver may adjust the msize paramater during probe, for
136214640Sdim		 * now we just check that the starting address does not
137214640Sdim		 * fall within any allocated region.
138214640Sdim		 * XXX could add a second check after the probe for overlap,
139214640Sdim		 * since at that time we would know the full range.
140214640Sdim		 * XXX KERNBASE is a hack, we should have vaddr in the table!
141214640Sdim		 */
142214640Sdim		if(tmpdvp->id_maddr) {
143214640Sdim			if((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
144214640Sdim			   (KERNBASE + dvp->id_maddr <=
145214640Sdim			   (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
146214640Sdim				conflict(dvp, tmpdvp, dvp->id_maddr, "maddr",
147214640Sdim					"0x%x");
148214640Sdim				status = 1;
149214640Sdim			}
150214640Sdim		}
151214640Sdim#ifndef COM_MULTIPORT
152214640Sdim		/*
153214640Sdim		 * Check for IRQ conflicts.
154214640Sdim		 */
155214640Sdim		if(tmpdvp->id_irq) {
156214640Sdim			if (tmpdvp->id_irq == dvp->id_irq) {
157214640Sdim				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
158214640Sdim					"irq", "%d");
159214640Sdim				status = 1;
160214640Sdim			}
161214640Sdim		}
162214640Sdim#endif
163214640Sdim		/*
164214640Sdim		 * Check for DRQ conflicts.
165214640Sdim		 */
166214640Sdim		if(tmpdvp->id_drq != -1) {
167214640Sdim			if (tmpdvp->id_drq == dvp->id_drq) {
168214640Sdim				conflict(dvp, tmpdvp, dvp->id_drq,
169214640Sdim					"drq", "%d");
170214640Sdim				status = 1;
171214640Sdim			}
172214640Sdim		}
173214640Sdim	}
174214640Sdim	return (status);
175214640Sdim}
176214640Sdim
177214640Sdim/*
178214640Sdim * Search through all the isa_devtab_* tables looking for anything that
179214640Sdim * conflicts with the current device.
180214640Sdim */
181214640Sdimint
182214640Sdimhaveseen_isadev(dvp)
183214640Sdim	struct isa_device *dvp;
184214640Sdim{
185214640Sdim	struct isa_device *tmpdvp;
186214640Sdim	int	status = 0;
187214640Sdim
188214640Sdim	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
189214640Sdim		status |= haveseen(dvp, tmpdvp);
190214640Sdim	}
191214640Sdim	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
192214640Sdim		status |= haveseen(dvp, tmpdvp);
193214640Sdim	}
194214640Sdim	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
195214640Sdim		status |= haveseen(dvp, tmpdvp);
196214640Sdim	}
197214640Sdim	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
198214640Sdim		status |= haveseen(dvp, tmpdvp);
199214640Sdim	}
200214640Sdim	return(status);
201214640Sdim}
202214640Sdim
203214640Sdim/*
204214640Sdim * Configure all ISA devices
205214640Sdim */
206214640Sdimvoid
207214640Sdimisa_configure() {
208214640Sdim	struct isa_device *dvp;
209214640Sdim
210214640Sdim	enable_intr();
211214640Sdim	splhigh();
212214640Sdim	INTREN(IRQ_SLAVE);
213214640Sdim	printf("Probing for devices on the ISA bus:\n");
214214640Sdim	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) {
215214640Sdim		if (!haveseen_isadev(dvp))
216214640Sdim			config_isadev(dvp,&tty_imask);
217214640Sdim	}
218214640Sdim	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) {
219214640Sdim		if (!haveseen_isadev(dvp))
220214640Sdim			config_isadev(dvp,&bio_imask);
221214640Sdim	}
222214640Sdim	for (dvp = isa_devtab_net; dvp->id_driver; dvp++) {
223214640Sdim		if (!haveseen_isadev(dvp))
224214640Sdim			config_isadev(dvp,&net_imask);
225214640Sdim	}
226214640Sdim	for (dvp = isa_devtab_null; dvp->id_driver; dvp++) {
227214640Sdim		if (!haveseen_isadev(dvp))
228214640Sdim			config_isadev(dvp,(u_int *) NULL);
229214640Sdim	}
230214640Sdim	bio_imask |= SWI_CLOCK_MASK;
231214640Sdim	net_imask |= SWI_NET_MASK;
232214640Sdim	tty_imask |= SWI_TTY_MASK;
233214640Sdim
234214640Sdim/*
235214640Sdim * XXX we should really add the tty device to net_imask when the line is
236214640Sdim * switched to SLIPDISC, and then remove it when it is switched away from
237214640Sdim * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
238214640Sdim * of them is running slip.
239214640Sdim *
240214640Sdim * XXX actually, blocking all ttys during a splimp doesn't matter so much
241214640Sdim * with sio because the serial interrupt layer doesn't use tty_imask.  Only
242214640Sdim * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
243214640Sdim * during spltty.
244214640Sdim */
245214640Sdim#include "sl.h"
246214640Sdim#if NSL > 0
247214640Sdim	net_imask |= tty_imask;
248214640Sdim	tty_imask = net_imask;
249214640Sdim#endif
250214640Sdim	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
251214640Sdim#ifdef DIAGNOSTIC
252214640Sdim	printf("bio_imask %x tty_imask %x net_imask %x\n",
253214640Sdim	       bio_imask, tty_imask, net_imask);
254214640Sdim#endif
255214640Sdim	splnone();
256214640Sdim}
257214640Sdim
258214640Sdim/*
259214640Sdim * Configure an ISA device.
260214640Sdim */
261214640Sdimvoid
262214640Sdimconfig_isadev(isdp, mp)
263214640Sdim	struct isa_device *isdp;
264214640Sdim	u_int *mp;
265214640Sdim{
266214640Sdim	struct isa_driver *dp = isdp->id_driver;
267214640Sdim
268214640Sdim	if (isdp->id_maddr) {
269214640Sdim		extern u_int atdevbase;
270214640Sdim
271214640Sdim		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
272214640Sdim		isdp->id_maddr += atdevbase;
273214640Sdim	}
274214640Sdim	isdp->id_alive = (*dp->probe)(isdp);
275214640Sdim	if (isdp->id_alive) {
276214640Sdim		/*
277214640Sdim		 * Only print the I/O address range if id_alive != -1
278214640Sdim		 * Right now this is a temporary fix just for the new
279214640Sdim		 * NPX code so that if it finds a 486 that can use trap
280214640Sdim		 * 16 it will not report I/O addresses.
281214640Sdim		 * Rod Grimes 04/26/94
282214640Sdim		 */
283214640Sdim		printf("%s%d", dp->name, isdp->id_unit);
284214640Sdim		if (isdp->id_alive != -1) {
285214640Sdim 			printf(" at 0x%x", isdp->id_iobase);
286214640Sdim 			if ((isdp->id_iobase + isdp->id_alive - 1) !=
287214640Sdim 			     isdp->id_iobase) {
288214640Sdim 				printf("-0x%x",
289214640Sdim				       isdp->id_iobase +
290214640Sdim				       isdp->id_alive - 1);
291214640Sdim			}
292214640Sdim		}
293214640Sdim		if(isdp->id_irq)
294214640Sdim			printf(" irq %d", ffs(isdp->id_irq) - 1);
295214640Sdim		if (isdp->id_drq != -1)
296214640Sdim			printf(" drq %d", isdp->id_drq);
297214640Sdim		if (isdp->id_maddr)
298214640Sdim			printf(" maddr 0x%x", kvtop(isdp->id_maddr));
299214640Sdim		if (isdp->id_msize)
300214640Sdim			printf(" msize %d", isdp->id_msize);
301214640Sdim		if (isdp->id_flags)
302214640Sdim			printf(" flags 0x%x", isdp->id_flags);
303214640Sdim		if (isdp->id_iobase) {
304214640Sdim			if (isdp->id_iobase < 0x100) {
305214640Sdim				printf(" on motherboard\n");
306214640Sdim			} else {
307214640Sdim				if (isdp->id_iobase >= 0x1000) {
308214640Sdim					printf (" on eisa\n");
309214640Sdim				} else {
310214640Sdim					printf (" on isa\n");
311214640Sdim				}
312214640Sdim			}
313214640Sdim		}
314214640Sdim
315214640Sdim		(*dp->attach)(isdp);
316214640Sdim
317214640Sdim		if(isdp->id_irq) {
318214640Sdim			int intrno;
319214640Sdim
320214640Sdim			intrno = ffs(isdp->id_irq)-1;
321214640Sdim			setidt(ICU_OFFSET+intrno, isdp->id_intr,
322214640Sdim				 SDT_SYS386IGT, SEL_KPL);
323214640Sdim			if(mp) {
324214640Sdim				INTRMASK(*mp,isdp->id_irq);
325214640Sdim			}
326214640Sdim			INTREN(isdp->id_irq);
327214640Sdim		}
328214640Sdim	} else {
329214640Sdim		printf("%s%d not found", dp->name, isdp->id_unit);
330214640Sdim		if (isdp->id_iobase) {
331214640Sdim			printf(" at 0x%x", isdp->id_iobase);
332214640Sdim		}
333214640Sdim		printf("\n");
334214640Sdim	}
335214640Sdim}
336214640Sdim
337214640Sdim#define	IDTVEC(name)	__CONCAT(X,name)
338214640Sdim/* default interrupt vector table entries */
339214640Sdimtypedef void inthand_t();
340214640Sdimtypedef void (*inthand_func_t)();
341214640Sdimextern inthand_t
342214640Sdim	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
343214640Sdim	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
344214640Sdim	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
345214640Sdim	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
346214640Sdim
347214640Sdimstatic inthand_func_t defvec[ICU_LEN] = {
348214640Sdim	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
349214640Sdim	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
350214640Sdim	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
351214640Sdim	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
352214640Sdim
353214640Sdim/*
354214640Sdim * Fill in default interrupt table (in case of spuruious interrupt
355214640Sdim * during configuration of kernel, setup interrupt control unit
356214640Sdim */
357214640Sdimvoid
358214640Sdimisa_defaultirq()
359214640Sdim{
360214640Sdim	int i;
361214640Sdim
362214640Sdim	/* icu vectors */
363214640Sdim	for (i = 0; i < ICU_LEN; i++)
364214640Sdim		setidt(ICU_OFFSET + i, defvec[i], SDT_SYS386IGT, SEL_KPL);
365214640Sdim
366214640Sdim	/* initialize 8259's */
367214640Sdim	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
368214640Sdim	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
369214640Sdim	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
370214640Sdim#ifdef AUTO_EOI_1
371214640Sdim	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
372214640Sdim#else
373214640Sdim	outb(IO_ICU1+1, 1);		/* 8086 mode */
374214640Sdim#endif
375214640Sdim	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
376214640Sdim	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
377214640Sdim	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
378214640Sdim
379214640Sdim	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
380214640Sdim	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
381214640Sdim	outb(IO_ICU2+1,2);		/* my slave id is 2 */
382214640Sdim#ifdef AUTO_EOI_2
383214640Sdim	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
384214640Sdim#else
385214640Sdim	outb(IO_ICU2+1,1);		/* 8086 mode */
386214640Sdim#endif
387214640Sdim	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
388214640Sdim	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
389214640Sdim}
390214640Sdim
391214640Sdim/* region of physical memory known to be contiguous */
392214640Sdimvm_offset_t isaphysmem;
393214640Sdimstatic caddr_t dma_bounce[8];		/* XXX */
394214640Sdimstatic char bounced[8];		/* XXX */
395214640Sdim#define MAXDMASZ 512		/* XXX */
396214640Sdim
397214640Sdim/* high byte of address is stored in this port for i-th dma channel */
398214640Sdimstatic short dmapageport[8] =
399214640Sdim	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
400214640Sdim
401214640Sdim/*
402214640Sdim * isa_dmacascade(): program 8237 DMA controller channel to accept
403214640Sdim * external dma control by a board.
404214640Sdim */
405214640Sdimvoid isa_dmacascade(unsigned chan)
406214640Sdim{
407214640Sdim	if (chan > 7)
408214640Sdim		panic("isa_dmacascade: impossible request");
409214640Sdim
410214640Sdim	/* set dma channel mode, and set dma channel mode */
411214640Sdim	if ((chan & 4) == 0) {
412214640Sdim		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
413214640Sdim		outb(DMA1_SMSK, chan);
414214640Sdim	} else {
415214640Sdim		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
416214640Sdim		outb(DMA2_SMSK, chan & 3);
417214640Sdim	}
418214640Sdim}
419214640Sdim
420214640Sdim/*
421214640Sdim * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
422214640Sdim * problems by using a bounce buffer.
423214640Sdim */
424214640Sdimvoid isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
425214640Sdim{	vm_offset_t phys;
426214640Sdim	int waport;
427214640Sdim	caddr_t newaddr;
428214640Sdim
429214640Sdim	if (    chan > 7
430214640Sdim	    || (chan < 4 && nbytes > (1<<16))
431214640Sdim	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
432214640Sdim		panic("isa_dmastart: impossible request");
433214640Sdim
434214640Sdim	if (isa_dmarangecheck(addr, nbytes, chan)) {
435214640Sdim		if (dma_bounce[chan] == 0)
436214640Sdim			dma_bounce[chan] =
437214640Sdim				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
438214640Sdim				(caddr_t) isaphysmem + NBPG*chan;
439214640Sdim		bounced[chan] = 1;
440214640Sdim		newaddr = dma_bounce[chan];
441214640Sdim		*(int *) newaddr = 0;	/* XXX */
442214640Sdim
443214640Sdim		/* copy bounce buffer on write */
444214640Sdim		if (!(flags & B_READ))
445214640Sdim			bcopy(addr, newaddr, nbytes);
446214640Sdim		addr = newaddr;
447214640Sdim	}
448214640Sdim
449214640Sdim	/* translate to physical */
450214640Sdim	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
451214640Sdim
452214640Sdim	if ((chan & 4) == 0) {
453214640Sdim		/*
454214640Sdim		 * Program one of DMA channels 0..3.  These are
455214640Sdim		 * byte mode channels.
456214640Sdim		 */
457214640Sdim		/* set dma channel mode, and reset address ff */
458214640Sdim		if (flags & B_READ)
459214640Sdim			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
460214640Sdim		else
461214640Sdim			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
462214640Sdim		outb(DMA1_FFC, 0);
463214640Sdim
464214640Sdim		/* send start address */
465214640Sdim		waport =  DMA1_CHN(chan);
466214640Sdim		outb(waport, phys);
467214640Sdim		outb(waport, phys>>8);
468214640Sdim		outb(dmapageport[chan], phys>>16);
469214640Sdim
470214640Sdim		/* send count */
471214640Sdim		outb(waport + 1, --nbytes);
472214640Sdim		outb(waport + 1, nbytes>>8);
473214640Sdim
474214640Sdim		/* unmask channel */
475214640Sdim		outb(DMA1_SMSK, chan);
476214640Sdim	} else {
477214640Sdim		/*
478214640Sdim		 * Program one of DMA channels 4..7.  These are
479214640Sdim		 * word mode channels.
480214640Sdim		 */
481214640Sdim		/* set dma channel mode, and reset address ff */
482214640Sdim		if (flags & B_READ)
483214640Sdim			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
484214640Sdim		else
485214640Sdim			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
486214640Sdim		outb(DMA2_FFC, 0);
487214640Sdim
488214640Sdim		/* send start address */
489214640Sdim		waport = DMA2_CHN(chan - 4);
490214640Sdim		outb(waport, phys>>1);
491214640Sdim		outb(waport, phys>>9);
492214640Sdim		outb(dmapageport[chan], phys>>16);
493214640Sdim
494214640Sdim		/* send count */
495214640Sdim		nbytes >>= 1;
496214640Sdim		outb(waport + 2, --nbytes);
497214640Sdim		outb(waport + 2, nbytes>>8);
498214640Sdim
499214640Sdim		/* unmask channel */
500214640Sdim		outb(DMA2_SMSK, chan & 3);
501214640Sdim	}
502214640Sdim}
503214640Sdim
504214640Sdimvoid isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
505214640Sdim{
506214640Sdim
507214640Sdim	/* copy bounce buffer on read */
508214640Sdim	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
509214640Sdim	if (bounced[chan]) {
510214640Sdim		bcopy(dma_bounce[chan], addr, nbytes);
511214640Sdim		bounced[chan] = 0;
512214640Sdim	}
513214640Sdim}
514214640Sdim
515214640Sdim/*
516214640Sdim * Check for problems with the address range of a DMA transfer
517214640Sdim * (non-contiguous physical pages, outside of bus address space,
518214640Sdim * crossing DMA page boundaries).
519214640Sdim * Return true if special handling needed.
520214640Sdim */
521214640Sdim
522214640Sdimint
523214640Sdimisa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
524214640Sdim	vm_offset_t phys, priorpage = 0, endva;
525214640Sdim	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
526214640Sdim
527214640Sdim	endva = (vm_offset_t)round_page(va + length);
528214640Sdim	for (; va < (caddr_t) endva ; va += NBPG) {
529214640Sdim		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
530214640Sdim#define ISARAM_END	RAM_END
531214640Sdim		if (phys == 0)
532214640Sdim			panic("isa_dmacheck: no physical page present");
533214640Sdim		if (phys >= ISARAM_END)
534214640Sdim			return (1);
535214640Sdim		if (priorpage) {
536214640Sdim			if (priorpage + NBPG != phys)
537214640Sdim				return (1);
538214640Sdim			/* check if crossing a DMA page boundary */
539214640Sdim			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
540214640Sdim				return (1);
541214640Sdim		}
542214640Sdim		priorpage = phys;
543214640Sdim	}
544214640Sdim	return (0);
545214640Sdim}
546214640Sdim
547214640Sdim/* head of queue waiting for physmem to become available */
548214640Sdimstruct buf isa_physmemq;
549214640Sdim
550214640Sdim/* blocked waiting for resource to become free for exclusive use */
551214640Sdimstatic isaphysmemflag;
552214640Sdim/* if waited for and call requested when free (B_CALL) */
553214640Sdimstatic void (*isaphysmemunblock)(); /* needs to be a list */
554214640Sdim
555214640Sdim/*
556214640Sdim * Allocate contiguous physical memory for transfer, returning
557214640Sdim * a *virtual* address to region. May block waiting for resource.
558214640Sdim * (assumed to be called at splbio())
559214640Sdim */
560214640Sdimcaddr_t
561214640Sdimisa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
562214640Sdim
563214640Sdim	isaphysmemunblock = func;
564214640Sdim	while (isaphysmemflag & B_BUSY) {
565214640Sdim		isaphysmemflag |= B_WANTED;
566214640Sdim		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
567214640Sdim	}
568214640Sdim	isaphysmemflag |= B_BUSY;
569214640Sdim
570214640Sdim	return((caddr_t)isaphysmem);
571214640Sdim}
572214640Sdim
573214640Sdim/*
574214640Sdim * Free contiguous physical memory used for transfer.
575214640Sdim * (assumed to be called at splbio())
576214640Sdim */
577214640Sdimvoid
578214640Sdimisa_freephysmem(caddr_t va, unsigned length) {
579214640Sdim
580214640Sdim	isaphysmemflag &= ~B_BUSY;
581214640Sdim	if (isaphysmemflag & B_WANTED) {
582214640Sdim		isaphysmemflag &= B_WANTED;
583214640Sdim		wakeup((caddr_t)&isaphysmemflag);
584214640Sdim		if (isaphysmemunblock)
585214640Sdim			(*isaphysmemunblock)();
586214640Sdim	}
587214640Sdim}
588214640Sdim
589214640Sdim/*
590214640Sdim * Handle a NMI, possibly a machine check.
591214640Sdim * return true to panic system, false to ignore.
592214640Sdim */
593214640Sdimint
594214640Sdimisa_nmi(cd)
595214640Sdim	int cd;
596214640Sdim{
597214640Sdim
598214640Sdim	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
599214640Sdim	return(0);
600214640Sdim}
601214640Sdim
602214640Sdim/*
603214640Sdim * Caught a stray interrupt, notify
604214640Sdim */
605214640Sdimvoid
606214640Sdimisa_strayintr(d)
607214640Sdim	int d;
608214640Sdim{
609214640Sdim
610214640Sdim	/* DON'T BOTHER FOR NOW! */
611214640Sdim	/* for some reason, we get bursts of intr #7, even if not enabled! */
612214640Sdim	/*
613214640Sdim	 * Well the reason you got bursts of intr #7 is because someone
614214640Sdim	 * raised an interrupt line and dropped it before the 8259 could
615214640Sdim	 * prioritize it.  This is documented in the intel data book.  This
616214640Sdim	 * means you have BAD hardware!  I have changed this so that only
617214640Sdim	 * the first 5 get logged, then it quits logging them, and puts
618214640Sdim	 * out a special message. rgrimes 3/25/1993
619214640Sdim	 */
620214640Sdim	extern u_long intrcnt_stray;
621214640Sdim
622214640Sdim	intrcnt_stray++;
623214640Sdim	if (intrcnt_stray <= 5)
624214640Sdim		log(LOG_ERR,"ISA strayintr %x\n", d);
625214640Sdim	if (intrcnt_stray == 5)
626214640Sdim		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
627214640Sdim}
628214640Sdim
629214640Sdim/*
630214640Sdim * Wait "n" microseconds.
631214640Sdim * Relies on timer 1 counting down from (TIMER_FREQ / hz) at
632214640Sdim * (1 * TIMER_FREQ) Hz.
633214640Sdim * Note: timer had better have been programmed before this is first used!
634214640Sdim * (The standard programming causes the timer to generate a square wave and
635214640Sdim * the counter is decremented twice every cycle.)
636214640Sdim */
637214640Sdim#define	CF		(1 * TIMER_FREQ)
638214640Sdim#define	TIMER_FREQ	1193182	/* XXX - should be elsewhere */
639214640Sdim
640214640Sdimvoid
641214640SdimDELAY(n)
642214640Sdim	int n;
643214640Sdim{
644214640Sdim	int counter_limit;
645214640Sdim	int prev_tick;
646214640Sdim	int tick;
647214640Sdim	int ticks_left;
648214640Sdim	int sec;
649214640Sdim	int usec;
650214640Sdim
651214640Sdim#ifdef DELAYDEBUG
652214640Sdim	int getit_calls = 1;
653214640Sdim	int n1;
654214640Sdim	static int state = 0;
655214640Sdim
656214640Sdim	if (state == 0) {
657214640Sdim		state = 1;
658214640Sdim		for (n1 = 1; n1 <= 10000000; n1 *= 10)
659214640Sdim			DELAY(n1);
660214640Sdim		state = 2;
661214640Sdim	}
662214640Sdim	if (state == 1)
663214640Sdim		printf("DELAY(%d)...", n);
664214640Sdim#endif
665214640Sdim
666214640Sdim	/*
667214640Sdim	 * Read the counter first, so that the rest of the setup overhead is
668214640Sdim	 * counted.  Guess the initial overhead is 20 usec (on most systems it
669214640Sdim	 * takes about 1.5 usec for each of the i/o's in getit().  The loop
670214640Sdim	 * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
671214640Sdim	 * multiplications and divisions to scale the count take a while).
672214640Sdim	 */
673214640Sdim	prev_tick = getit(0, 0);
674214640Sdim	n -= 20;
675214640Sdim
676214640Sdim	/*
677214640Sdim	 * Calculate (n * (CF / 1e6)) without using floating point and without
678214640Sdim	 * any avoidable overflows.
679214640Sdim	 */
680214640Sdim	sec = n / 1000000;
681214640Sdim	usec = n - sec * 1000000;
682214640Sdim	ticks_left = sec * CF
683214640Sdim		     + usec * (CF / 1000000)
684214640Sdim		     + usec * ((CF % 1000000) / 1000) / 1000
685214640Sdim		     + usec * (CF % 1000) / 1000000;
686214640Sdim
687214640Sdim	counter_limit = TIMER_FREQ / hz;
688214640Sdim	while (ticks_left > 0) {
689214640Sdim		tick = getit(0, 0);
690214640Sdim#ifdef DELAYDEBUG
691214640Sdim		++getit_calls;
692214640Sdim#endif
693214640Sdim		if (tick > prev_tick)
694214640Sdim			ticks_left -= prev_tick - (tick - counter_limit);
695214640Sdim		else
696214640Sdim			ticks_left -= prev_tick - tick;
697214640Sdim		prev_tick = tick;
698214640Sdim	}
699214640Sdim#ifdef DELAYDEBUG
700214640Sdim	if (state == 1)
701214640Sdim		printf(" %d calls to getit() at %d usec each\n",
702214640Sdim		       getit_calls, (n + 5) / getit_calls);
703214640Sdim#endif
704214640Sdim}
705214640Sdim
706214640Sdimint
707214640Sdimgetit(unit, timer)
708214640Sdim	int unit;
709214640Sdim	int timer;
710214640Sdim{
711214640Sdim	int high;
712214640Sdim	int low;
713214640Sdim
714214640Sdim	/*
715214640Sdim	 * XXX - isa.h defines bogus timers.  There's no such timer as
716214640Sdim	 * IO_TIMER_2 = 0x48.  There's a timer in the CMOS RAM chip but
717214640Sdim	 * its interface is quite different.  Neither timer is an 8252.
718214640Sdim	 * We actually only call this with unit = 0 and timer = 0.  It
719214640Sdim	 * could be static...
720214640Sdim	 */
721214640Sdim	/*
722214640Sdim	 * Protect ourself against interrupts.
723214640Sdim	 * XXX - sysbeep() and sysbeepstop() need protection.
724214640Sdim	 */
725214640Sdim	disable_intr();
726214640Sdim	/*
727214640Sdim	 * Latch the count for 'timer' (cc00xxxx, c = counter, x = any).
728214640Sdim	 */
729214640Sdim	outb(IO_TIMER1 + 3, timer << 6);
730214640Sdim
731214640Sdim	low = inb(IO_TIMER1 + timer);
732214640Sdim	high = inb(IO_TIMER1 + timer);
733214640Sdim	enable_intr();
734214640Sdim	return ((high << 8) | low);
735214640Sdim}
736214640Sdim
737214640Sdimstatic int beeping;
738214640Sdim
739214640Sdimstatic void
740214640Sdimsysbeepstop(f, dummy)
741214640Sdim	caddr_t f;
742214640Sdim	int dummy;
743214640Sdim{
744214640Sdim	/* disable counter 2 */
745214640Sdim	outb(0x61, inb(0x61) & 0xFC);
746214640Sdim	if (f)
747214640Sdim		timeout(sysbeepstop, (caddr_t)0, (int)f);
748214640Sdim	else
749214640Sdim		beeping = 0;
750214640Sdim}
751214640Sdim
752214640Sdimvoid
753214640Sdimsysbeep(int pitch, int period)
754214640Sdim{
755214640Sdim
756214640Sdim	outb(0x61, inb(0x61) | 3);	/* enable counter 2 */
757214640Sdim	/*
758214640Sdim	 * XXX - move timer stuff to clock.c.
759214640Sdim	 * Program counter 2:
760214640Sdim	 * ccaammmb, c counter, a = access, m = mode, b = BCD
761214640Sdim	 * 1011x110, 11 for aa = LSB then MSB, x11 for mmm = square wave.
762214640Sdim	 */
763214640Sdim	outb(0x43, 0xb6);	/* set command for counter 2, 2 byte write */
764214640Sdim
765214640Sdim	outb(0x42, pitch);
766214640Sdim	outb(0x42, (pitch>>8));
767214640Sdim
768214640Sdim	if (!beeping) {
769214640Sdim		beeping = period;
770214640Sdim		timeout(sysbeepstop, (caddr_t)(period/2), period);
771214640Sdim	}
772214640Sdim}
773214640Sdim
774214640Sdim/*
775214640Sdim * Pass command to keyboard controller (8042)
776214640Sdim */
777214640Sdimunsigned
778214640Sdimkbc_8042cmd(val)
779214640Sdim	int val;
780214640Sdim{
781214640Sdim
782214640Sdim	while (inb(KBSTATP)&KBS_IBF);
783214640Sdim	if (val) outb(KBCMDP, val);
784214640Sdim	while (inb(KBSTATP)&KBS_IBF);
785214640Sdim	return (inb(KBDATAP));
786214640Sdim}
787214640Sdim
788214640Sdim/*
789214640Sdim * find an ISA device in a given isa_devtab_* table, given
790214640Sdim * the table to search, the expected id_driver entry, and the unit number.
791214640Sdim *
792214640Sdim * this function is defined in isa_device.h, and this location is debatable;
793214640Sdim * i put it there because it's useless w/o, and directly operates on
794214640Sdim * the other stuff in that file.
795214640Sdim *
796214640Sdim */
797214640Sdim
798214640Sdimstruct isa_device *find_isadev(table, driverp, unit)
799214640Sdim     struct isa_device *table;
800214640Sdim     struct isa_driver *driverp;
801214640Sdim     int unit;
802214640Sdim{
803214640Sdim  if (driverp == NULL) /* sanity check */
804214640Sdim    return NULL;
805214640Sdim
806214640Sdim  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
807214640Sdim    if (table->id_driver == 0)
808214640Sdim      return NULL;
809214640Sdim
810214640Sdim    table++;
811214640Sdim  }
812214640Sdim
813214640Sdim  return table;
814214640Sdim}
815214640Sdim
816214640Sdim/*
817214640Sdim * Return nonzero if a (masked) irq is pending for a given device.
818214640Sdim */
819214640Sdimint
820214640Sdimisa_irq_pending(dvp)
821214640Sdim	struct isa_device *dvp;
822214640Sdim{
823214640Sdim	unsigned id_irq;
824214640Sdim
825214640Sdim	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
826214640Sdim	if (id_irq & 0xff)
827214640Sdim		return (inb(IO_ICU1) & id_irq);
828214640Sdim	return (inb(IO_ICU2) & (id_irq >> 8));
829214640Sdim}
830214640Sdim