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