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