Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.26
      1  1.26   thorpej /*	$NetBSD: isadma.c,v 1.26 1997/06/06 23:43:55 thorpej Exp $	*/
      2  1.26   thorpej 
      3  1.26   thorpej /*-
      4  1.26   thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.26   thorpej  * All rights reserved.
      6  1.26   thorpej  *
      7  1.26   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.26   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.26   thorpej  * NASA Ames Research Center.
     10  1.26   thorpej  *
     11  1.26   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.26   thorpej  * modification, are permitted provided that the following conditions
     13  1.26   thorpej  * are met:
     14  1.26   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.26   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.26   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.26   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.26   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.26   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.26   thorpej  *    must display the following acknowledgement:
     21  1.26   thorpej  *	This product includes software developed by the NetBSD
     22  1.26   thorpej  *	Foundation, Inc. and its contributors.
     23  1.26   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.26   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.26   thorpej  *    from this software without specific prior written permission.
     26  1.26   thorpej  *
     27  1.26   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.26   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.26   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.26   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.26   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.26   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.26   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.26   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.26   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.26   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.26   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.26   thorpej  */
     39  1.26   thorpej 
     40  1.26   thorpej /*
     41  1.26   thorpej  * Device driver for the ISA on-board DMA controller.
     42  1.26   thorpej  */
     43   1.8       cgd 
     44   1.3   mycroft #include <sys/param.h>
     45   1.3   mycroft #include <sys/systm.h>
     46  1.19  christos #include <sys/proc.h>
     47  1.26   thorpej #include <sys/device.h>
     48   1.5   mycroft 
     49   1.3   mycroft #include <vm/vm.h>
     50   1.3   mycroft 
     51  1.26   thorpej #include <machine/bus.h>
     52   1.3   mycroft 
     53  1.12       cgd #include <dev/isa/isareg.h>
     54  1.26   thorpej #include <dev/isa/isavar.h>
     55  1.12       cgd #include <dev/isa/isadmavar.h>
     56  1.12       cgd #include <dev/isa/isadmareg.h>
     57   1.1   mycroft 
     58  1.26   thorpej /*
     59  1.26   thorpej  * High byte of DMA address is stored in this DMAPG register for
     60  1.26   thorpej  * the Nth DMA channel.
     61  1.26   thorpej  */
     62  1.18   mycroft static int dmapageport[2][4] = {
     63  1.26   thorpej 	{0x7, 0x3, 0x1, 0x2},
     64  1.26   thorpej 	{0xf, 0xb, 0x9, 0xa}
     65  1.15   mycroft };
     66  1.15   mycroft 
     67  1.15   mycroft static u_int8_t dmamode[4] = {
     68  1.17   mycroft 	DMA37MD_READ | DMA37MD_SINGLE,
     69  1.15   mycroft 	DMA37MD_WRITE | DMA37MD_SINGLE,
     70  1.25   mycroft 	DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
     71  1.25   mycroft 	DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP
     72  1.15   mycroft };
     73   1.1   mycroft 
     74  1.26   thorpej static inline void isa_dmaunmask __P((struct isa_softc *, int));
     75  1.26   thorpej static inline void isa_dmamask __P((struct isa_softc *, int));
     76  1.19  christos 
     77  1.26   thorpej static inline void
     78  1.26   thorpej isa_dmaunmask(sc, chan)
     79  1.26   thorpej 	struct isa_softc *sc;
     80  1.23   mycroft 	int chan;
     81  1.23   mycroft {
     82  1.23   mycroft 	int ochan = chan & 3;
     83  1.23   mycroft 
     84  1.23   mycroft 	/* set dma channel mode, and set dma channel mode */
     85  1.23   mycroft 	if ((chan & 4) == 0)
     86  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
     87  1.26   thorpej 		    DMA1_SMSK, ochan | DMA37SM_CLEAR);
     88  1.23   mycroft 	else
     89  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
     90  1.26   thorpej 		    DMA2_SMSK, ochan | DMA37SM_CLEAR);
     91  1.23   mycroft }
     92  1.23   mycroft 
     93  1.26   thorpej static inline void
     94  1.26   thorpej isa_dmamask(sc, chan)
     95  1.26   thorpej 	struct isa_softc *sc;
     96  1.23   mycroft 	int chan;
     97  1.23   mycroft {
     98  1.23   mycroft 	int ochan = chan & 3;
     99  1.23   mycroft 
    100  1.23   mycroft 	/* set dma channel mode, and set dma channel mode */
    101  1.23   mycroft 	if ((chan & 4) == 0) {
    102  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    103  1.26   thorpej 		    DMA1_SMSK, ochan | DMA37SM_SET);
    104  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    105  1.26   thorpej 		    DMA1_FFC, 0);
    106  1.23   mycroft 	} else {
    107  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    108  1.26   thorpej 		    DMA2_SMSK, ochan | DMA37SM_SET);
    109  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    110  1.26   thorpej 		    DMA2_FFC, 0);
    111  1.23   mycroft 	}
    112  1.23   mycroft }
    113  1.23   mycroft 
    114   1.1   mycroft /*
    115   1.5   mycroft  * isa_dmacascade(): program 8237 DMA controller channel to accept
    116   1.1   mycroft  * external dma control by a board.
    117   1.1   mycroft  */
    118   1.1   mycroft void
    119  1.26   thorpej isa_dmacascade(isadev, chan)
    120  1.26   thorpej 	struct device *isadev;
    121   1.4   mycroft 	int chan;
    122   1.1   mycroft {
    123  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    124  1.23   mycroft 	int ochan = chan & 3;
    125   1.4   mycroft 
    126  1.26   thorpej 	if (chan < 0 || chan > 7) {
    127  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    128  1.26   thorpej 		goto lose;
    129  1.26   thorpej 	}
    130  1.26   thorpej 
    131  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan) == 0) {
    132  1.26   thorpej 		printf("%s: DRQ %d is not free\n", sc->sc_dev.dv_xname, chan);
    133  1.26   thorpej 		goto lose;
    134  1.26   thorpej 	}
    135  1.26   thorpej 
    136  1.26   thorpej 	ISA_DRQ_ALLOC(sc, chan);
    137   1.1   mycroft 
    138   1.1   mycroft 	/* set dma channel mode, and set dma channel mode */
    139  1.23   mycroft 	if ((chan & 4) == 0)
    140  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    141  1.26   thorpej 		    DMA1_MODE, ochan | DMA37MD_CASCADE);
    142  1.26   thorpej 	else
    143  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    144  1.26   thorpej 		    DMA2_MODE, ochan | DMA37MD_CASCADE);
    145  1.26   thorpej 
    146  1.26   thorpej 	isa_dmaunmask(sc, chan);
    147  1.26   thorpej 	return;
    148  1.26   thorpej 
    149  1.26   thorpej  lose:
    150  1.26   thorpej 	panic("isa_dmacascade");
    151  1.26   thorpej }
    152  1.26   thorpej 
    153  1.26   thorpej int
    154  1.26   thorpej isa_dmamap_create(isadev, chan, size, flags)
    155  1.26   thorpej 	struct device *isadev;
    156  1.26   thorpej 	int chan;
    157  1.26   thorpej 	bus_size_t size;
    158  1.26   thorpej 	int flags;
    159  1.26   thorpej {
    160  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    161  1.26   thorpej 	bus_size_t maxsize;
    162  1.26   thorpej 
    163  1.26   thorpej 	if (chan < 0 || chan > 7) {
    164  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    165  1.26   thorpej 		goto lose;
    166  1.26   thorpej 	}
    167  1.26   thorpej 
    168  1.26   thorpej 	if (chan & 4)
    169  1.26   thorpej 		maxsize = (1 << 17);
    170  1.23   mycroft 	else
    171  1.26   thorpej 		maxsize = (1 << 16);
    172  1.26   thorpej 
    173  1.26   thorpej 	if (size > maxsize)
    174  1.26   thorpej 		return (EINVAL);
    175  1.26   thorpej 
    176  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan) == 0) {
    177  1.26   thorpej 		printf("%s: drq %d is not free\n", sc->sc_dev.dv_xname, chan);
    178  1.26   thorpej 		goto lose;
    179  1.26   thorpej 	}
    180  1.15   mycroft 
    181  1.26   thorpej 	ISA_DRQ_ALLOC(sc, chan);
    182  1.26   thorpej 
    183  1.26   thorpej 	return (bus_dmamap_create(sc->sc_dmat, size, 1, size, maxsize,
    184  1.26   thorpej 	    flags, &sc->sc_dmamaps[chan]));
    185  1.26   thorpej 
    186  1.26   thorpej  lose:
    187  1.26   thorpej 	panic("isa_dmamap_create");
    188  1.26   thorpej }
    189  1.26   thorpej 
    190  1.26   thorpej void
    191  1.26   thorpej isa_dmamap_destroy(isadev, chan)
    192  1.26   thorpej 	struct device *isadev;
    193  1.26   thorpej 	int chan;
    194  1.26   thorpej {
    195  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    196  1.26   thorpej 
    197  1.26   thorpej 	if (chan < 0 || chan > 7) {
    198  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    199  1.26   thorpej 		goto lose;
    200  1.26   thorpej 	}
    201  1.26   thorpej 
    202  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan)) {
    203  1.26   thorpej 		printf("%s: drq %d is already free\n",
    204  1.26   thorpej 		    sc->sc_dev.dv_xname, chan);
    205  1.26   thorpej 		goto lose;
    206  1.26   thorpej 	}
    207  1.26   thorpej 
    208  1.26   thorpej 	ISA_DRQ_FREE(sc, chan);
    209  1.26   thorpej 
    210  1.26   thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamaps[chan]);
    211  1.26   thorpej 	return;
    212  1.26   thorpej 
    213  1.26   thorpej  lose:
    214  1.26   thorpej 	panic("isa_dmamap_destroy");
    215   1.1   mycroft }
    216   1.1   mycroft 
    217   1.1   mycroft /*
    218  1.26   thorpej  * isa_dmastart(): program 8237 DMA controller channel and set it
    219  1.26   thorpej  * in motion.
    220   1.1   mycroft  */
    221  1.26   thorpej int
    222  1.26   thorpej isa_dmastart(isadev, chan, addr, nbytes, p, flags, busdmaflags)
    223  1.26   thorpej 	struct device *isadev;
    224  1.26   thorpej 	int chan;
    225  1.26   thorpej 	void *addr;
    226  1.26   thorpej 	bus_size_t nbytes;
    227  1.26   thorpej 	struct proc *p;
    228   1.5   mycroft 	int flags;
    229  1.26   thorpej 	int busdmaflags;
    230   1.1   mycroft {
    231  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    232  1.26   thorpej 	bus_dmamap_t dmam;
    233  1.26   thorpej 	bus_addr_t dmaaddr;
    234   1.1   mycroft 	int waport;
    235  1.23   mycroft 	int ochan = chan & 3;
    236  1.26   thorpej 	int error;
    237  1.26   thorpej 
    238  1.26   thorpej 	if (chan < 0 || chan > 7) {
    239  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    240  1.26   thorpej 		goto lose;
    241  1.26   thorpej 	}
    242  1.26   thorpej 
    243  1.26   thorpej #ifdef ISADMA_DEBUG
    244  1.26   thorpej 	printf("isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
    245  1.26   thorpej 	    "flags 0x%x, dmaflags 0x%x\n",
    246  1.26   thorpej 	    chan, addr, nbytes, p, flags, busdmaflags);
    247  1.26   thorpej #endif
    248  1.26   thorpej 
    249  1.26   thorpej 	if (chan & 4) {
    250  1.26   thorpej 		if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
    251  1.26   thorpej 			printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
    252  1.26   thorpej 			    sc->sc_dev.dv_xname, chan, nbytes, addr);
    253  1.26   thorpej 			goto lose;
    254  1.26   thorpej 		}
    255  1.26   thorpej 	} else {
    256  1.26   thorpej 		if (nbytes > (1 << 16)) {
    257  1.26   thorpej 			printf("%s: drq %d, nbytes 0x%lx\n",
    258  1.26   thorpej 			    sc->sc_dev.dv_xname, chan, nbytes);
    259  1.26   thorpej 			goto lose;
    260  1.26   thorpej 		}
    261  1.26   thorpej 	}
    262  1.26   thorpej 
    263  1.26   thorpej 	dmam = sc->sc_dmamaps[chan];
    264  1.26   thorpej 
    265  1.26   thorpej 	error = bus_dmamap_load(sc->sc_dmat, dmam, addr, nbytes,
    266  1.26   thorpej 	    p, busdmaflags);
    267  1.26   thorpej 	if (error)
    268  1.26   thorpej 		return (error);
    269   1.1   mycroft 
    270  1.13   mycroft #ifdef ISADMA_DEBUG
    271  1.26   thorpej 	__asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
    272   1.4   mycroft #endif
    273   1.1   mycroft 
    274  1.26   thorpej 	if (flags & DMAMODE_READ) {
    275  1.26   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREREAD);
    276  1.26   thorpej 		sc->sc_dmareads |= (1 << chan);
    277  1.26   thorpej 	} else {
    278  1.26   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREWRITE);
    279  1.26   thorpej 		sc->sc_dmareads &= ~(1 << chan);
    280   1.1   mycroft 	}
    281   1.1   mycroft 
    282  1.26   thorpej 	dmaaddr = dmam->dm_segs[0].ds_addr;
    283  1.26   thorpej 
    284  1.26   thorpej #ifdef ISADMA_DEBUG
    285  1.26   thorpej 	printf("     dmaaddr 0x%lx\n", dmaaddr);
    286  1.26   thorpej 
    287  1.26   thorpej 	__asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
    288  1.26   thorpej #endif
    289  1.24   mycroft 
    290  1.26   thorpej 	sc->sc_dmalength[chan] = nbytes;
    291   1.1   mycroft 
    292  1.26   thorpej 	isa_dmamask(sc, chan);
    293  1.26   thorpej 	sc->sc_dmafinished &= ~(1 << chan);
    294  1.14   mycroft 
    295   1.1   mycroft 	if ((chan & 4) == 0) {
    296  1.23   mycroft 		/* set dma channel mode */
    297  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, DMA1_MODE,
    298  1.26   thorpej 		    ochan | dmamode[flags]);
    299   1.1   mycroft 
    300   1.1   mycroft 		/* send start address */
    301  1.23   mycroft 		waport = DMA1_CHN(ochan);
    302  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    303  1.26   thorpej 		    dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
    304  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    305  1.26   thorpej 		    dmaaddr & 0xff);
    306  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    307  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    308   1.1   mycroft 
    309   1.1   mycroft 		/* send count */
    310  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    311  1.26   thorpej 		    (--nbytes) & 0xff);
    312  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    313  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    314   1.1   mycroft 	} else {
    315  1.23   mycroft 		/* set dma channel mode */
    316  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, DMA2_MODE,
    317  1.26   thorpej 		    ochan | dmamode[flags]);
    318   1.1   mycroft 
    319   1.1   mycroft 		/* send start address */
    320  1.23   mycroft 		waport = DMA2_CHN(ochan);
    321  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    322  1.26   thorpej 		    dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
    323  1.26   thorpej 		dmaaddr >>= 1;
    324  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    325  1.26   thorpej 		    dmaaddr & 0xff);
    326  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    327  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    328   1.1   mycroft 
    329   1.1   mycroft 		/* send count */
    330   1.1   mycroft 		nbytes >>= 1;
    331  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    332  1.26   thorpej 		    (--nbytes) & 0xff);
    333  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    334  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    335  1.23   mycroft 	}
    336   1.1   mycroft 
    337  1.26   thorpej 	isa_dmaunmask(sc, chan);
    338  1.26   thorpej 	return (0);
    339  1.26   thorpej 
    340  1.26   thorpej  lose:
    341  1.26   thorpej 	panic("isa_dmastart");
    342   1.1   mycroft }
    343   1.1   mycroft 
    344   1.1   mycroft void
    345  1.26   thorpej isa_dmaabort(isadev, chan)
    346  1.26   thorpej 	struct device *isadev;
    347   1.4   mycroft 	int chan;
    348   1.1   mycroft {
    349  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    350   1.1   mycroft 
    351  1.26   thorpej 	if (chan < 0 || chan > 7) {
    352  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    353  1.26   thorpej 		panic("isa_dmaabort");
    354  1.26   thorpej 	}
    355   1.1   mycroft 
    356  1.26   thorpej 	isa_dmamask(sc, chan);
    357  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamaps[chan]);
    358  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    359  1.22   mycroft }
    360  1.22   mycroft 
    361  1.26   thorpej bus_size_t
    362  1.26   thorpej isa_dmacount(isadev, chan)
    363  1.26   thorpej 	struct device *isadev;
    364  1.22   mycroft 	int chan;
    365  1.22   mycroft {
    366  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    367  1.22   mycroft 	int waport;
    368  1.26   thorpej 	bus_size_t nbytes;
    369  1.23   mycroft 	int ochan = chan & 3;
    370  1.22   mycroft 
    371  1.26   thorpej 	if (chan < 0 || chan > 7) {
    372  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    373  1.26   thorpej 		panic("isa_dmacount");
    374  1.26   thorpej 	}
    375  1.22   mycroft 
    376  1.26   thorpej 	isa_dmamask(sc, chan);
    377  1.23   mycroft 
    378  1.24   mycroft 	/*
    379  1.24   mycroft 	 * We have to shift the byte count by 1.  If we're in auto-initialize
    380  1.24   mycroft 	 * mode, the count may have wrapped around to the initial value.  We
    381  1.24   mycroft 	 * can't use the TC bit to check for this case, so instead we compare
    382  1.24   mycroft 	 * against the original byte count.
    383  1.24   mycroft 	 * If we're not in auto-initialize mode, then the count will wrap to
    384  1.24   mycroft 	 * -1, so we also handle that case.
    385  1.24   mycroft 	 */
    386  1.24   mycroft 	if ((chan & 4) == 0) {
    387  1.24   mycroft 		waport = DMA1_CHN(ochan);
    388  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    389  1.26   thorpej 		    waport + 1) + 1;
    390  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    391  1.26   thorpej 		    waport + 1) << 8;
    392  1.24   mycroft 		nbytes &= 0xffff;
    393  1.24   mycroft 	} else {
    394  1.24   mycroft 		waport = DMA2_CHN(ochan);
    395  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    396  1.26   thorpej 		    waport + 2) + 1;
    397  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    398  1.26   thorpej 		    waport + 2) << 8;
    399  1.24   mycroft 		nbytes <<= 1;
    400  1.24   mycroft 		nbytes &= 0x1ffff;
    401  1.24   mycroft 	}
    402  1.24   mycroft 
    403  1.26   thorpej 	if (nbytes == sc->sc_dmalength[chan])
    404  1.23   mycroft 		nbytes = 0;
    405  1.22   mycroft 
    406  1.26   thorpej 	isa_dmaunmask(sc, chan);
    407  1.22   mycroft 	return (nbytes);
    408   1.1   mycroft }
    409   1.1   mycroft 
    410  1.13   mycroft int
    411  1.26   thorpej isa_dmafinished(isadev, chan)
    412  1.26   thorpej 	struct device *isadev;
    413   1.5   mycroft 	int chan;
    414   1.1   mycroft {
    415  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    416   1.1   mycroft 
    417  1.26   thorpej 	if (chan < 0 || chan > 7) {
    418  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    419  1.26   thorpej 		panic("isa_dmafinished");
    420  1.26   thorpej 	}
    421   1.1   mycroft 
    422   1.1   mycroft 	/* check that the terminal count was reached */
    423   1.1   mycroft 	if ((chan & 4) == 0)
    424  1.26   thorpej 		sc->sc_dmafinished |= bus_space_read_1(sc->sc_iot,
    425  1.26   thorpej 		    sc->sc_dma1h, DMA1_SR) & 0x0f;
    426   1.1   mycroft 	else
    427  1.26   thorpej 		sc->sc_dmafinished |= (bus_space_read_1(sc->sc_iot,
    428  1.26   thorpej 		    sc->sc_dma2h, DMA2_SR) & 0x0f) << 4;
    429  1.14   mycroft 
    430  1.26   thorpej 	return ((sc->sc_dmafinished & (1 << chan)) != 0);
    431  1.13   mycroft }
    432  1.13   mycroft 
    433  1.13   mycroft void
    434  1.26   thorpej isa_dmadone(isadev, chan)
    435  1.26   thorpej 	struct device *isadev;
    436  1.13   mycroft 	int chan;
    437  1.13   mycroft {
    438  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    439  1.26   thorpej 	bus_dmamap_t dmam;
    440  1.13   mycroft 
    441  1.26   thorpej 	if (chan < 0 || chan > 7) {
    442  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    443  1.26   thorpej 		panic("isa_dmadone");
    444  1.26   thorpej 	}
    445  1.26   thorpej 
    446  1.26   thorpej 	dmam = sc->sc_dmamaps[chan];
    447  1.26   thorpej 
    448  1.26   thorpej 	isa_dmamask(sc, chan);
    449  1.14   mycroft 
    450  1.26   thorpej 	if (isa_dmafinished(isadev, chan) == 0)
    451  1.26   thorpej 		printf("%s: isa_dmadone: channel %d not finished\n",
    452  1.26   thorpej 		    sc->sc_dev.dv_xname, chan);
    453  1.23   mycroft 
    454  1.26   thorpej 	bus_dmamap_sync(sc->sc_dmat, dmam,
    455  1.26   thorpej 	    (sc->sc_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
    456  1.26   thorpej 	    BUS_DMASYNC_POSTWRITE);
    457   1.9   mycroft 
    458  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, dmam);
    459  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    460   1.1   mycroft }
    461   1.1   mycroft 
    462   1.1   mycroft int
    463  1.26   thorpej isa_dmamem_alloc(isadev, chan, size, addrp, flags)
    464  1.26   thorpej 	struct device *isadev;
    465  1.26   thorpej 	int chan;
    466  1.26   thorpej 	bus_size_t size;
    467  1.26   thorpej 	bus_addr_t *addrp;
    468  1.26   thorpej 	int flags;
    469  1.26   thorpej {
    470  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    471  1.26   thorpej 	bus_dma_segment_t seg;
    472  1.26   thorpej 	int error, boundary, rsegs;
    473  1.26   thorpej 
    474  1.26   thorpej 	if (chan < 0 || chan > 7) {
    475  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    476  1.26   thorpej 		panic("isa_dmamem_alloc");
    477   1.1   mycroft 	}
    478  1.26   thorpej 
    479  1.26   thorpej 	boundary = (chan & 4) ? (1 << 17) : (1 << 16);
    480  1.26   thorpej 
    481  1.26   thorpej 	size = round_page(size);
    482  1.26   thorpej 
    483  1.26   thorpej 	error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, boundary,
    484  1.26   thorpej 	    &seg, 1, &rsegs, flags);
    485  1.26   thorpej 	if (error)
    486  1.26   thorpej 		return (error);
    487  1.26   thorpej 
    488  1.26   thorpej 	*addrp = seg.ds_addr;
    489  1.26   thorpej 	return (0);
    490   1.5   mycroft }
    491   1.5   mycroft 
    492  1.26   thorpej void
    493  1.26   thorpej isa_dmamem_free(isadev, chan, addr, size)
    494  1.26   thorpej 	struct device *isadev;
    495  1.26   thorpej 	int chan;
    496  1.26   thorpej 	bus_addr_t addr;
    497  1.26   thorpej 	bus_size_t size;
    498  1.26   thorpej {
    499  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    500  1.26   thorpej 	bus_dma_segment_t seg;
    501  1.26   thorpej 
    502  1.26   thorpej 	if (chan < 0 || chan > 7) {
    503  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    504  1.26   thorpej 		panic("isa_dmamem_free");
    505  1.26   thorpej 	}
    506  1.26   thorpej 
    507  1.26   thorpej 	seg.ds_addr = addr;
    508  1.26   thorpej 	seg.ds_len = size;
    509   1.5   mycroft 
    510  1.26   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, 1);
    511  1.26   thorpej }
    512   1.5   mycroft 
    513  1.26   thorpej int
    514  1.26   thorpej isa_dmamem_map(isadev, chan, addr, size, kvap, flags)
    515  1.26   thorpej 	struct device *isadev;
    516  1.26   thorpej 	int chan;
    517  1.26   thorpej 	bus_addr_t addr;
    518  1.26   thorpej 	bus_size_t size;
    519  1.26   thorpej 	caddr_t *kvap;
    520  1.26   thorpej 	int flags;
    521  1.26   thorpej {
    522  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    523  1.26   thorpej 	bus_dma_segment_t seg;
    524  1.26   thorpej 
    525  1.26   thorpej 	if (chan < 0 || chan > 7) {
    526  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    527  1.26   thorpej 		panic("isa_dmamem_map");
    528   1.5   mycroft 	}
    529   1.5   mycroft 
    530  1.26   thorpej 	seg.ds_addr = addr;
    531  1.26   thorpej 	seg.ds_len = size;
    532  1.26   thorpej 
    533  1.26   thorpej 	return (bus_dmamem_map(sc->sc_dmat, &seg, 1, size, kvap, flags));
    534   1.5   mycroft }
    535   1.5   mycroft 
    536   1.5   mycroft void
    537  1.26   thorpej isa_dmamem_unmap(isadev, chan, kva, size)
    538  1.26   thorpej 	struct device *isadev;
    539  1.26   thorpej 	int chan;
    540  1.26   thorpej 	caddr_t kva;
    541  1.26   thorpej 	size_t size;
    542  1.19  christos {
    543  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    544   1.5   mycroft 
    545  1.26   thorpej 	if (chan < 0 || chan > 7) {
    546  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    547  1.26   thorpej 		panic("isa_dmamem_unmap");
    548   1.5   mycroft 	}
    549  1.26   thorpej 
    550  1.26   thorpej 	bus_dmamem_unmap(sc->sc_dmat, kva, size);
    551  1.26   thorpej }
    552  1.26   thorpej 
    553  1.26   thorpej int
    554  1.26   thorpej isa_dmamem_mmap(isadev, chan, addr, size, off, prot, flags)
    555  1.26   thorpej 	struct device *isadev;
    556  1.26   thorpej 	int chan;
    557  1.26   thorpej 	bus_addr_t addr;
    558  1.26   thorpej 	bus_size_t size;
    559  1.26   thorpej 	int off, prot, flags;
    560  1.26   thorpej {
    561  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    562  1.26   thorpej 	bus_dma_segment_t seg;
    563  1.26   thorpej 
    564  1.26   thorpej 	if (chan < 0 || chan > 7) {
    565  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    566  1.26   thorpej 		panic("isa_dmamem_mmap");
    567  1.26   thorpej 	}
    568  1.26   thorpej 
    569  1.26   thorpej 	seg.ds_addr = addr;
    570  1.26   thorpej 	seg.ds_len = size;
    571  1.26   thorpej 
    572  1.26   thorpej 	return (bus_dmamem_mmap(sc->sc_dmat, &seg, 1, off, prot, flags));
    573   1.1   mycroft }
    574