2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
37 * $Id: isa.c,v 1.115 1998/10/13 08:24:38 dg Exp $
41 * code to manage AT bus
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
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <machine/ipl.h>
55 #include <machine/md_var.h>
57 #include <machine/smp.h>
60 #include <vm/vm_param.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>
67 #include <sys/interrupt.h>
71 #include <i386/isa/pnp.h>
75 ** Register definitions for DMA controller 1 (channels 0..3):
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 */
83 ** Register definitions for DMA controller 2 (channels 4..7):
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 */
90 static void config_isadev __P((struct isa_device *isdp, u_int *mp));
91 static void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
93 static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
94 int item, char const *whatnot, char const *reason,
96 static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
98 static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
101 * print a conflict message
104 conflict(dvp, tmpdvp, item, whatnot, reason, format)
105 struct isa_device *dvp;
106 struct isa_device *tmpdvp;
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);
120 * Check to see if things are already in use, like IRQ's, I/O addresses
121 * and Memory addresses.
124 haveseen(dvp, tmpdvp, checkbits)
125 struct isa_device *dvp;
126 struct isa_device *tmpdvp;
130 * Ignore all conflicts except IRQ ones if conflicts are allowed.
132 if (dvp->id_conflicts)
133 checkbits &= ~(CC_DRQ | CC_IOADDR | CC_MEMADDR);
135 * Only check against devices that have already been found.
137 if (tmpdvp->id_alive) {
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.
147 if (tmpdvp->id_driver == dvp->id_driver &&
148 tmpdvp->id_unit == dvp->id_unit) {
152 whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
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.
159 if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
160 if ((dvp->id_iobase >= tmpdvp->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");
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!
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,
191 * Check for IRQ conflicts.
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,
203 * Check for DRQ conflicts.
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");
217 #ifdef RESOURCE_CHECK
218 #include <sys/drvresource.h>
221 checkone (struct isa_device *dvp, int type, addr_t low, addr_t high,
222 char *resname, char *resfmt, int attaching)
227 printf("\tcheck %s: 0x%x\n", resname, low);
229 printf("\tcheck %s: 0x%x to 0x%x\n",
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;
238 dummydev.id_driver = &dummydrv;
239 dummydev.id_unit = 0;
240 dummydrv.name = "pci";
241 conflict(dvp, tmpdvp, low, whatnot, resname, resfmt);
243 } else if (attaching) {
245 printf("\tregister %s: 0x%x\n", resname, low);
247 printf("\tregister %s: 0x%x to 0x%x\n",
249 resource_claim(dvp, type, RESF_NONE, low, high);
255 check_pciconflict(struct isa_device *dvp, int checkbits)
258 int attaching = (checkbits & CC_ATTACH) != 0;
260 if (checkbits & CC_MEMADDR) {
261 long maddr = dvp->id_maddr;
262 long msize = dvp->id_msize;
264 if (checkone(dvp, REST_MEM, maddr, maddr + msize - 1,
265 "maddr", "0x%x", attaching) != 0) {
271 if (checkbits & CC_IOADDR) {
272 unsigned iobase = dvp->id_iobase;
273 unsigned iosize = dvp->id_alive;
275 iosize = 1; /* XXX can't do much about this ... */
277 if (checkone(dvp, REST_PORT, iobase, iobase + iosize -1,
278 "I/O address", "0x%x", attaching) != 0) {
284 if (checkbits & CC_IRQ) {
285 int irq = ffs(dvp->id_irq) - 1;
287 if (checkone(dvp, REST_INT, irq, irq,
288 "irq", "%d", attaching) != 0) {
294 if (checkbits & CC_DRQ) {
295 int drq = dvp->id_drq;
297 if (checkone(dvp, REST_DMA, drq, drq,
298 "drq", "%d", attaching) != 0) {
308 #endif /* RESOURCE_CHECK */
311 * Search through all the isa_devtab_* tables looking for anything that
312 * conflicts with the current device.
315 haveseen_isadev(dvp, checkbits)
316 struct isa_device *dvp;
320 struct pnp_dlist_node *nod;
322 struct isa_device *tmpdvp;
325 for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
326 status |= haveseen(dvp, tmpdvp, checkbits);
330 for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
331 status |= haveseen(dvp, tmpdvp, checkbits);
335 for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
336 status |= haveseen(dvp, tmpdvp, checkbits);
340 for (tmpdvp = isa_devtab_cam; tmpdvp->id_driver; tmpdvp++) {
341 status |= haveseen(dvp, tmpdvp, checkbits);
345 for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
346 status |= haveseen(dvp, tmpdvp, checkbits);
351 for (nod = pnp_device_list; nod != NULL; nod = nod->next)
352 if (status |= haveseen(dvp, &(nod->dev), checkbits))
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 */
365 * Configure all ISA devices
370 struct isa_device *dvp;
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);
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);
407 bio_imask |= SWI_CLOCK_MASK;
408 net_imask |= SWI_NET_MASK;
409 tty_imask |= SWI_TTY_MASK;
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.
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
424 net_imask |= tty_imask;
425 tty_imask = net_imask;
428 /* bio_imask |= tty_imask ; can some tty devices use buffers? */
431 printf("imasks: bio %x, tty %x, net %x\n",
432 bio_imask, tty_imask, net_imask);
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.
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);
454 * Configure an ISA device.
457 config_isadev(isdp, mp)
458 struct isa_device *isdp;
461 config_isadev_c(isdp, mp, 0);
465 reconfig_isadev(isdp, mp)
466 struct isa_device *isdp;
469 config_isadev_c(isdp, mp, 1);
473 config_isadev_c(isdp, mp, reconfig)
474 struct isa_device *isdp;
481 struct isa_driver *dp = isdp->id_driver;
483 if (!isdp->id_enabled) {
485 printf("%s%d: disabled, not probed.\n", dp->name, isdp->id_unit);
488 checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR;
489 if (!reconfig && haveseen_isadev(isdp, checkbits))
491 if (!reconfig && isdp->id_maddr) {
492 isdp->id_maddr -= ISA_HOLE_START;
493 isdp->id_maddr += atdevbase;
496 last_alive = isdp->id_alive;
497 isdp->id_reconfig = 1;
501 isdp->id_reconfig = 0;
503 id_alive = (*dp->probe)(isdp);
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
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)
518 printf(" at 0x%x", isdp->id_iobase);
519 if (isdp->id_iobase + id_alive - 1 !=
522 isdp->id_iobase + id_alive - 1);
527 printf(" irq %d", ffs(isdp->id_irq) - 1);
528 if (isdp->id_drq != -1)
529 printf(" drq %d", isdp->id_drq);
531 printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
533 printf(" msize %d", isdp->id_msize);
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);
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.
554 checkbits |= CC_ATTACH | CC_IRQ;
555 if (haveseen_isadev(isdp, checkbits))
557 isdp->id_alive = id_alive;
560 if (isdp->id_irq != 0 && isdp->id_intr == NULL)
561 printf("%s%d: irq with no handler\n",
562 dp->name, isdp->id_unit);
563 if (isdp->id_irq != 0 && isdp->id_intr != NULL) {
566 * Some motherboards use upper IRQs for traditional
567 * ISA INTerrupt sources. In particular we have
568 * seen the secondary IDE connected to IRQ20.
569 * This code detects and fixes this situation.
574 apic_mask = isa_apic_mask(isdp->id_irq);
575 if (apic_mask != isdp->id_irq) {
576 rirq = ffs(isdp->id_irq) - 1;
577 isdp->id_irq = apic_mask;
578 undirect_isa_irq(rirq); /* free for ISA */
581 register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
582 isdp->id_ri_flags, isdp->id_intr,
586 if (isdp->id_reconfig) {
587 (*dp->attach)(isdp); /* reconfiguration attach */
590 if (!isdp->id_reconfig) {
591 printf("%s%d not found",
592 dp->name, isdp->id_unit);
593 if (isdp->id_iobase != -1)
594 printf(" at 0x%x", isdp->id_iobase);
599 /* This code has not been tested.... */
600 if (isdp->id_irq != 0 && isdp->id_intr != NULL) {
601 icu_unset(ffs(isdp->id_irq) - 1,
604 INTRUNMASK(*mp, isdp->id_irq);
607 printf ("icu_unset() not supported here ...\n");
613 static caddr_t dma_bouncebuf[8];
614 static u_int dma_bouncebufsize[8];
615 static u_int8_t dma_bounced = 0;
616 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
617 static u_int8_t dma_inuse = 0; /* User for acquire/release */
618 static u_int8_t dma_auto_mode = 0;
620 #define VALID_DMA_MASK (7)
622 /* high byte of address is stored in this port for i-th dma channel */
623 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
626 * Setup a DMA channel's bounce buffer.
629 isa_dmainit(chan, bouncebufsize)
636 if (chan & ~VALID_DMA_MASK)
637 panic("isa_dmainit: channel out of range");
639 if (dma_bouncebuf[chan] != NULL)
640 panic("isa_dmainit: impossible request");
643 dma_bouncebufsize[chan] = bouncebufsize;
645 /* Try malloc() first. It works better if it works. */
646 buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
648 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
649 dma_bouncebuf[chan] = buf;
654 buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
655 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
657 printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
659 dma_bouncebuf[chan] = buf;
663 * Register a DMA channel's usage. Usually called from a device driver
664 * in open() or during its initialization.
667 isa_dma_acquire(chan)
671 if (chan & ~VALID_DMA_MASK)
672 panic("isa_dma_acquire: channel out of range");
675 if (dma_inuse & (1 << chan)) {
676 printf("isa_dma_acquire: channel %d already in use\n", chan);
679 dma_inuse |= (1 << chan);
680 dma_auto_mode &= ~(1 << chan);
686 * Unregister a DMA channel's usage. Usually called from a device driver
687 * during close() or during its shutdown.
690 isa_dma_release(chan)
694 if (chan & ~VALID_DMA_MASK)
695 panic("isa_dma_release: channel out of range");
697 if ((dma_inuse & (1 << chan)) == 0)
698 printf("isa_dma_release: channel %d not in use\n", chan);
701 if (dma_busy & (1 << chan)) {
702 dma_busy &= ~(1 << chan);
704 * XXX We should also do "dma_bounced &= (1 << chan);"
705 * because we are acting on behalf of isa_dmadone() which
706 * was not called to end the last DMA operation. This does
707 * not matter now, but it may in the future.
711 dma_inuse &= ~(1 << chan);
712 dma_auto_mode &= ~(1 << chan);
716 * isa_dmacascade(): program 8237 DMA controller channel to accept
717 * external dma control by a board.
724 if (chan & ~VALID_DMA_MASK)
725 panic("isa_dmacascade: channel out of range");
728 /* set dma channel mode, and set dma channel mode */
729 if ((chan & 4) == 0) {
730 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
731 outb(DMA1_SMSK, chan);
733 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
734 outb(DMA2_SMSK, chan & 3);
739 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
740 * problems by using a bounce buffer.
743 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
750 if (chan & ~VALID_DMA_MASK)
751 panic("isa_dmastart: channel out of range");
753 if ((chan < 4 && nbytes > (1<<16))
754 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
755 panic("isa_dmastart: impossible request");
757 if ((dma_inuse & (1 << chan)) == 0)
758 printf("isa_dmastart: channel %d not acquired\n", chan);
763 * XXX This should be checked, but drivers like ad1848 only call
764 * isa_dmastart() once because they use Auto DMA mode. If we
765 * leave this in, drivers that do this will print this continuously.
767 if (dma_busy & (1 << chan))
768 printf("isa_dmastart: channel %d busy\n", chan);
771 dma_busy |= (1 << chan);
773 if (isa_dmarangecheck(addr, nbytes, chan)) {
774 if (dma_bouncebuf[chan] == NULL
775 || dma_bouncebufsize[chan] < nbytes)
776 panic("isa_dmastart: bad bounce buffer");
777 dma_bounced |= (1 << chan);
778 newaddr = dma_bouncebuf[chan];
780 /* copy bounce buffer on write */
781 if (!(flags & B_READ))
782 bcopy(addr, newaddr, nbytes);
786 /* translate to physical */
787 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
790 dma_auto_mode |= (1 << chan);
792 dma_auto_mode &= ~(1 << chan);
795 if ((chan & 4) == 0) {
797 * Program one of DMA channels 0..3. These are
798 * byte mode channels.
800 /* set dma channel mode, and reset address ff */
802 /* If B_RAW flag is set, then use autoinitialise mode */
805 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
807 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
811 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
813 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
816 /* send start address */
817 waport = DMA1_CHN(chan);
819 outb(waport, phys>>8);
820 outb(dmapageport[chan], phys>>16);
823 outb(waport + 1, --nbytes);
824 outb(waport + 1, nbytes>>8);
827 outb(DMA1_SMSK, chan);
830 * Program one of DMA channels 4..7. These are
831 * word mode channels.
833 /* set dma channel mode, and reset address ff */
835 /* If B_RAW flag is set, then use autoinitialise mode */
838 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
840 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
844 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
846 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
849 /* send start address */
850 waport = DMA2_CHN(chan - 4);
851 outb(waport, phys>>1);
852 outb(waport, phys>>9);
853 outb(dmapageport[chan], phys>>16);
857 outb(waport + 2, --nbytes);
858 outb(waport + 2, nbytes>>8);
861 outb(DMA2_SMSK, chan & 3);
866 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
869 if (chan & ~VALID_DMA_MASK)
870 panic("isa_dmadone: channel out of range");
872 if ((dma_inuse & (1 << chan)) == 0)
873 printf("isa_dmadone: channel %d not acquired\n", chan);
876 if (((dma_busy & (1 << chan)) == 0) &&
877 (dma_auto_mode & (1 << chan)) == 0 )
878 printf("isa_dmadone: channel %d not busy\n", chan);
880 if ((dma_auto_mode & (1 << chan)) == 0)
881 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
883 if (dma_bounced & (1 << chan)) {
884 /* copy bounce buffer on read */
886 bcopy(dma_bouncebuf[chan], addr, nbytes);
888 dma_bounced &= ~(1 << chan);
890 dma_busy &= ~(1 << chan);
894 * Check for problems with the address range of a DMA transfer
895 * (non-contiguous physical pages, outside of bus address space,
896 * crossing DMA page boundaries).
897 * Return true if special handling needed.
901 isa_dmarangecheck(caddr_t va, u_int length, int chan)
903 vm_offset_t phys, priorpage = 0, endva;
904 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
906 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
907 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
908 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
909 #define ISARAM_END RAM_END
911 panic("isa_dmacheck: no physical page present");
912 if (phys >= ISARAM_END)
915 if (priorpage + PAGE_SIZE != phys)
917 /* check if crossing a DMA page boundary */
918 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
927 * Query the progress of a transfer on a DMA channel.
929 * To avoid having to interrupt a transfer in progress, we sample
930 * each of the high and low databytes twice, and apply the following
931 * logic to determine the correct count.
933 * Reads are performed with interrupts disabled, thus it is to be
934 * expected that the time between reads is very small. At most
935 * one rollover in the low count byte can be expected within the
936 * four reads that are performed.
938 * There are three gaps in which a rollover can occur :
948 * If a rollover occurs in gap1 or gap2, the low2 value will be
949 * greater than the low1 value. In this case, low2 and high2 are a
950 * corresponding pair.
952 * In any other case, low1 and high1 can be considered to be correct.
954 * The function returns the number of bytes remaining in the transfer,
955 * or -1 if the channel requested is not active.
959 isa_dmastatus(int chan)
963 u_long low1, high1, low2, high2;
965 /* channel active? */
966 if ((dma_inuse & (1 << chan)) == 0) {
967 printf("isa_dmastatus: channel %d not active\n", chan);
972 if (((dma_busy & (1 << chan)) == 0) &&
973 (dma_auto_mode & (1 << chan)) == 0 ) {
974 printf("chan %d not busy\n", chan);
977 if (chan < 4) { /* low DMA controller */
979 waport = DMA1_CHN(chan) + 1;
980 } else { /* high DMA controller */
982 waport = DMA2_CHN(chan - 4) + 2;
985 disable_intr(); /* no interrupts Mr Jones! */
986 outb(ffport, 0); /* clear register LSB flipflop */
989 outb(ffport, 0); /* clear again */
992 enable_intr(); /* enable interrupts again */
995 * Now decide if a wrap has tried to skew our results.
996 * Note that after TC, the count will read 0xffff, while we want
997 * to return zero, so we add and then mask to compensate.
1000 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
1002 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
1005 if (chan >= 4) /* high channels move words */
1011 * Stop a DMA transfer currently in progress.
1014 isa_dmastop(int chan)
1016 if ((dma_inuse & (1 << chan)) == 0)
1017 printf("isa_dmastop: channel %d not acquired\n", chan);
1019 if (((dma_busy & (1 << chan)) == 0) &&
1020 ((dma_auto_mode & (1 << chan)) == 0)) {
1021 printf("chan %d not busy\n", chan);
1025 if ((chan & 4) == 0) {
1026 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
1028 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
1030 return(isa_dmastatus(chan));
1034 * Find the highest priority enabled display device. Since we can't
1035 * distinguish display devices from ttys, depend on display devices
1036 * being sensitive and before sensitive non-display devices (if any)
1037 * in isa_devtab_tty.
1039 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
1044 struct isa_device *dvp;
1046 for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
1047 if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
1053 * find an ISA device in a given isa_devtab_* table, given
1054 * the table to search, the expected id_driver entry, and the unit number.
1056 * this function is defined in isa_device.h, and this location is debatable;
1057 * i put it there because it's useless w/o, and directly operates on
1058 * the other stuff in that file.
1063 find_isadev(table, driverp, unit)
1064 struct isa_device *table;
1065 struct isa_driver *driverp;
1068 if (driverp == NULL) /* sanity check */
1071 while ((table->id_driver != driverp) || (table->id_unit != unit)) {
1072 if (table->id_driver == 0)