Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.33
      1  1.33   thorpej /*	$NetBSD: isadma.c,v 1.33 1998/02/04 05:14:35 thorpej Exp $	*/
      2  1.26   thorpej 
      3  1.26   thorpej /*-
      4  1.33   thorpej  * Copyright (c) 1997, 1998 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.27  augustss /* Used by isa_malloc() */
     59  1.27  augustss #include <sys/malloc.h>
     60  1.27  augustss struct isa_mem {
     61  1.27  augustss 	struct device *isadev;
     62  1.27  augustss 	int chan;
     63  1.27  augustss 	bus_size_t size;
     64  1.27  augustss 	bus_addr_t addr;
     65  1.27  augustss 	caddr_t kva;
     66  1.27  augustss 	struct isa_mem *next;
     67  1.27  augustss } *isa_mem_head = 0;
     68  1.27  augustss 
     69  1.26   thorpej /*
     70  1.26   thorpej  * High byte of DMA address is stored in this DMAPG register for
     71  1.26   thorpej  * the Nth DMA channel.
     72  1.26   thorpej  */
     73  1.18   mycroft static int dmapageport[2][4] = {
     74  1.26   thorpej 	{0x7, 0x3, 0x1, 0x2},
     75  1.26   thorpej 	{0xf, 0xb, 0x9, 0xa}
     76  1.15   mycroft };
     77  1.15   mycroft 
     78  1.15   mycroft static u_int8_t dmamode[4] = {
     79  1.17   mycroft 	DMA37MD_READ | DMA37MD_SINGLE,
     80  1.15   mycroft 	DMA37MD_WRITE | DMA37MD_SINGLE,
     81  1.25   mycroft 	DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
     82  1.25   mycroft 	DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP
     83  1.15   mycroft };
     84   1.1   mycroft 
     85  1.26   thorpej static inline void isa_dmaunmask __P((struct isa_softc *, int));
     86  1.26   thorpej static inline void isa_dmamask __P((struct isa_softc *, int));
     87  1.19  christos 
     88  1.26   thorpej static inline void
     89  1.26   thorpej isa_dmaunmask(sc, chan)
     90  1.26   thorpej 	struct isa_softc *sc;
     91  1.23   mycroft 	int chan;
     92  1.23   mycroft {
     93  1.23   mycroft 	int ochan = chan & 3;
     94  1.23   mycroft 
     95  1.23   mycroft 	/* set dma channel mode, and set dma channel mode */
     96  1.23   mycroft 	if ((chan & 4) == 0)
     97  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
     98  1.26   thorpej 		    DMA1_SMSK, ochan | DMA37SM_CLEAR);
     99  1.23   mycroft 	else
    100  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    101  1.26   thorpej 		    DMA2_SMSK, ochan | DMA37SM_CLEAR);
    102  1.23   mycroft }
    103  1.23   mycroft 
    104  1.26   thorpej static inline void
    105  1.26   thorpej isa_dmamask(sc, chan)
    106  1.26   thorpej 	struct isa_softc *sc;
    107  1.23   mycroft 	int chan;
    108  1.23   mycroft {
    109  1.23   mycroft 	int ochan = chan & 3;
    110  1.23   mycroft 
    111  1.23   mycroft 	/* set dma channel mode, and set dma channel mode */
    112  1.23   mycroft 	if ((chan & 4) == 0) {
    113  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    114  1.26   thorpej 		    DMA1_SMSK, ochan | DMA37SM_SET);
    115  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    116  1.26   thorpej 		    DMA1_FFC, 0);
    117  1.23   mycroft 	} else {
    118  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    119  1.26   thorpej 		    DMA2_SMSK, ochan | DMA37SM_SET);
    120  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    121  1.26   thorpej 		    DMA2_FFC, 0);
    122  1.23   mycroft 	}
    123  1.23   mycroft }
    124  1.23   mycroft 
    125   1.1   mycroft /*
    126   1.5   mycroft  * isa_dmacascade(): program 8237 DMA controller channel to accept
    127   1.1   mycroft  * external dma control by a board.
    128   1.1   mycroft  */
    129   1.1   mycroft void
    130  1.26   thorpej isa_dmacascade(isadev, chan)
    131  1.26   thorpej 	struct device *isadev;
    132   1.4   mycroft 	int chan;
    133   1.1   mycroft {
    134  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    135  1.23   mycroft 	int ochan = chan & 3;
    136   1.4   mycroft 
    137  1.26   thorpej 	if (chan < 0 || chan > 7) {
    138  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    139  1.26   thorpej 		goto lose;
    140  1.26   thorpej 	}
    141  1.26   thorpej 
    142  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan) == 0) {
    143  1.26   thorpej 		printf("%s: DRQ %d is not free\n", sc->sc_dev.dv_xname, chan);
    144  1.26   thorpej 		goto lose;
    145  1.26   thorpej 	}
    146  1.26   thorpej 
    147  1.26   thorpej 	ISA_DRQ_ALLOC(sc, chan);
    148   1.1   mycroft 
    149   1.1   mycroft 	/* set dma channel mode, and set dma channel mode */
    150  1.23   mycroft 	if ((chan & 4) == 0)
    151  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
    152  1.26   thorpej 		    DMA1_MODE, ochan | DMA37MD_CASCADE);
    153  1.26   thorpej 	else
    154  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
    155  1.26   thorpej 		    DMA2_MODE, ochan | DMA37MD_CASCADE);
    156  1.26   thorpej 
    157  1.26   thorpej 	isa_dmaunmask(sc, chan);
    158  1.26   thorpej 	return;
    159  1.26   thorpej 
    160  1.26   thorpej  lose:
    161  1.26   thorpej 	panic("isa_dmacascade");
    162  1.26   thorpej }
    163  1.26   thorpej 
    164  1.26   thorpej int
    165  1.26   thorpej isa_dmamap_create(isadev, chan, size, flags)
    166  1.26   thorpej 	struct device *isadev;
    167  1.26   thorpej 	int chan;
    168  1.26   thorpej 	bus_size_t size;
    169  1.26   thorpej 	int flags;
    170  1.26   thorpej {
    171  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    172  1.26   thorpej 	bus_size_t maxsize;
    173  1.26   thorpej 
    174  1.26   thorpej 	if (chan < 0 || chan > 7) {
    175  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    176  1.26   thorpej 		goto lose;
    177  1.26   thorpej 	}
    178  1.26   thorpej 
    179  1.26   thorpej 	if (chan & 4)
    180  1.26   thorpej 		maxsize = (1 << 17);
    181  1.23   mycroft 	else
    182  1.26   thorpej 		maxsize = (1 << 16);
    183  1.26   thorpej 
    184  1.26   thorpej 	if (size > maxsize)
    185  1.26   thorpej 		return (EINVAL);
    186  1.26   thorpej 
    187  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan) == 0) {
    188  1.26   thorpej 		printf("%s: drq %d is not free\n", sc->sc_dev.dv_xname, chan);
    189  1.30  augustss 		goto lose;
    190  1.26   thorpej 	}
    191  1.15   mycroft 
    192  1.26   thorpej 	ISA_DRQ_ALLOC(sc, chan);
    193  1.26   thorpej 
    194  1.26   thorpej 	return (bus_dmamap_create(sc->sc_dmat, size, 1, size, maxsize,
    195  1.26   thorpej 	    flags, &sc->sc_dmamaps[chan]));
    196  1.26   thorpej 
    197  1.26   thorpej  lose:
    198  1.26   thorpej 	panic("isa_dmamap_create");
    199  1.26   thorpej }
    200  1.26   thorpej 
    201  1.26   thorpej void
    202  1.26   thorpej isa_dmamap_destroy(isadev, chan)
    203  1.26   thorpej 	struct device *isadev;
    204  1.26   thorpej 	int chan;
    205  1.26   thorpej {
    206  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    207  1.26   thorpej 
    208  1.26   thorpej 	if (chan < 0 || chan > 7) {
    209  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    210  1.26   thorpej 		goto lose;
    211  1.26   thorpej 	}
    212  1.26   thorpej 
    213  1.26   thorpej 	if (ISA_DRQ_ISFREE(sc, chan)) {
    214  1.26   thorpej 		printf("%s: drq %d is already free\n",
    215  1.26   thorpej 		    sc->sc_dev.dv_xname, chan);
    216  1.26   thorpej 		goto lose;
    217  1.26   thorpej 	}
    218  1.26   thorpej 
    219  1.26   thorpej 	ISA_DRQ_FREE(sc, chan);
    220  1.26   thorpej 
    221  1.26   thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamaps[chan]);
    222  1.26   thorpej 	return;
    223  1.26   thorpej 
    224  1.26   thorpej  lose:
    225  1.26   thorpej 	panic("isa_dmamap_destroy");
    226   1.1   mycroft }
    227   1.1   mycroft 
    228   1.1   mycroft /*
    229  1.26   thorpej  * isa_dmastart(): program 8237 DMA controller channel and set it
    230  1.26   thorpej  * in motion.
    231   1.1   mycroft  */
    232  1.26   thorpej int
    233  1.26   thorpej isa_dmastart(isadev, chan, addr, nbytes, p, flags, busdmaflags)
    234  1.26   thorpej 	struct device *isadev;
    235  1.26   thorpej 	int chan;
    236  1.26   thorpej 	void *addr;
    237  1.26   thorpej 	bus_size_t nbytes;
    238  1.26   thorpej 	struct proc *p;
    239   1.5   mycroft 	int flags;
    240  1.26   thorpej 	int busdmaflags;
    241   1.1   mycroft {
    242  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    243  1.26   thorpej 	bus_dmamap_t dmam;
    244  1.26   thorpej 	bus_addr_t dmaaddr;
    245   1.1   mycroft 	int waport;
    246  1.23   mycroft 	int ochan = chan & 3;
    247  1.26   thorpej 	int error;
    248  1.26   thorpej 
    249  1.26   thorpej 	if (chan < 0 || chan > 7) {
    250  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    251  1.26   thorpej 		goto lose;
    252  1.26   thorpej 	}
    253  1.26   thorpej 
    254  1.26   thorpej #ifdef ISADMA_DEBUG
    255  1.26   thorpej 	printf("isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
    256  1.26   thorpej 	    "flags 0x%x, dmaflags 0x%x\n",
    257  1.26   thorpej 	    chan, addr, nbytes, p, flags, busdmaflags);
    258  1.26   thorpej #endif
    259  1.26   thorpej 
    260  1.26   thorpej 	if (chan & 4) {
    261  1.26   thorpej 		if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
    262  1.26   thorpej 			printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
    263  1.26   thorpej 			    sc->sc_dev.dv_xname, chan, nbytes, addr);
    264  1.26   thorpej 			goto lose;
    265  1.26   thorpej 		}
    266  1.26   thorpej 	} else {
    267  1.26   thorpej 		if (nbytes > (1 << 16)) {
    268  1.26   thorpej 			printf("%s: drq %d, nbytes 0x%lx\n",
    269  1.26   thorpej 			    sc->sc_dev.dv_xname, chan, nbytes);
    270  1.26   thorpej 			goto lose;
    271  1.26   thorpej 		}
    272  1.26   thorpej 	}
    273  1.26   thorpej 
    274  1.26   thorpej 	dmam = sc->sc_dmamaps[chan];
    275  1.31  augustss 	if (dmam == NULL)
    276  1.31  augustss 		panic("isa_dmastart: no DMA map for chan %d\n", chan);
    277  1.26   thorpej 
    278  1.26   thorpej 	error = bus_dmamap_load(sc->sc_dmat, dmam, addr, nbytes,
    279  1.26   thorpej 	    p, busdmaflags);
    280  1.26   thorpej 	if (error)
    281  1.26   thorpej 		return (error);
    282   1.1   mycroft 
    283  1.13   mycroft #ifdef ISADMA_DEBUG
    284  1.26   thorpej 	__asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
    285   1.4   mycroft #endif
    286   1.1   mycroft 
    287  1.26   thorpej 	if (flags & DMAMODE_READ) {
    288  1.33   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, 0, dmam->dm_mapsize,
    289  1.33   thorpej 		    BUS_DMASYNC_PREREAD);
    290  1.26   thorpej 		sc->sc_dmareads |= (1 << chan);
    291  1.26   thorpej 	} else {
    292  1.33   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, 0, dmam->dm_mapsize,
    293  1.33   thorpej 		    BUS_DMASYNC_PREWRITE);
    294  1.26   thorpej 		sc->sc_dmareads &= ~(1 << chan);
    295   1.1   mycroft 	}
    296   1.1   mycroft 
    297  1.26   thorpej 	dmaaddr = dmam->dm_segs[0].ds_addr;
    298  1.26   thorpej 
    299  1.26   thorpej #ifdef ISADMA_DEBUG
    300  1.26   thorpej 	printf("     dmaaddr 0x%lx\n", dmaaddr);
    301  1.26   thorpej 
    302  1.26   thorpej 	__asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
    303  1.26   thorpej #endif
    304  1.24   mycroft 
    305  1.26   thorpej 	sc->sc_dmalength[chan] = nbytes;
    306   1.1   mycroft 
    307  1.26   thorpej 	isa_dmamask(sc, chan);
    308  1.26   thorpej 	sc->sc_dmafinished &= ~(1 << chan);
    309  1.14   mycroft 
    310   1.1   mycroft 	if ((chan & 4) == 0) {
    311  1.23   mycroft 		/* set dma channel mode */
    312  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, DMA1_MODE,
    313  1.26   thorpej 		    ochan | dmamode[flags]);
    314   1.1   mycroft 
    315   1.1   mycroft 		/* send start address */
    316  1.23   mycroft 		waport = DMA1_CHN(ochan);
    317  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    318  1.26   thorpej 		    dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
    319  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    320  1.26   thorpej 		    dmaaddr & 0xff);
    321  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    322  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    323   1.1   mycroft 
    324   1.1   mycroft 		/* send count */
    325  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    326  1.26   thorpej 		    (--nbytes) & 0xff);
    327  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    328  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    329   1.1   mycroft 	} else {
    330  1.23   mycroft 		/* set dma channel mode */
    331  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, DMA2_MODE,
    332  1.26   thorpej 		    ochan | dmamode[flags]);
    333   1.1   mycroft 
    334   1.1   mycroft 		/* send start address */
    335  1.23   mycroft 		waport = DMA2_CHN(ochan);
    336  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    337  1.26   thorpej 		    dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
    338  1.26   thorpej 		dmaaddr >>= 1;
    339  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    340  1.26   thorpej 		    dmaaddr & 0xff);
    341  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    342  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    343   1.1   mycroft 
    344   1.1   mycroft 		/* send count */
    345   1.1   mycroft 		nbytes >>= 1;
    346  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    347  1.26   thorpej 		    (--nbytes) & 0xff);
    348  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    349  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    350  1.23   mycroft 	}
    351   1.1   mycroft 
    352  1.26   thorpej 	isa_dmaunmask(sc, chan);
    353  1.26   thorpej 	return (0);
    354  1.26   thorpej 
    355  1.26   thorpej  lose:
    356  1.26   thorpej 	panic("isa_dmastart");
    357   1.1   mycroft }
    358   1.1   mycroft 
    359   1.1   mycroft void
    360  1.26   thorpej isa_dmaabort(isadev, chan)
    361  1.26   thorpej 	struct device *isadev;
    362   1.4   mycroft 	int chan;
    363   1.1   mycroft {
    364  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    365   1.1   mycroft 
    366  1.26   thorpej 	if (chan < 0 || chan > 7) {
    367  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    368  1.26   thorpej 		panic("isa_dmaabort");
    369  1.26   thorpej 	}
    370   1.1   mycroft 
    371  1.26   thorpej 	isa_dmamask(sc, chan);
    372  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamaps[chan]);
    373  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    374  1.22   mycroft }
    375  1.22   mycroft 
    376  1.26   thorpej bus_size_t
    377  1.26   thorpej isa_dmacount(isadev, chan)
    378  1.26   thorpej 	struct device *isadev;
    379  1.22   mycroft 	int chan;
    380  1.22   mycroft {
    381  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    382  1.22   mycroft 	int waport;
    383  1.26   thorpej 	bus_size_t nbytes;
    384  1.23   mycroft 	int ochan = chan & 3;
    385  1.22   mycroft 
    386  1.26   thorpej 	if (chan < 0 || chan > 7) {
    387  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    388  1.26   thorpej 		panic("isa_dmacount");
    389  1.26   thorpej 	}
    390  1.22   mycroft 
    391  1.26   thorpej 	isa_dmamask(sc, chan);
    392  1.23   mycroft 
    393  1.24   mycroft 	/*
    394  1.24   mycroft 	 * We have to shift the byte count by 1.  If we're in auto-initialize
    395  1.24   mycroft 	 * mode, the count may have wrapped around to the initial value.  We
    396  1.24   mycroft 	 * can't use the TC bit to check for this case, so instead we compare
    397  1.24   mycroft 	 * against the original byte count.
    398  1.24   mycroft 	 * If we're not in auto-initialize mode, then the count will wrap to
    399  1.24   mycroft 	 * -1, so we also handle that case.
    400  1.24   mycroft 	 */
    401  1.24   mycroft 	if ((chan & 4) == 0) {
    402  1.24   mycroft 		waport = DMA1_CHN(ochan);
    403  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    404  1.26   thorpej 		    waport + 1) + 1;
    405  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    406  1.26   thorpej 		    waport + 1) << 8;
    407  1.24   mycroft 		nbytes &= 0xffff;
    408  1.24   mycroft 	} else {
    409  1.24   mycroft 		waport = DMA2_CHN(ochan);
    410  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    411  1.26   thorpej 		    waport + 2) + 1;
    412  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    413  1.26   thorpej 		    waport + 2) << 8;
    414  1.24   mycroft 		nbytes <<= 1;
    415  1.24   mycroft 		nbytes &= 0x1ffff;
    416  1.24   mycroft 	}
    417  1.24   mycroft 
    418  1.26   thorpej 	if (nbytes == sc->sc_dmalength[chan])
    419  1.23   mycroft 		nbytes = 0;
    420  1.22   mycroft 
    421  1.26   thorpej 	isa_dmaunmask(sc, chan);
    422  1.22   mycroft 	return (nbytes);
    423   1.1   mycroft }
    424   1.1   mycroft 
    425  1.13   mycroft int
    426  1.26   thorpej isa_dmafinished(isadev, chan)
    427  1.26   thorpej 	struct device *isadev;
    428   1.5   mycroft 	int chan;
    429   1.1   mycroft {
    430  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    431   1.1   mycroft 
    432  1.26   thorpej 	if (chan < 0 || chan > 7) {
    433  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    434  1.26   thorpej 		panic("isa_dmafinished");
    435  1.26   thorpej 	}
    436   1.1   mycroft 
    437   1.1   mycroft 	/* check that the terminal count was reached */
    438   1.1   mycroft 	if ((chan & 4) == 0)
    439  1.26   thorpej 		sc->sc_dmafinished |= bus_space_read_1(sc->sc_iot,
    440  1.26   thorpej 		    sc->sc_dma1h, DMA1_SR) & 0x0f;
    441   1.1   mycroft 	else
    442  1.26   thorpej 		sc->sc_dmafinished |= (bus_space_read_1(sc->sc_iot,
    443  1.26   thorpej 		    sc->sc_dma2h, DMA2_SR) & 0x0f) << 4;
    444  1.14   mycroft 
    445  1.26   thorpej 	return ((sc->sc_dmafinished & (1 << chan)) != 0);
    446  1.13   mycroft }
    447  1.13   mycroft 
    448  1.13   mycroft void
    449  1.26   thorpej isa_dmadone(isadev, chan)
    450  1.26   thorpej 	struct device *isadev;
    451  1.13   mycroft 	int chan;
    452  1.13   mycroft {
    453  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    454  1.26   thorpej 	bus_dmamap_t dmam;
    455  1.13   mycroft 
    456  1.26   thorpej 	if (chan < 0 || chan > 7) {
    457  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    458  1.26   thorpej 		panic("isa_dmadone");
    459  1.26   thorpej 	}
    460  1.26   thorpej 
    461  1.26   thorpej 	dmam = sc->sc_dmamaps[chan];
    462  1.26   thorpej 
    463  1.26   thorpej 	isa_dmamask(sc, chan);
    464  1.14   mycroft 
    465  1.26   thorpej 	if (isa_dmafinished(isadev, chan) == 0)
    466  1.26   thorpej 		printf("%s: isa_dmadone: channel %d not finished\n",
    467  1.26   thorpej 		    sc->sc_dev.dv_xname, chan);
    468  1.23   mycroft 
    469  1.33   thorpej 	bus_dmamap_sync(sc->sc_dmat, dmam, 0, dmam->dm_mapsize,
    470  1.26   thorpej 	    (sc->sc_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
    471  1.26   thorpej 	    BUS_DMASYNC_POSTWRITE);
    472   1.9   mycroft 
    473  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, dmam);
    474  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    475   1.1   mycroft }
    476   1.1   mycroft 
    477   1.1   mycroft int
    478  1.26   thorpej isa_dmamem_alloc(isadev, chan, size, addrp, flags)
    479  1.26   thorpej 	struct device *isadev;
    480  1.26   thorpej 	int chan;
    481  1.26   thorpej 	bus_size_t size;
    482  1.26   thorpej 	bus_addr_t *addrp;
    483  1.26   thorpej 	int flags;
    484  1.26   thorpej {
    485  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    486  1.26   thorpej 	bus_dma_segment_t seg;
    487  1.26   thorpej 	int error, boundary, rsegs;
    488  1.26   thorpej 
    489  1.26   thorpej 	if (chan < 0 || chan > 7) {
    490  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    491  1.26   thorpej 		panic("isa_dmamem_alloc");
    492   1.1   mycroft 	}
    493  1.26   thorpej 
    494  1.26   thorpej 	boundary = (chan & 4) ? (1 << 17) : (1 << 16);
    495  1.26   thorpej 
    496  1.26   thorpej 	size = round_page(size);
    497  1.26   thorpej 
    498  1.26   thorpej 	error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, boundary,
    499  1.26   thorpej 	    &seg, 1, &rsegs, flags);
    500  1.26   thorpej 	if (error)
    501  1.26   thorpej 		return (error);
    502  1.26   thorpej 
    503  1.26   thorpej 	*addrp = seg.ds_addr;
    504  1.26   thorpej 	return (0);
    505   1.5   mycroft }
    506   1.5   mycroft 
    507  1.26   thorpej void
    508  1.26   thorpej isa_dmamem_free(isadev, chan, addr, size)
    509  1.26   thorpej 	struct device *isadev;
    510  1.26   thorpej 	int chan;
    511  1.26   thorpej 	bus_addr_t addr;
    512  1.26   thorpej 	bus_size_t size;
    513  1.26   thorpej {
    514  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    515  1.26   thorpej 	bus_dma_segment_t seg;
    516  1.26   thorpej 
    517  1.26   thorpej 	if (chan < 0 || chan > 7) {
    518  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    519  1.26   thorpej 		panic("isa_dmamem_free");
    520  1.26   thorpej 	}
    521  1.26   thorpej 
    522  1.26   thorpej 	seg.ds_addr = addr;
    523  1.26   thorpej 	seg.ds_len = size;
    524   1.5   mycroft 
    525  1.26   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, 1);
    526  1.26   thorpej }
    527   1.5   mycroft 
    528  1.26   thorpej int
    529  1.26   thorpej isa_dmamem_map(isadev, chan, addr, size, kvap, flags)
    530  1.26   thorpej 	struct device *isadev;
    531  1.26   thorpej 	int chan;
    532  1.26   thorpej 	bus_addr_t addr;
    533  1.26   thorpej 	bus_size_t size;
    534  1.26   thorpej 	caddr_t *kvap;
    535  1.26   thorpej 	int flags;
    536  1.26   thorpej {
    537  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    538  1.26   thorpej 	bus_dma_segment_t seg;
    539  1.26   thorpej 
    540  1.26   thorpej 	if (chan < 0 || chan > 7) {
    541  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    542  1.26   thorpej 		panic("isa_dmamem_map");
    543   1.5   mycroft 	}
    544   1.5   mycroft 
    545  1.26   thorpej 	seg.ds_addr = addr;
    546  1.26   thorpej 	seg.ds_len = size;
    547  1.26   thorpej 
    548  1.26   thorpej 	return (bus_dmamem_map(sc->sc_dmat, &seg, 1, size, kvap, flags));
    549   1.5   mycroft }
    550   1.5   mycroft 
    551   1.5   mycroft void
    552  1.26   thorpej isa_dmamem_unmap(isadev, chan, kva, size)
    553  1.26   thorpej 	struct device *isadev;
    554  1.26   thorpej 	int chan;
    555  1.26   thorpej 	caddr_t kva;
    556  1.26   thorpej 	size_t size;
    557  1.19  christos {
    558  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    559   1.5   mycroft 
    560  1.26   thorpej 	if (chan < 0 || chan > 7) {
    561  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    562  1.26   thorpej 		panic("isa_dmamem_unmap");
    563   1.5   mycroft 	}
    564  1.26   thorpej 
    565  1.26   thorpej 	bus_dmamem_unmap(sc->sc_dmat, kva, size);
    566  1.26   thorpej }
    567  1.26   thorpej 
    568  1.26   thorpej int
    569  1.26   thorpej isa_dmamem_mmap(isadev, chan, addr, size, off, prot, flags)
    570  1.26   thorpej 	struct device *isadev;
    571  1.26   thorpej 	int chan;
    572  1.26   thorpej 	bus_addr_t addr;
    573  1.26   thorpej 	bus_size_t size;
    574  1.26   thorpej 	int off, prot, flags;
    575  1.26   thorpej {
    576  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    577  1.26   thorpej 	bus_dma_segment_t seg;
    578  1.26   thorpej 
    579  1.26   thorpej 	if (chan < 0 || chan > 7) {
    580  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    581  1.26   thorpej 		panic("isa_dmamem_mmap");
    582  1.26   thorpej 	}
    583  1.26   thorpej 
    584  1.26   thorpej 	seg.ds_addr = addr;
    585  1.26   thorpej 	seg.ds_len = size;
    586  1.26   thorpej 
    587  1.26   thorpej 	return (bus_dmamem_mmap(sc->sc_dmat, &seg, 1, off, prot, flags));
    588  1.30  augustss }
    589  1.30  augustss 
    590  1.30  augustss int
    591  1.30  augustss isa_drq_isfree(isadev, chan)
    592  1.30  augustss 	struct device *isadev;
    593  1.30  augustss 	int chan;
    594  1.30  augustss {
    595  1.30  augustss 	struct isa_softc *sc = (struct isa_softc *)isadev;
    596  1.30  augustss 	if (chan < 0 || chan > 7) {
    597  1.30  augustss 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    598  1.30  augustss 		panic("isa_drq_isfree");
    599  1.30  augustss 	}
    600  1.30  augustss 	return ISA_DRQ_ISFREE(sc, chan);
    601  1.27  augustss }
    602  1.27  augustss 
    603  1.27  augustss void *
    604  1.27  augustss isa_malloc(isadev, chan, size, pool, flags)
    605  1.27  augustss 	struct device *isadev;
    606  1.27  augustss 	int chan;
    607  1.27  augustss 	size_t size;
    608  1.27  augustss 	int pool;
    609  1.27  augustss 	int flags;
    610  1.27  augustss {
    611  1.27  augustss 	bus_addr_t addr;
    612  1.27  augustss 	caddr_t kva;
    613  1.27  augustss 	int bflags;
    614  1.27  augustss 	struct isa_mem *m;
    615  1.27  augustss 
    616  1.27  augustss 	bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
    617  1.27  augustss 
    618  1.27  augustss 	if (isa_dmamem_alloc(isadev, chan, size, &addr, bflags))
    619  1.27  augustss 		return 0;
    620  1.27  augustss 	if (isa_dmamem_map(isadev, chan, addr, size, &kva, bflags)) {
    621  1.27  augustss 		isa_dmamem_free(isadev, chan, addr, size);
    622  1.27  augustss 		return 0;
    623  1.27  augustss 	}
    624  1.27  augustss 	m = malloc(sizeof(*m), pool, flags);
    625  1.27  augustss 	if (m == 0) {
    626  1.27  augustss 		isa_dmamem_unmap(isadev, chan, kva, size);
    627  1.27  augustss 		isa_dmamem_free(isadev, chan, addr, size);
    628  1.27  augustss 		return 0;
    629  1.27  augustss 	}
    630  1.27  augustss 	m->isadev = isadev;
    631  1.27  augustss 	m->chan = chan;
    632  1.27  augustss 	m->size = size;
    633  1.27  augustss 	m->addr = addr;
    634  1.27  augustss 	m->kva = kva;
    635  1.27  augustss 	m->next = isa_mem_head;
    636  1.27  augustss 	isa_mem_head = m;
    637  1.27  augustss 	return (void *)kva;
    638  1.27  augustss }
    639  1.27  augustss 
    640  1.27  augustss void
    641  1.27  augustss isa_free(addr, pool)
    642  1.27  augustss 	void *addr;
    643  1.27  augustss 	int pool;
    644  1.27  augustss {
    645  1.27  augustss 	struct isa_mem **mp, *m;
    646  1.27  augustss 	caddr_t kva = (caddr_t)addr;
    647  1.27  augustss 
    648  1.27  augustss 	for(mp = &isa_mem_head; *mp && (*mp)->kva != kva; mp = &(*mp)->next)
    649  1.27  augustss 		;
    650  1.27  augustss 	m = *mp;
    651  1.27  augustss 	if (!m) {
    652  1.27  augustss 		printf("isa_free: freeing unallocted memory\n");
    653  1.27  augustss 		return;
    654  1.27  augustss 	}
    655  1.27  augustss 	*mp = m->next;
    656  1.27  augustss 	isa_dmamem_unmap(m->isadev, m->chan, kva, m->size);
    657  1.27  augustss 	isa_dmamem_free(m->isadev, m->chan, m->addr, m->size);
    658  1.27  augustss 	free(m, pool);
    659  1.28  augustss }
    660  1.28  augustss 
    661  1.28  augustss int
    662  1.28  augustss isa_mappage(mem, off, prot)
    663  1.28  augustss 	void *mem;
    664  1.28  augustss 	int off;
    665  1.28  augustss 	int prot;
    666  1.28  augustss {
    667  1.28  augustss 	struct isa_mem *m;
    668  1.28  augustss 
    669  1.28  augustss 	for(m = isa_mem_head; m && m->kva != (caddr_t)mem; m = m->next)
    670  1.28  augustss 		;
    671  1.28  augustss 	if (!m) {
    672  1.28  augustss 		printf("isa_mappage: mapping unallocted memory\n");
    673  1.28  augustss 		return -1;
    674  1.28  augustss 	}
    675  1.28  augustss 	return isa_dmamem_mmap(m->isadev, m->chan, m->addr,
    676  1.28  augustss 			       m->size, off, prot, BUS_DMA_WAITOK);
    677   1.1   mycroft }
    678