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