isa.c revision 26949
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/06/25 20:12:23 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/syslog.h>
54#include <sys/malloc.h>
55#include <machine/ipl.h>
56#include <machine/md_var.h>
57#include <machine/segments.h>
58#ifdef 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#ifdef 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 = 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			if (!isdp->id_conflicts
541			    || (!intr_registered(ffs(isdp->id_irq) - 1)
542			    && isdp->id_intr != NULL))
543				register_intr(ffs(isdp->id_irq) - 1,
544					      isdp->id_id,
545					      isdp->id_ri_flags,
546					      isdp->id_intr,
547					      mp, isdp->id_unit);
548		}
549	} else {
550		if (isdp->id_reconfig) {
551			(*dp->attach)(isdp); /* reconfiguration attach */
552		}
553		if (!last_alive) {
554			if (!isdp->id_reconfig) {
555				printf("%s%d not found",
556				       dp->name, isdp->id_unit);
557				if (isdp->id_iobase != -1)
558					printf(" at 0x%x", isdp->id_iobase);
559				printf("\n");
560			}
561		}
562		else {
563#if 0
564			/* This code has not been tested.... */
565			if (isdp->id_irq) {
566				icu_unset(ffs(isdp->id_irq) - 1,
567						isdp->id_intr);
568				if (mp)
569					INTRUNMASK(*mp, isdp->id_irq);
570			}
571#else
572			printf ("icu_unset() not supported here ...\n");
573#endif
574		}
575	}
576}
577
578static caddr_t	dma_bouncebuf[8];
579static u_int	dma_bouncebufsize[8];
580static u_int8_t	dma_bounced = 0;
581static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
582static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
583
584#define VALID_DMA_MASK (7)
585
586/* high byte of address is stored in this port for i-th dma channel */
587static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
588
589/*
590 * Setup a DMA channel's bounce buffer.
591 */
592void
593isa_dmainit(chan, bouncebufsize)
594	int chan;
595	u_int bouncebufsize;
596{
597	void *buf;
598
599#ifdef DIAGNOSTIC
600	if (chan & ~VALID_DMA_MASK)
601		panic("isa_dmainit: channel out of range");
602
603	if (dma_bouncebuf[chan] != NULL)
604		panic("isa_dmainit: impossible request");
605#endif
606
607	dma_bouncebufsize[chan] = bouncebufsize;
608
609	/* Try malloc() first.  It works better if it works. */
610	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
611	if (buf != NULL) {
612		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
613			dma_bouncebuf[chan] = buf;
614			return;
615		}
616		free(buf, M_DEVBUF);
617	}
618	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
619			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
620	if (buf == NULL)
621		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
622	else
623		dma_bouncebuf[chan] = buf;
624}
625
626/*
627 * Register a DMA channel's usage.  Usually called from a device driver
628 * in open() or during it's initialization.
629 */
630int
631isa_dma_acquire(chan)
632	int chan;
633{
634#ifdef DIAGNOSTIC
635	if (chan & ~VALID_DMA_MASK)
636		panic("isa_dma_acquire: channel out of range");
637#endif
638
639	if (dma_inuse & (1 << chan)) {
640		printf("isa_dma_acquire: channel %d already in use\n", chan);
641		return (EBUSY);
642	}
643	dma_inuse |= (1 << chan);
644
645	return (0);
646}
647
648/*
649 * Unregister a DMA channel's usage.  Usually called from a device driver
650 * during close() or during it's shutdown.
651 */
652void
653isa_dma_release(chan)
654	int chan;
655{
656#ifdef DIAGNOSTIC
657	if (chan & ~VALID_DMA_MASK)
658		panic("isa_dma_release: channel out of range");
659
660	if (dma_inuse & (1 << chan) == 0)
661		printf("isa_dma_release: channel %d not in use\n", chan);
662#endif
663
664	if (dma_busy & (1 << chan)) {
665		dma_busy &= ~(1 << chan);
666		/*
667		 * XXX We should also do "dma_bounced &= (1 << chan);"
668		 * because we are acting on behalf of isa_dmadone() which
669		 * was not called to end the last DMA operation.  This does
670		 * not matter now, but it may in the future.
671		 */
672	}
673
674	dma_inuse &= ~(1 << chan);
675}
676
677/*
678 * isa_dmacascade(): program 8237 DMA controller channel to accept
679 * external dma control by a board.
680 */
681void isa_dmacascade(chan)
682	int chan;
683{
684#ifdef DIAGNOSTIC
685	if (chan & ~VALID_DMA_MASK)
686		panic("isa_dmacascade: channel out of range");
687#endif
688
689	/* set dma channel mode, and set dma channel mode */
690	if ((chan & 4) == 0) {
691		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
692		outb(DMA1_SMSK, chan);
693	} else {
694		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
695		outb(DMA2_SMSK, chan & 3);
696	}
697}
698
699/*
700 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
701 * problems by using a bounce buffer.
702 */
703void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
704{
705	vm_offset_t phys;
706	int waport;
707	caddr_t newaddr;
708
709#ifdef DIAGNOSTIC
710	if (chan & ~VALID_DMA_MASK)
711		panic("isa_dmastart: channel out of range");
712
713	if ((chan < 4 && nbytes > (1<<16))
714	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
715		panic("isa_dmastart: impossible request");
716
717	if (dma_inuse & (1 << chan) == 0)
718		printf("isa_dmastart: channel %d not acquired\n", chan);
719#endif
720
721#if 0
722	/*
723	 * XXX This should be checked, but drivers like ad1848 only call
724	 * isa_dmastart() once because they use Auto DMA mode.  If we
725	 * leave this in, drivers that do this will print this continuously.
726	 */
727	if (dma_busy & (1 << chan))
728		printf("isa_dmastart: channel %d busy\n", chan);
729#endif
730
731	dma_busy |= (1 << chan);
732
733	if (isa_dmarangecheck(addr, nbytes, chan)) {
734		if (dma_bouncebuf[chan] == NULL
735		    || dma_bouncebufsize[chan] < nbytes)
736			panic("isa_dmastart: bad bounce buffer");
737		dma_bounced |= (1 << chan);
738		newaddr = dma_bouncebuf[chan];
739
740		/* copy bounce buffer on write */
741		if (!(flags & B_READ))
742			bcopy(addr, newaddr, nbytes);
743		addr = newaddr;
744	}
745
746	/* translate to physical */
747	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
748
749	if ((chan & 4) == 0) {
750		/*
751		 * Program one of DMA channels 0..3.  These are
752		 * byte mode channels.
753		 */
754		/* set dma channel mode, and reset address ff */
755
756		/* If B_RAW flag is set, then use autoinitialise mode */
757		if (flags & B_RAW) {
758		  if (flags & B_READ)
759			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
760		  else
761			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
762		}
763		else
764		if (flags & B_READ)
765			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
766		else
767			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
768		outb(DMA1_FFC, 0);
769
770		/* send start address */
771		waport =  DMA1_CHN(chan);
772		outb(waport, phys);
773		outb(waport, phys>>8);
774		outb(dmapageport[chan], phys>>16);
775
776		/* send count */
777		outb(waport + 1, --nbytes);
778		outb(waport + 1, nbytes>>8);
779
780		/* unmask channel */
781		outb(DMA1_SMSK, chan);
782	} else {
783		/*
784		 * Program one of DMA channels 4..7.  These are
785		 * word mode channels.
786		 */
787		/* set dma channel mode, and reset address ff */
788
789		/* If B_RAW flag is set, then use autoinitialise mode */
790		if (flags & B_RAW) {
791		  if (flags & B_READ)
792			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
793		  else
794			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
795		}
796		else
797		if (flags & B_READ)
798			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
799		else
800			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
801		outb(DMA2_FFC, 0);
802
803		/* send start address */
804		waport = DMA2_CHN(chan - 4);
805		outb(waport, phys>>1);
806		outb(waport, phys>>9);
807		outb(dmapageport[chan], phys>>16);
808
809		/* send count */
810		nbytes >>= 1;
811		outb(waport + 2, --nbytes);
812		outb(waport + 2, nbytes>>8);
813
814		/* unmask channel */
815		outb(DMA2_SMSK, chan & 3);
816	}
817}
818
819void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
820{
821#ifdef DIAGNOSTIC
822	if (chan & ~VALID_DMA_MASK)
823		panic("isa_dmadone: channel out of range");
824
825	if (dma_inuse & (1 << chan) == 0)
826		printf("isa_dmadone: channel %d not acquired\n", chan);
827#endif
828
829#if 0
830	/*
831	 * XXX This should be checked, but drivers like ad1848 only call
832	 * isa_dmastart() once because they use Auto DMA mode.  If we
833	 * leave this in, drivers that do this will print this continuously.
834	 */
835	if (dma_busy & (1 << chan) == 0)
836		printf("isa_dmadone: channel %d not busy\n", chan);
837#endif
838
839	if (dma_bounced & (1 << chan)) {
840		/* copy bounce buffer on read */
841		if (flags & B_READ)
842			bcopy(dma_bouncebuf[chan], addr, nbytes);
843
844		dma_bounced &= ~(1 << chan);
845	}
846	dma_busy &= ~(1 << chan);
847}
848
849/*
850 * Check for problems with the address range of a DMA transfer
851 * (non-contiguous physical pages, outside of bus address space,
852 * crossing DMA page boundaries).
853 * Return true if special handling needed.
854 */
855
856static int
857isa_dmarangecheck(caddr_t va, u_int length, int chan) {
858	vm_offset_t phys, priorpage = 0, endva;
859	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
860
861	endva = (vm_offset_t)round_page(va + length);
862	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
863		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
864#define ISARAM_END	RAM_END
865		if (phys == 0)
866			panic("isa_dmacheck: no physical page present");
867		if (phys >= ISARAM_END)
868			return (1);
869		if (priorpage) {
870			if (priorpage + PAGE_SIZE != phys)
871				return (1);
872			/* check if crossing a DMA page boundary */
873			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
874				return (1);
875		}
876		priorpage = phys;
877	}
878	return (0);
879}
880
881/*
882 * Find the highest priority enabled display device.  Since we can't
883 * distinguish display devices from ttys, depend on display devices
884 * being sensitive and before sensitive non-display devices (if any)
885 * in isa_devtab_tty.
886 *
887 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
888 */
889struct isa_device *
890find_display()
891{
892	struct isa_device *dvp;
893
894	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
895		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
896			return (dvp);
897	return (NULL);
898}
899
900/*
901 * find an ISA device in a given isa_devtab_* table, given
902 * the table to search, the expected id_driver entry, and the unit number.
903 *
904 * this function is defined in isa_device.h, and this location is debatable;
905 * i put it there because it's useless w/o, and directly operates on
906 * the other stuff in that file.
907 *
908 */
909
910struct isa_device *find_isadev(table, driverp, unit)
911	struct isa_device *table;
912	struct isa_driver *driverp;
913	int unit;
914{
915	if (driverp == NULL) /* sanity check */
916		return (NULL);
917
918	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
919		if (table->id_driver == 0)
920			return NULL;
921
922		table++;
923	}
924
925	return (table);
926}
927
928