Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.27
      1  1.27  augustss /*	$NetBSD: isadma.c,v 1.27 1997/07/27 01:16:57 augustss 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.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.26   thorpej 		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.26   thorpej 
    276  1.26   thorpej 	error = bus_dmamap_load(sc->sc_dmat, dmam, addr, nbytes,
    277  1.26   thorpej 	    p, busdmaflags);
    278  1.26   thorpej 	if (error)
    279  1.26   thorpej 		return (error);
    280   1.1   mycroft 
    281  1.13   mycroft #ifdef ISADMA_DEBUG
    282  1.26   thorpej 	__asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
    283   1.4   mycroft #endif
    284   1.1   mycroft 
    285  1.26   thorpej 	if (flags & DMAMODE_READ) {
    286  1.26   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREREAD);
    287  1.26   thorpej 		sc->sc_dmareads |= (1 << chan);
    288  1.26   thorpej 	} else {
    289  1.26   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREWRITE);
    290  1.26   thorpej 		sc->sc_dmareads &= ~(1 << chan);
    291   1.1   mycroft 	}
    292   1.1   mycroft 
    293  1.26   thorpej 	dmaaddr = dmam->dm_segs[0].ds_addr;
    294  1.26   thorpej 
    295  1.26   thorpej #ifdef ISADMA_DEBUG
    296  1.26   thorpej 	printf("     dmaaddr 0x%lx\n", dmaaddr);
    297  1.26   thorpej 
    298  1.26   thorpej 	__asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
    299  1.26   thorpej #endif
    300  1.24   mycroft 
    301  1.26   thorpej 	sc->sc_dmalength[chan] = nbytes;
    302   1.1   mycroft 
    303  1.26   thorpej 	isa_dmamask(sc, chan);
    304  1.26   thorpej 	sc->sc_dmafinished &= ~(1 << chan);
    305  1.14   mycroft 
    306   1.1   mycroft 	if ((chan & 4) == 0) {
    307  1.23   mycroft 		/* set dma channel mode */
    308  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, DMA1_MODE,
    309  1.26   thorpej 		    ochan | dmamode[flags]);
    310   1.1   mycroft 
    311   1.1   mycroft 		/* send start address */
    312  1.23   mycroft 		waport = DMA1_CHN(ochan);
    313  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    314  1.26   thorpej 		    dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
    315  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    316  1.26   thorpej 		    dmaaddr & 0xff);
    317  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
    318  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    319   1.1   mycroft 
    320   1.1   mycroft 		/* send count */
    321  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    322  1.26   thorpej 		    (--nbytes) & 0xff);
    323  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
    324  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    325   1.1   mycroft 	} else {
    326  1.23   mycroft 		/* set dma channel mode */
    327  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, DMA2_MODE,
    328  1.26   thorpej 		    ochan | dmamode[flags]);
    329   1.1   mycroft 
    330   1.1   mycroft 		/* send start address */
    331  1.23   mycroft 		waport = DMA2_CHN(ochan);
    332  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
    333  1.26   thorpej 		    dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
    334  1.26   thorpej 		dmaaddr >>= 1;
    335  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    336  1.26   thorpej 		    dmaaddr & 0xff);
    337  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
    338  1.26   thorpej 		    (dmaaddr >> 8) & 0xff);
    339   1.1   mycroft 
    340   1.1   mycroft 		/* send count */
    341   1.1   mycroft 		nbytes >>= 1;
    342  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    343  1.26   thorpej 		    (--nbytes) & 0xff);
    344  1.26   thorpej 		bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
    345  1.26   thorpej 		    (nbytes >> 8) & 0xff);
    346  1.23   mycroft 	}
    347   1.1   mycroft 
    348  1.26   thorpej 	isa_dmaunmask(sc, chan);
    349  1.26   thorpej 	return (0);
    350  1.26   thorpej 
    351  1.26   thorpej  lose:
    352  1.26   thorpej 	panic("isa_dmastart");
    353   1.1   mycroft }
    354   1.1   mycroft 
    355   1.1   mycroft void
    356  1.26   thorpej isa_dmaabort(isadev, chan)
    357  1.26   thorpej 	struct device *isadev;
    358   1.4   mycroft 	int chan;
    359   1.1   mycroft {
    360  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    361   1.1   mycroft 
    362  1.26   thorpej 	if (chan < 0 || chan > 7) {
    363  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    364  1.26   thorpej 		panic("isa_dmaabort");
    365  1.26   thorpej 	}
    366   1.1   mycroft 
    367  1.26   thorpej 	isa_dmamask(sc, chan);
    368  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamaps[chan]);
    369  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    370  1.22   mycroft }
    371  1.22   mycroft 
    372  1.26   thorpej bus_size_t
    373  1.26   thorpej isa_dmacount(isadev, chan)
    374  1.26   thorpej 	struct device *isadev;
    375  1.22   mycroft 	int chan;
    376  1.22   mycroft {
    377  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    378  1.22   mycroft 	int waport;
    379  1.26   thorpej 	bus_size_t nbytes;
    380  1.23   mycroft 	int ochan = chan & 3;
    381  1.22   mycroft 
    382  1.26   thorpej 	if (chan < 0 || chan > 7) {
    383  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    384  1.26   thorpej 		panic("isa_dmacount");
    385  1.26   thorpej 	}
    386  1.22   mycroft 
    387  1.26   thorpej 	isa_dmamask(sc, chan);
    388  1.23   mycroft 
    389  1.24   mycroft 	/*
    390  1.24   mycroft 	 * We have to shift the byte count by 1.  If we're in auto-initialize
    391  1.24   mycroft 	 * mode, the count may have wrapped around to the initial value.  We
    392  1.24   mycroft 	 * can't use the TC bit to check for this case, so instead we compare
    393  1.24   mycroft 	 * against the original byte count.
    394  1.24   mycroft 	 * If we're not in auto-initialize mode, then the count will wrap to
    395  1.24   mycroft 	 * -1, so we also handle that case.
    396  1.24   mycroft 	 */
    397  1.24   mycroft 	if ((chan & 4) == 0) {
    398  1.24   mycroft 		waport = DMA1_CHN(ochan);
    399  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    400  1.26   thorpej 		    waport + 1) + 1;
    401  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
    402  1.26   thorpej 		    waport + 1) << 8;
    403  1.24   mycroft 		nbytes &= 0xffff;
    404  1.24   mycroft 	} else {
    405  1.24   mycroft 		waport = DMA2_CHN(ochan);
    406  1.26   thorpej 		nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    407  1.26   thorpej 		    waport + 2) + 1;
    408  1.26   thorpej 		nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
    409  1.26   thorpej 		    waport + 2) << 8;
    410  1.24   mycroft 		nbytes <<= 1;
    411  1.24   mycroft 		nbytes &= 0x1ffff;
    412  1.24   mycroft 	}
    413  1.24   mycroft 
    414  1.26   thorpej 	if (nbytes == sc->sc_dmalength[chan])
    415  1.23   mycroft 		nbytes = 0;
    416  1.22   mycroft 
    417  1.26   thorpej 	isa_dmaunmask(sc, chan);
    418  1.22   mycroft 	return (nbytes);
    419   1.1   mycroft }
    420   1.1   mycroft 
    421  1.13   mycroft int
    422  1.26   thorpej isa_dmafinished(isadev, chan)
    423  1.26   thorpej 	struct device *isadev;
    424   1.5   mycroft 	int chan;
    425   1.1   mycroft {
    426  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    427   1.1   mycroft 
    428  1.26   thorpej 	if (chan < 0 || chan > 7) {
    429  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    430  1.26   thorpej 		panic("isa_dmafinished");
    431  1.26   thorpej 	}
    432   1.1   mycroft 
    433   1.1   mycroft 	/* check that the terminal count was reached */
    434   1.1   mycroft 	if ((chan & 4) == 0)
    435  1.26   thorpej 		sc->sc_dmafinished |= bus_space_read_1(sc->sc_iot,
    436  1.26   thorpej 		    sc->sc_dma1h, DMA1_SR) & 0x0f;
    437   1.1   mycroft 	else
    438  1.26   thorpej 		sc->sc_dmafinished |= (bus_space_read_1(sc->sc_iot,
    439  1.26   thorpej 		    sc->sc_dma2h, DMA2_SR) & 0x0f) << 4;
    440  1.14   mycroft 
    441  1.26   thorpej 	return ((sc->sc_dmafinished & (1 << chan)) != 0);
    442  1.13   mycroft }
    443  1.13   mycroft 
    444  1.13   mycroft void
    445  1.26   thorpej isa_dmadone(isadev, chan)
    446  1.26   thorpej 	struct device *isadev;
    447  1.13   mycroft 	int chan;
    448  1.13   mycroft {
    449  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    450  1.26   thorpej 	bus_dmamap_t dmam;
    451  1.13   mycroft 
    452  1.26   thorpej 	if (chan < 0 || chan > 7) {
    453  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    454  1.26   thorpej 		panic("isa_dmadone");
    455  1.26   thorpej 	}
    456  1.26   thorpej 
    457  1.26   thorpej 	dmam = sc->sc_dmamaps[chan];
    458  1.26   thorpej 
    459  1.26   thorpej 	isa_dmamask(sc, chan);
    460  1.14   mycroft 
    461  1.26   thorpej 	if (isa_dmafinished(isadev, chan) == 0)
    462  1.26   thorpej 		printf("%s: isa_dmadone: channel %d not finished\n",
    463  1.26   thorpej 		    sc->sc_dev.dv_xname, chan);
    464  1.23   mycroft 
    465  1.26   thorpej 	bus_dmamap_sync(sc->sc_dmat, dmam,
    466  1.26   thorpej 	    (sc->sc_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
    467  1.26   thorpej 	    BUS_DMASYNC_POSTWRITE);
    468   1.9   mycroft 
    469  1.26   thorpej 	bus_dmamap_unload(sc->sc_dmat, dmam);
    470  1.26   thorpej 	sc->sc_dmareads &= ~(1 << chan);
    471   1.1   mycroft }
    472   1.1   mycroft 
    473   1.1   mycroft int
    474  1.26   thorpej isa_dmamem_alloc(isadev, chan, size, addrp, flags)
    475  1.26   thorpej 	struct device *isadev;
    476  1.26   thorpej 	int chan;
    477  1.26   thorpej 	bus_size_t size;
    478  1.26   thorpej 	bus_addr_t *addrp;
    479  1.26   thorpej 	int flags;
    480  1.26   thorpej {
    481  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    482  1.26   thorpej 	bus_dma_segment_t seg;
    483  1.26   thorpej 	int error, boundary, rsegs;
    484  1.26   thorpej 
    485  1.26   thorpej 	if (chan < 0 || chan > 7) {
    486  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    487  1.26   thorpej 		panic("isa_dmamem_alloc");
    488   1.1   mycroft 	}
    489  1.26   thorpej 
    490  1.26   thorpej 	boundary = (chan & 4) ? (1 << 17) : (1 << 16);
    491  1.26   thorpej 
    492  1.26   thorpej 	size = round_page(size);
    493  1.26   thorpej 
    494  1.26   thorpej 	error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, boundary,
    495  1.26   thorpej 	    &seg, 1, &rsegs, flags);
    496  1.26   thorpej 	if (error)
    497  1.26   thorpej 		return (error);
    498  1.26   thorpej 
    499  1.26   thorpej 	*addrp = seg.ds_addr;
    500  1.26   thorpej 	return (0);
    501   1.5   mycroft }
    502   1.5   mycroft 
    503  1.26   thorpej void
    504  1.26   thorpej isa_dmamem_free(isadev, chan, addr, size)
    505  1.26   thorpej 	struct device *isadev;
    506  1.26   thorpej 	int chan;
    507  1.26   thorpej 	bus_addr_t addr;
    508  1.26   thorpej 	bus_size_t size;
    509  1.26   thorpej {
    510  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    511  1.26   thorpej 	bus_dma_segment_t seg;
    512  1.26   thorpej 
    513  1.26   thorpej 	if (chan < 0 || chan > 7) {
    514  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    515  1.26   thorpej 		panic("isa_dmamem_free");
    516  1.26   thorpej 	}
    517  1.26   thorpej 
    518  1.26   thorpej 	seg.ds_addr = addr;
    519  1.26   thorpej 	seg.ds_len = size;
    520   1.5   mycroft 
    521  1.26   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, 1);
    522  1.26   thorpej }
    523   1.5   mycroft 
    524  1.26   thorpej int
    525  1.26   thorpej isa_dmamem_map(isadev, chan, addr, size, kvap, flags)
    526  1.26   thorpej 	struct device *isadev;
    527  1.26   thorpej 	int chan;
    528  1.26   thorpej 	bus_addr_t addr;
    529  1.26   thorpej 	bus_size_t size;
    530  1.26   thorpej 	caddr_t *kvap;
    531  1.26   thorpej 	int flags;
    532  1.26   thorpej {
    533  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    534  1.26   thorpej 	bus_dma_segment_t seg;
    535  1.26   thorpej 
    536  1.26   thorpej 	if (chan < 0 || chan > 7) {
    537  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    538  1.26   thorpej 		panic("isa_dmamem_map");
    539   1.5   mycroft 	}
    540   1.5   mycroft 
    541  1.26   thorpej 	seg.ds_addr = addr;
    542  1.26   thorpej 	seg.ds_len = size;
    543  1.26   thorpej 
    544  1.26   thorpej 	return (bus_dmamem_map(sc->sc_dmat, &seg, 1, size, kvap, flags));
    545   1.5   mycroft }
    546   1.5   mycroft 
    547   1.5   mycroft void
    548  1.26   thorpej isa_dmamem_unmap(isadev, chan, kva, size)
    549  1.26   thorpej 	struct device *isadev;
    550  1.26   thorpej 	int chan;
    551  1.26   thorpej 	caddr_t kva;
    552  1.26   thorpej 	size_t size;
    553  1.19  christos {
    554  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    555   1.5   mycroft 
    556  1.26   thorpej 	if (chan < 0 || chan > 7) {
    557  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    558  1.26   thorpej 		panic("isa_dmamem_unmap");
    559   1.5   mycroft 	}
    560  1.26   thorpej 
    561  1.26   thorpej 	bus_dmamem_unmap(sc->sc_dmat, kva, size);
    562  1.26   thorpej }
    563  1.26   thorpej 
    564  1.26   thorpej int
    565  1.26   thorpej isa_dmamem_mmap(isadev, chan, addr, size, off, prot, flags)
    566  1.26   thorpej 	struct device *isadev;
    567  1.26   thorpej 	int chan;
    568  1.26   thorpej 	bus_addr_t addr;
    569  1.26   thorpej 	bus_size_t size;
    570  1.26   thorpej 	int off, prot, flags;
    571  1.26   thorpej {
    572  1.26   thorpej 	struct isa_softc *sc = (struct isa_softc *)isadev;
    573  1.26   thorpej 	bus_dma_segment_t seg;
    574  1.26   thorpej 
    575  1.26   thorpej 	if (chan < 0 || chan > 7) {
    576  1.26   thorpej 		printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
    577  1.26   thorpej 		panic("isa_dmamem_mmap");
    578  1.26   thorpej 	}
    579  1.26   thorpej 
    580  1.26   thorpej 	seg.ds_addr = addr;
    581  1.26   thorpej 	seg.ds_len = size;
    582  1.26   thorpej 
    583  1.26   thorpej 	return (bus_dmamem_mmap(sc->sc_dmat, &seg, 1, off, prot, flags));
    584  1.27  augustss }
    585  1.27  augustss 
    586  1.27  augustss void *
    587  1.27  augustss isa_malloc(isadev, chan, size, pool, flags)
    588  1.27  augustss 	struct device *isadev;
    589  1.27  augustss 	int chan;
    590  1.27  augustss 	size_t size;
    591  1.27  augustss 	int pool;
    592  1.27  augustss 	int flags;
    593  1.27  augustss {
    594  1.27  augustss 	bus_addr_t addr;
    595  1.27  augustss 	caddr_t kva;
    596  1.27  augustss 	int bflags;
    597  1.27  augustss 	struct isa_mem *m;
    598  1.27  augustss 
    599  1.27  augustss 	bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
    600  1.27  augustss 
    601  1.27  augustss 	if (isa_dmamem_alloc(isadev, chan, size, &addr, bflags))
    602  1.27  augustss 		return 0;
    603  1.27  augustss 	if (isa_dmamem_map(isadev, chan, addr, size, &kva, bflags)) {
    604  1.27  augustss 		isa_dmamem_free(isadev, chan, addr, size);
    605  1.27  augustss 		return 0;
    606  1.27  augustss 	}
    607  1.27  augustss 	m = malloc(sizeof(*m), pool, flags);
    608  1.27  augustss 	if (m == 0) {
    609  1.27  augustss 		isa_dmamem_unmap(isadev, chan, kva, size);
    610  1.27  augustss 		isa_dmamem_free(isadev, chan, addr, size);
    611  1.27  augustss 		return 0;
    612  1.27  augustss 	}
    613  1.27  augustss 	m->isadev = isadev;
    614  1.27  augustss 	m->chan = chan;
    615  1.27  augustss 	m->size = size;
    616  1.27  augustss 	m->addr = addr;
    617  1.27  augustss 	m->kva = kva;
    618  1.27  augustss 	m->next = isa_mem_head;
    619  1.27  augustss 	isa_mem_head = m;
    620  1.27  augustss 	return (void *)kva;
    621  1.27  augustss }
    622  1.27  augustss 
    623  1.27  augustss void
    624  1.27  augustss isa_free(addr, pool)
    625  1.27  augustss 	void *addr;
    626  1.27  augustss 	int pool;
    627  1.27  augustss {
    628  1.27  augustss 	struct isa_mem **mp, *m;
    629  1.27  augustss 	caddr_t kva = (caddr_t)addr;
    630  1.27  augustss 
    631  1.27  augustss 	for(mp = &isa_mem_head; *mp && (*mp)->kva != kva; mp = &(*mp)->next)
    632  1.27  augustss 		;
    633  1.27  augustss 	m = *mp;
    634  1.27  augustss 	if (!m) {
    635  1.27  augustss 		printf("isa_free: freeing unallocted memory\n");
    636  1.27  augustss 		return;
    637  1.27  augustss 	}
    638  1.27  augustss 	*mp = m->next;
    639  1.27  augustss 	isa_dmamem_unmap(m->isadev, m->chan, kva, m->size);
    640  1.27  augustss 	isa_dmamem_free(m->isadev, m->chan, m->addr, m->size);
    641  1.27  augustss 	free(m, pool);
    642   1.1   mycroft }
    643