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