isa.c revision 28487
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.2 1997/08/21 04:51:00 smp Exp smp $
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 <sys/param.h>
51#include <sys/systm.h>
52#include <sys/buf.h>
53#include <sys/malloc.h>
54#include <machine/ipl.h>
55#include <machine/md_var.h>
56#ifdef APIC_IO
57#include <machine/smp.h>
58#endif /* APIC_IO */
59#include <vm/vm.h>
60#include <vm/vm_param.h>
61#include <vm/pmap.h>
62#include <i386/isa/isa_device.h>
63#include <i386/isa/intr_machdep.h>
64#include <i386/isa/isa.h>
65#include <i386/isa/ic/i8237.h>
66
67#include <sys/interrupt.h>
68
69/*
70**  Register definitions for DMA controller 1 (channels 0..3):
71*/
72#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
73#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
74#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
75#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
76
77/*
78**  Register definitions for DMA controller 2 (channels 4..7):
79*/
80#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
81#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
82#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
83#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
84
85static void config_isadev __P((struct isa_device *isdp, u_int *mp));
86static void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
87				 int reconfig));
88static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
89			  int item, char const *whatnot, char const *reason,
90			  char const *format));
91static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
92			 u_int checkbits));
93static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
94
95/*
96 * print a conflict message
97 */
98static void
99conflict(dvp, tmpdvp, item, whatnot, reason, format)
100	struct isa_device	*dvp;
101	struct isa_device	*tmpdvp;
102	int			item;
103	char const		*whatnot;
104	char const		*reason;
105	char const		*format;
106{
107	printf("%s%d not %sed due to %s conflict with %s%d at ",
108		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
109		tmpdvp->id_driver->name, tmpdvp->id_unit);
110	printf(format, item);
111	printf("\n");
112}
113
114/*
115 * Check to see if things are already in use, like IRQ's, I/O addresses
116 * and Memory addresses.
117 */
118static int
119haveseen(dvp, tmpdvp, checkbits)
120	struct isa_device *dvp;
121	struct isa_device *tmpdvp;
122	u_int	checkbits;
123{
124	/*
125	 * Only check against devices that have already been found and are not
126	 * unilaterally allowed to conflict anyway.
127	 */
128	if (tmpdvp->id_alive && !dvp->id_conflicts) {
129		char const *whatnot;
130
131		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
132		/*
133		 * Check for I/O address conflict.  We can only check the
134		 * starting address of the device against the range of the
135		 * device that has already been probed since we do not
136		 * know how many I/O addresses this device uses.
137		 */
138		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
139			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
140			    (dvp->id_iobase <=
141				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
142				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
143					 "I/O address", "0x%x");
144				return 1;
145			}
146		}
147		/*
148		 * Check for Memory address conflict.  We can check for
149		 * range overlap, but it will not catch all cases since the
150		 * driver may adjust the msize paramater during probe, for
151		 * now we just check that the starting address does not
152		 * fall within any allocated region.
153		 * XXX could add a second check after the probe for overlap,
154		 * since at that time we would know the full range.
155		 * XXX KERNBASE is a hack, we should have vaddr in the table!
156		 */
157		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
158			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
159			    (KERNBASE + dvp->id_maddr <=
160			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
161				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
162					 whatnot, "maddr", "0x%x");
163				return 1;
164			}
165		}
166		/*
167		 * Check for IRQ conflicts.
168		 */
169		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
170			if (tmpdvp->id_irq == dvp->id_irq) {
171				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
172					 whatnot, "irq", "%d");
173				return 1;
174			}
175		}
176		/*
177		 * Check for DRQ conflicts.
178		 */
179		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
180			if (tmpdvp->id_drq == dvp->id_drq) {
181				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
182					 "drq", "%d");
183				return 1;
184			}
185		}
186	}
187	return 0;
188}
189
190#ifdef RESOURCE_CHECK
191#include <sys/drvresource.h>
192
193static int
194checkone (struct isa_device *dvp, int type, addr_t low, addr_t high,
195	  char *resname, char *resfmt, int attaching)
196{
197	int result = 0;
198	if (bootverbose) {
199		if (low == high)
200			printf("\tcheck %s: 0x%x\n", resname, low);
201		else
202			printf("\tcheck %s: 0x%x to 0x%x\n",
203			       resname, low, high);
204	}
205	if (resource_check(type, RESF_NONE, low, high) != NULL) {
206		char *whatnot = attaching ? "attach" : "prob";
207		static struct isa_device dummydev;
208		static struct isa_driver dummydrv;
209		struct isa_device *tmpdvp = &dummydev;
210
211		dummydev.id_driver = &dummydrv;
212		dummydev.id_unit = 0;
213		dummydrv.name = "pci";
214		conflict(dvp, tmpdvp, low, whatnot, resname, resfmt);
215		result = 1;
216	} else if (attaching) {
217		if (low == high)
218			printf("\tregister %s: 0x%x\n", resname, low);
219		else
220			printf("\tregister %s: 0x%x to 0x%x\n",
221			       resname, low, high);
222		resource_claim(dvp, type, RESF_NONE, low, high);
223	}
224	return (result);
225}
226
227static int
228check_pciconflict(struct isa_device *dvp, int checkbits)
229{
230	int result = 0;
231	int attaching = (checkbits & CC_ATTACH) != 0;
232
233	if (checkbits & CC_MEMADDR) {
234		long maddr = dvp->id_maddr;
235		long msize = dvp->id_msize;
236		if (msize > 0) {
237			if (checkone(dvp, REST_MEM, maddr, maddr + msize - 1,
238				     "maddr", "0x%x", attaching) != 0) {
239				result = 1;
240				attaching = 0;
241			}
242		}
243	}
244	if (checkbits & CC_IOADDR) {
245		unsigned iobase = dvp->id_iobase;
246		unsigned iosize = dvp->id_alive;
247		if (iosize == -1)
248			iosize = 1; /* XXX can't do much about this ... */
249		if (iosize > 0) {
250			if (checkone(dvp, REST_PORT, iobase, iobase + iosize -1,
251				     "I/O address", "0x%x", attaching) != 0) {
252				result = 1;
253				attaching = 0;
254			}
255		}
256	}
257	if (checkbits & CC_IRQ) {
258		int irq = ffs(dvp->id_irq) - 1;
259		if (irq >= 0) {
260			if (checkone(dvp, REST_INT, irq, irq,
261				     "irq", "%d", attaching) != 0) {
262				result = 1;
263				attaching = 0;
264			}
265		}
266	}
267	if (checkbits & CC_DRQ) {
268		int drq = dvp->id_drq;
269		if (drq >= 0) {
270			if (checkone(dvp, REST_DMA, drq, drq,
271				     "drq", "%d", attaching) != 0) {
272				result = 1;
273				attaching = 0;
274			}
275		}
276	}
277	if (result != 0)
278		resource_free (dvp);
279	return (result);
280}
281#endif /* RESOURCE_CHECK */
282
283/*
284 * Search through all the isa_devtab_* tables looking for anything that
285 * conflicts with the current device.
286 */
287int
288haveseen_isadev(dvp, checkbits)
289	struct isa_device *dvp;
290	u_int	checkbits;
291{
292	struct isa_device *tmpdvp;
293	int	status = 0;
294
295	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
296		status |= haveseen(dvp, tmpdvp, checkbits);
297		if (status)
298			return status;
299	}
300	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
301		status |= haveseen(dvp, tmpdvp, checkbits);
302		if (status)
303			return status;
304	}
305	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
306		status |= haveseen(dvp, tmpdvp, checkbits);
307		if (status)
308			return status;
309	}
310	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
311		status |= haveseen(dvp, tmpdvp, checkbits);
312		if (status)
313			return status;
314	}
315#ifdef RESOURCE_CHECK
316	if (!dvp->id_conflicts) {
317		status = check_pciconflict(dvp, checkbits);
318	} else if (bootverbose)
319		printf("\tnot checking for resource conflicts ...\n");
320	}
321#endif /* RESOURCE_CHECK */
322	return(status);
323}
324
325/*
326 * Configure all ISA devices
327 */
328void
329isa_configure() {
330	struct isa_device *dvp;
331
332	splhigh();
333	printf("Probing for devices on the ISA bus:\n");
334	/* First probe all the sensitive probes */
335	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
336		if (dvp->id_driver->sensitive_hw)
337			config_isadev(dvp, &tty_imask);
338	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
339		if (dvp->id_driver->sensitive_hw)
340			config_isadev(dvp, &bio_imask);
341	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
342		if (dvp->id_driver->sensitive_hw)
343			config_isadev(dvp, &net_imask);
344	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
345		if (dvp->id_driver->sensitive_hw)
346			config_isadev(dvp, (u_int *)NULL);
347
348	/* Then all the bad ones */
349	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
350		if (!dvp->id_driver->sensitive_hw)
351			config_isadev(dvp, &tty_imask);
352	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
353		if (!dvp->id_driver->sensitive_hw)
354			config_isadev(dvp, &bio_imask);
355	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
356		if (!dvp->id_driver->sensitive_hw)
357			config_isadev(dvp, &net_imask);
358	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
359		if (!dvp->id_driver->sensitive_hw)
360			config_isadev(dvp, (u_int *)NULL);
361
362	bio_imask |= SWI_CLOCK_MASK;
363	net_imask |= SWI_NET_MASK;
364	tty_imask |= SWI_TTY_MASK;
365
366/*
367 * XXX we should really add the tty device to net_imask when the line is
368 * switched to SLIPDISC, and then remove it when it is switched away from
369 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
370 * of them is running slip.
371 *
372 * XXX actually, blocking all ttys during a splimp doesn't matter so much
373 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
374 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
375 * during spltty.
376 */
377#include "sl.h"
378#if NSL > 0
379	net_imask |= tty_imask;
380	tty_imask = net_imask;
381#endif
382
383	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
384
385	if (bootverbose)
386		printf("imasks: bio %x, tty %x, net %x\n",
387		       bio_imask, tty_imask, net_imask);
388
389	/*
390	 * Finish initializing intr_mask[].  Note that the partly
391	 * constructed masks aren't actually used since we're at splhigh.
392	 * For fully dynamic initialization, register_intr() and
393	 * icu_unset() will have to adjust the masks for _all_
394	 * interrupts and for tty_imask, etc.
395	 */
396	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
397		register_imask(dvp, tty_imask);
398	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
399		register_imask(dvp, bio_imask);
400	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
401		register_imask(dvp, net_imask);
402	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
403		register_imask(dvp, SWI_CLOCK_MASK);
404	spl0();
405}
406
407/*
408 * Configure an ISA device.
409 */
410
411
412static void
413config_isadev(isdp, mp)
414	struct isa_device *isdp;
415	u_int *mp;
416{
417	config_isadev_c(isdp, mp, 0);
418}
419
420void
421reconfig_isadev(isdp, mp)
422	struct isa_device *isdp;
423	u_int *mp;
424{
425	config_isadev_c(isdp, mp, 1);
426}
427
428static void
429config_isadev_c(isdp, mp, reconfig)
430	struct isa_device *isdp;
431	u_int *mp;
432	int reconfig;
433{
434	u_int checkbits;
435	int id_alive;
436	int last_alive;
437	struct isa_driver *dp = isdp->id_driver;
438
439	if (!isdp->id_enabled) {
440		printf("%s%d: disabled, not probed.\n",
441			dp->name, isdp->id_unit);
442		return;
443	}
444	checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR;
445	if (!reconfig && haveseen_isadev(isdp, checkbits))
446		return;
447	if (!reconfig && isdp->id_maddr) {
448		isdp->id_maddr -= ISA_HOLE_START;
449		isdp->id_maddr += atdevbase;
450	}
451	if (reconfig) {
452		last_alive = isdp->id_alive;
453		isdp->id_reconfig = 1;
454	}
455	else {
456		last_alive = 0;
457		isdp->id_reconfig = 0;
458	}
459	id_alive = (*dp->probe)(isdp);
460	if (id_alive) {
461		/*
462		 * Only print the I/O address range if id_alive != -1
463		 * Right now this is a temporary fix just for the new
464		 * NPX code so that if it finds a 486 that can use trap
465		 * 16 it will not report I/O addresses.
466		 * Rod Grimes 04/26/94
467		 */
468		if (!isdp->id_reconfig) {
469			printf("%s%d", dp->name, isdp->id_unit);
470			if (id_alive != -1) {
471				if (isdp->id_iobase == -1)
472					printf(" at ?");
473				else {
474					printf(" at 0x%x", isdp->id_iobase);
475					if (isdp->id_iobase + id_alive - 1 !=
476					    isdp->id_iobase) {
477						printf("-0x%x",
478						       isdp->id_iobase + id_alive - 1);
479					}
480				}
481			}
482			if (isdp->id_irq)
483				printf(" irq %d", ffs(isdp->id_irq) - 1);
484			if (isdp->id_drq != -1)
485				printf(" drq %d", isdp->id_drq);
486			if (isdp->id_maddr)
487				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
488			if (isdp->id_msize)
489				printf(" msize %d", isdp->id_msize);
490			if (isdp->id_flags)
491				printf(" flags 0x%x", isdp->id_flags);
492			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
493				printf(" on motherboard");
494			} else if (isdp->id_iobase >= 0x1000 &&
495				    !(isdp->id_iobase & 0x300)) {
496				printf (" on eisa slot %d",
497					isdp->id_iobase >> 12);
498			} else {
499				printf (" on isa");
500			}
501			printf("\n");
502			/*
503			 * Check for conflicts again.  The driver may have
504			 * changed *dvp.  We should weaken the early check
505			 * since the driver may have been able to change
506			 * *dvp to avoid conflicts if given a chance.  We
507			 * already skip the early check for IRQs and force
508			 * a check for IRQs in the next group of checks.
509			 */
510			checkbits |= CC_IRQ;
511			if (haveseen_isadev(isdp, checkbits)) {
512				return;
513			}
514			isdp->id_alive = id_alive;
515		}
516		(*dp->attach)(isdp);
517		if (isdp->id_irq) {
518#ifdef APIC_IO
519			/*
520			 * Some motherboards use upper IRQs for traditional
521			 * ISA INTerrupt sources.  In particular we have
522			 * seen the secondary IDE connected to IRQ20.
523			 * This code detects and fixes this situation.
524			 */
525			u_int	apic_mask;
526			int	rirq;
527
528			apic_mask = isa_apic_mask(isdp->id_irq);
529			if (apic_mask != isdp->id_irq) {
530				rirq = ffs(isdp->id_irq) - 1;
531				isdp->id_irq = apic_mask;
532				undirect_isa_irq(rirq);	/* free for ISA */
533			}
534#endif /* APIC_IO */
535			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
536				      isdp->id_ri_flags, isdp->id_intr,
537				      mp, isdp->id_unit);
538		}
539	} else {
540		if (isdp->id_reconfig) {
541			(*dp->attach)(isdp); /* reconfiguration attach */
542		}
543		if (!last_alive) {
544			if (!isdp->id_reconfig) {
545				printf("%s%d not found",
546				       dp->name, isdp->id_unit);
547				if (isdp->id_iobase != -1)
548					printf(" at 0x%x", isdp->id_iobase);
549				printf("\n");
550			}
551		}
552		else {
553#if 0
554			/* This code has not been tested.... */
555			if (isdp->id_irq) {
556				icu_unset(ffs(isdp->id_irq) - 1,
557						isdp->id_intr);
558				if (mp)
559					INTRUNMASK(*mp, isdp->id_irq);
560			}
561#else
562			printf ("icu_unset() not supported here ...\n");
563#endif
564		}
565	}
566}
567
568static caddr_t	dma_bouncebuf[8];
569static u_int	dma_bouncebufsize[8];
570static u_int8_t	dma_bounced = 0;
571static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
572static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
573
574#define VALID_DMA_MASK (7)
575
576/* high byte of address is stored in this port for i-th dma channel */
577static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
578
579/*
580 * Setup a DMA channel's bounce buffer.
581 */
582void
583isa_dmainit(chan, bouncebufsize)
584	int chan;
585	u_int bouncebufsize;
586{
587	void *buf;
588
589#ifdef DIAGNOSTIC
590	if (chan & ~VALID_DMA_MASK)
591		panic("isa_dmainit: channel out of range");
592
593	if (dma_bouncebuf[chan] != NULL)
594		panic("isa_dmainit: impossible request");
595#endif
596
597	dma_bouncebufsize[chan] = bouncebufsize;
598
599	/* Try malloc() first.  It works better if it works. */
600	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
601	if (buf != NULL) {
602		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
603			dma_bouncebuf[chan] = buf;
604			return;
605		}
606		free(buf, M_DEVBUF);
607	}
608	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
609			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
610	if (buf == NULL)
611		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
612	else
613		dma_bouncebuf[chan] = buf;
614}
615
616/*
617 * Register a DMA channel's usage.  Usually called from a device driver
618 * in open() or during it's initialization.
619 */
620int
621isa_dma_acquire(chan)
622	int chan;
623{
624#ifdef DIAGNOSTIC
625	if (chan & ~VALID_DMA_MASK)
626		panic("isa_dma_acquire: channel out of range");
627#endif
628
629	if (dma_inuse & (1 << chan)) {
630		printf("isa_dma_acquire: channel %d already in use\n", chan);
631		return (EBUSY);
632	}
633	dma_inuse |= (1 << chan);
634
635	return (0);
636}
637
638/*
639 * Unregister a DMA channel's usage.  Usually called from a device driver
640 * during close() or during it's shutdown.
641 */
642void
643isa_dma_release(chan)
644	int chan;
645{
646#ifdef DIAGNOSTIC
647	if (chan & ~VALID_DMA_MASK)
648		panic("isa_dma_release: channel out of range");
649
650	if ((dma_inuse & (1 << chan)) == 0)
651		printf("isa_dma_release: channel %d not in use\n", chan);
652#endif
653
654	if (dma_busy & (1 << chan)) {
655		dma_busy &= ~(1 << chan);
656		/*
657		 * XXX We should also do "dma_bounced &= (1 << chan);"
658		 * because we are acting on behalf of isa_dmadone() which
659		 * was not called to end the last DMA operation.  This does
660		 * not matter now, but it may in the future.
661		 */
662	}
663
664	dma_inuse &= ~(1 << chan);
665}
666
667/*
668 * isa_dmacascade(): program 8237 DMA controller channel to accept
669 * external dma control by a board.
670 */
671void isa_dmacascade(chan)
672	int chan;
673{
674#ifdef DIAGNOSTIC
675	if (chan & ~VALID_DMA_MASK)
676		panic("isa_dmacascade: channel out of range");
677#endif
678
679	/* set dma channel mode, and set dma channel mode */
680	if ((chan & 4) == 0) {
681		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
682		outb(DMA1_SMSK, chan);
683	} else {
684		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
685		outb(DMA2_SMSK, chan & 3);
686	}
687}
688
689/*
690 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
691 * problems by using a bounce buffer.
692 */
693void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
694{
695	vm_offset_t phys;
696	int waport;
697	caddr_t newaddr;
698
699#ifdef DIAGNOSTIC
700	if (chan & ~VALID_DMA_MASK)
701		panic("isa_dmastart: channel out of range");
702
703	if ((chan < 4 && nbytes > (1<<16))
704	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
705		panic("isa_dmastart: impossible request");
706
707	if ((dma_inuse & (1 << chan)) == 0)
708		printf("isa_dmastart: channel %d not acquired\n", chan);
709#endif
710
711#if 0
712	/*
713	 * XXX This should be checked, but drivers like ad1848 only call
714	 * isa_dmastart() once because they use Auto DMA mode.  If we
715	 * leave this in, drivers that do this will print this continuously.
716	 */
717	if (dma_busy & (1 << chan))
718		printf("isa_dmastart: channel %d busy\n", chan);
719#endif
720
721	dma_busy |= (1 << chan);
722
723	if (isa_dmarangecheck(addr, nbytes, chan)) {
724		if (dma_bouncebuf[chan] == NULL
725		    || dma_bouncebufsize[chan] < nbytes)
726			panic("isa_dmastart: bad bounce buffer");
727		dma_bounced |= (1 << chan);
728		newaddr = dma_bouncebuf[chan];
729
730		/* copy bounce buffer on write */
731		if (!(flags & B_READ))
732			bcopy(addr, newaddr, nbytes);
733		addr = newaddr;
734	}
735
736	/* translate to physical */
737	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
738
739	if ((chan & 4) == 0) {
740		/*
741		 * Program one of DMA channels 0..3.  These are
742		 * byte mode channels.
743		 */
744		/* set dma channel mode, and reset address ff */
745
746		/* If B_RAW flag is set, then use autoinitialise mode */
747		if (flags & B_RAW) {
748		  if (flags & B_READ)
749			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
750		  else
751			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
752		}
753		else
754		if (flags & B_READ)
755			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
756		else
757			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
758		outb(DMA1_FFC, 0);
759
760		/* send start address */
761		waport =  DMA1_CHN(chan);
762		outb(waport, phys);
763		outb(waport, phys>>8);
764		outb(dmapageport[chan], phys>>16);
765
766		/* send count */
767		outb(waport + 1, --nbytes);
768		outb(waport + 1, nbytes>>8);
769
770		/* unmask channel */
771		outb(DMA1_SMSK, chan);
772	} else {
773		/*
774		 * Program one of DMA channels 4..7.  These are
775		 * word mode channels.
776		 */
777		/* set dma channel mode, and reset address ff */
778
779		/* If B_RAW flag is set, then use autoinitialise mode */
780		if (flags & B_RAW) {
781		  if (flags & B_READ)
782			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
783		  else
784			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
785		}
786		else
787		if (flags & B_READ)
788			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
789		else
790			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
791		outb(DMA2_FFC, 0);
792
793		/* send start address */
794		waport = DMA2_CHN(chan - 4);
795		outb(waport, phys>>1);
796		outb(waport, phys>>9);
797		outb(dmapageport[chan], phys>>16);
798
799		/* send count */
800		nbytes >>= 1;
801		outb(waport + 2, --nbytes);
802		outb(waport + 2, nbytes>>8);
803
804		/* unmask channel */
805		outb(DMA2_SMSK, chan & 3);
806	}
807}
808
809void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
810{
811#ifdef DIAGNOSTIC
812	if (chan & ~VALID_DMA_MASK)
813		panic("isa_dmadone: channel out of range");
814
815	if ((dma_inuse & (1 << chan)) == 0)
816		printf("isa_dmadone: channel %d not acquired\n", chan);
817#endif
818
819#if 0
820	/*
821	 * XXX This should be checked, but drivers like ad1848 only call
822	 * isa_dmastart() once because they use Auto DMA mode.  If we
823	 * leave this in, drivers that do this will print this continuously.
824	 */
825	if ((dma_busy & (1 << chan)) == 0)
826		printf("isa_dmadone: channel %d not busy\n", chan);
827#endif
828
829	if (dma_bounced & (1 << chan)) {
830		/* copy bounce buffer on read */
831		if (flags & B_READ)
832			bcopy(dma_bouncebuf[chan], addr, nbytes);
833
834		dma_bounced &= ~(1 << chan);
835	}
836	dma_busy &= ~(1 << chan);
837}
838
839/*
840 * Check for problems with the address range of a DMA transfer
841 * (non-contiguous physical pages, outside of bus address space,
842 * crossing DMA page boundaries).
843 * Return true if special handling needed.
844 */
845
846static int
847isa_dmarangecheck(caddr_t va, u_int length, int chan) {
848	vm_offset_t phys, priorpage = 0, endva;
849	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
850
851	endva = (vm_offset_t)round_page(va + length);
852	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
853		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
854#define ISARAM_END	RAM_END
855		if (phys == 0)
856			panic("isa_dmacheck: no physical page present");
857		if (phys >= ISARAM_END)
858			return (1);
859		if (priorpage) {
860			if (priorpage + PAGE_SIZE != phys)
861				return (1);
862			/* check if crossing a DMA page boundary */
863			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
864				return (1);
865		}
866		priorpage = phys;
867	}
868	return (0);
869}
870
871/*
872 * Query the progress of a transfer on a DMA channel.
873 *
874 * To avoid having to interrupt a transfer in progress, we sample
875 * each of the high and low databytes twice, and apply the following
876 * logic to determine the correct count.
877 *
878 * Reads are performed with interrupts disabled, thus it is to be
879 * expected that the time between reads is very small.  At most
880 * one rollover in the low count byte can be expected within the
881 * four reads that are performed.
882 *
883 * There are three gaps in which a rollover can occur :
884 *
885 * - read low1
886 *              gap1
887 * - read high1
888 *              gap2
889 * - read low2
890 *              gap3
891 * - read high2
892 *
893 * If a rollover occurs in gap1 or gap2, the low2 value will be
894 * greater than the low1 value.  In this case, low2 and high2 are a
895 * corresponding pair.
896 *
897 * In any other case, low1 and high1 can be considered to be correct.
898 *
899 * The function returns the number of bytes remaining in the transfer,
900 * or -1 if the channel requested is not active.
901 *
902 */
903int
904isa_dmastatus(int chan)
905{
906	u_long	cnt = 0;
907	int	ffport, waport;
908	u_long	low1, high1, low2, high2;
909	u_long	ef;
910
911	/* channel active? */
912	if ((dma_inuse & (1 << chan)) == 0) {
913		printf("isa_dmastatus: channel %d not active\n", chan);
914		return(-1);
915	}
916
917	/* still busy? */
918	if ((dma_busy & (1 << chan)) == 0) {
919		return(0);
920	}
921
922	if (chan < 4) {			/* low DMA controller */
923		ffport = DMA1_FFC;
924		waport = DMA1_CHN(chan) + 1;
925	} else {			/* high DMA controller */
926		ffport = DMA2_FFC;
927		waport = DMA2_CHN(chan - 4) + 2;
928	}
929
930	disable_intr();			/* no interrupts Mr Jones! */
931	outb(ffport, 0);		/* clear register LSB flipflop */
932	low1 = inb(waport);
933	high1 = inb(waport);
934	outb(ffport, 0);		/* clear again */
935	low2 = inb(waport);
936	high2 = inb(waport);
937	enable_intr();			/* enable interrupts again */
938
939	/*
940	 * Now decide if a wrap has tried to skew our results.
941	 * Note that after TC, the count will read 0xffff, while we want
942	 * to return zero, so we add and then mask to compensate.
943	 */
944	if (low1 >= low2) {
945		cnt = (low1 + (high1 << 8) + 1) & 0xffff;
946	} else {
947		cnt = (low2 + (high2 << 8) + 1) & 0xffff;
948	}
949
950	if (chan >= 4)			/* high channels move words */
951		cnt *= 2;
952	return(cnt);
953}
954
955/*
956 * Find the highest priority enabled display device.  Since we can't
957 * distinguish display devices from ttys, depend on display devices
958 * being sensitive and before sensitive non-display devices (if any)
959 * in isa_devtab_tty.
960 *
961 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
962 */
963struct isa_device *
964find_display()
965{
966	struct isa_device *dvp;
967
968	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
969		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
970			return (dvp);
971	return (NULL);
972}
973
974/*
975 * find an ISA device in a given isa_devtab_* table, given
976 * the table to search, the expected id_driver entry, and the unit number.
977 *
978 * this function is defined in isa_device.h, and this location is debatable;
979 * i put it there because it's useless w/o, and directly operates on
980 * the other stuff in that file.
981 *
982 */
983
984struct isa_device *find_isadev(table, driverp, unit)
985	struct isa_device *table;
986	struct isa_driver *driverp;
987	int unit;
988{
989	if (driverp == NULL) /* sanity check */
990		return (NULL);
991
992	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
993		if (table->id_driver == 0)
994			return NULL;
995
996		table++;
997	}
998
999	return (table);
1000}
1001
1002