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