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