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