Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.2
      1  1.2  soda /*	$NetBSD: isadma.c,v 1.2 2000/01/23 21:01:59 soda Exp $	*/
      2  1.1  soda /*	$OpenBSD: isadma.c,v 1.2 1996/11/23 21:45:34 kstailey Exp $	*/
      3  1.2  soda /*	NetBSD: isadma.c,v 1.19 1996/04/29 20:03:26 christos Exp 	*/
      4  1.1  soda 
      5  1.1  soda #include <sys/param.h>
      6  1.1  soda #include <sys/systm.h>
      7  1.1  soda #include <sys/device.h>
      8  1.1  soda #include <sys/file.h>
      9  1.1  soda #include <sys/buf.h>
     10  1.1  soda #include <sys/syslog.h>
     11  1.1  soda #include <sys/malloc.h>
     12  1.1  soda #include <sys/uio.h>
     13  1.1  soda 
     14  1.1  soda #include <vm/vm.h>
     15  1.1  soda 
     16  1.1  soda #include <machine/pio.h>
     17  1.1  soda 
     18  1.1  soda #include <dev/isa/isareg.h>
     19  1.1  soda #include <dev/isa/isavar.h>
     20  1.1  soda #include <dev/isa/isadmavar.h>
     21  1.1  soda #include <arch/arc/isa/isadmareg.h> /*XXX*/
     22  1.1  soda 
     23  1.1  soda struct dma_info {
     24  1.1  soda 	int flags;
     25  1.1  soda 	int active;
     26  1.1  soda 	caddr_t addr;
     27  1.1  soda 	vm_size_t nbytes;
     28  1.1  soda 	struct isadma_seg phys[1];
     29  1.1  soda };
     30  1.1  soda 
     31  1.1  soda static struct isadma_softc *isadma_sc;	/*XXX ugly */
     32  1.1  soda static struct dma_info dma_info[8];
     33  1.1  soda static u_int8_t dma_finished;
     34  1.1  soda 
     35  1.1  soda /* high byte of address is stored in this port for i-th dma channel */
     36  1.1  soda static int dmapageport[2][4] = {
     37  1.1  soda 	{0x87, 0x83, 0x81, 0x82},
     38  1.1  soda 	{0x8f, 0x8b, 0x89, 0x8a}
     39  1.1  soda };
     40  1.1  soda 
     41  1.1  soda static u_int8_t dmamode[4] = {
     42  1.1  soda 	DMA37MD_READ | DMA37MD_SINGLE,
     43  1.1  soda 	DMA37MD_WRITE | DMA37MD_SINGLE,
     44  1.1  soda 	DMA37MD_READ | DMA37MD_LOOP,
     45  1.1  soda 	DMA37MD_WRITE | DMA37MD_LOOP
     46  1.1  soda };
     47  1.1  soda 
     48  1.2  soda int isadmamatch __P((struct device *, struct cfdata *, void *));
     49  1.1  soda void isadmaattach __P((struct device *, struct device *, void *));
     50  1.1  soda int isadmaprint __P((void *, const char *));
     51  1.1  soda 
     52  1.1  soda struct isadma_softc {
     53  1.1  soda 	struct device sc_dev;
     54  1.2  soda 	bus_space_tag_t sc_iot;
     55  1.2  soda 	bus_space_handle_t sc_ioh1;
     56  1.2  soda 	bus_space_handle_t sc_ioh2;
     57  1.1  soda }
     58  1.1  soda 
     59  1.1  soda struct cfattach isadma_ca = {
     60  1.1  soda 	sizeof(struct isadma_softc), isadmamatch, isadmaattach
     61  1.1  soda };
     62  1.1  soda 
     63  1.1  soda struct cfdriver isadma_cd = {
     64  1.1  soda 	NULL, "isadma", DV_DULL, 1
     65  1.1  soda };
     66  1.1  soda 
     67  1.1  soda isadmamatch(parent, match, aux)
     68  1.1  soda 	struct device *parent;
     69  1.2  soda 	struct cfdata *match;
     70  1.2  soda 	void *aux;
     71  1.1  soda {
     72  1.1  soda 	struct isa_attach_args *ia = aux;
     73  1.1  soda 
     74  1.1  soda 	/* Sure we exist */
     75  1.1  soda 	ia->ia_iosize = 0;
     76  1.1  soda 	return (1);
     77  1.1  soda }
     78  1.1  soda 
     79  1.1  soda void
     80  1.1  soda isadmaattach(parent, self, aux)
     81  1.1  soda 	struct device *parent, *self;
     82  1.1  soda 	void *aux;
     83  1.1  soda {
     84  1.1  soda 	struct isadma_softc *sc = (void *)self;
     85  1.1  soda 	struct isa_attach_args *ia = aux;
     86  1.2  soda 	bus_space_tag_t iot;
     87  1.2  soda 	bus_space_handle_t ioh;
     88  1.1  soda 
     89  1.1  soda 	printf("\n");
     90  1.1  soda 
     91  1.2  soda 	iot = sc->sc_iot = ia->ia_iot;
     92  1.2  soda 	if (bus_space_map(iot, IO_DMA1, DMA_NREGS, 0, &ioh))
     93  1.1  soda 		panic("isadmaattach: couldn't map I/O ports");
     94  1.1  soda 	sc->sc_ioh1 = ioh;
     95  1.2  soda 	if (bus_space_map(iot, IO_DMA2, DMA_NREGS*2, 0, &ioh))
     96  1.1  soda 		panic("isadmaattach: couldn't map I/O ports");
     97  1.1  soda 	sc->sc_ioh2 = ioh;
     98  1.1  soda 	isadma_sc = sc;
     99  1.1  soda }
    100  1.1  soda 
    101  1.1  soda /*
    102  1.1  soda  * isadma_cascade(): program 8237 DMA controller channel to accept
    103  1.1  soda  * external dma control by a board.
    104  1.1  soda  */
    105  1.1  soda void
    106  1.1  soda isadma_cascade(chan)
    107  1.1  soda 	int chan;
    108  1.1  soda {
    109  1.1  soda 	struct isadma_softc *sc = isadma_sc;
    110  1.2  soda 	bus_space_tag_t iot = sc->sc_iot;
    111  1.1  soda 
    112  1.1  soda #ifdef ISADMA_DEBUG
    113  1.1  soda 	if (chan < 0 || chan > 7)
    114  1.1  soda 		panic("isadma_cascade: impossible request");
    115  1.1  soda #endif
    116  1.1  soda 
    117  1.1  soda 	/* set dma channel mode, and set dma channel mode */
    118  1.1  soda 	if ((chan & 4) == 0) {
    119  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_MODE, chan | DMA37MD_CASCADE);
    120  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, chan);
    121  1.1  soda 	} else {
    122  1.1  soda 		chan &= 3;
    123  1.1  soda 
    124  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_MODE, chan | DMA37MD_CASCADE);
    125  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, chan);
    126  1.1  soda 	}
    127  1.1  soda }
    128  1.1  soda 
    129  1.1  soda /*
    130  1.1  soda  * isadma_start(): program 8237 DMA controller channel, avoid page alignment
    131  1.1  soda  * problems by using a bounce buffer.
    132  1.1  soda  */
    133  1.1  soda void
    134  1.1  soda isadma_start(addr, nbytes, chan, flags)
    135  1.1  soda 	caddr_t addr;
    136  1.1  soda 	vm_size_t nbytes;
    137  1.1  soda 	int chan;
    138  1.1  soda 	int flags;
    139  1.1  soda {
    140  1.1  soda 	struct dma_info *di;
    141  1.1  soda 	int waport;
    142  1.1  soda 	int mflags;
    143  1.1  soda 	vm_size_t size;
    144  1.1  soda 	struct isadma_softc *sc = isadma_sc;
    145  1.2  soda 	bus_space_tag_t iot = sc->sc_iot;
    146  1.2  soda 	bus_space_handle_t ioh;
    147  1.1  soda 
    148  1.1  soda #ifdef ISADMA_DEBUG
    149  1.1  soda 	if (chan < 0 || chan > 7 ||
    150  1.1  soda 	    (((flags & DMAMODE_READ) != 0) + ((flags & DMAMODE_WRITE) != 0) +
    151  1.1  soda 	    ((flags & DMAMODE_LOOP) != 0) != 1) ||
    152  1.1  soda 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
    153  1.1  soda 	    (nbytes >= (1<<16))))
    154  1.1  soda 		panic("isadma_start: impossible request");
    155  1.1  soda #endif
    156  1.1  soda 
    157  1.1  soda 	di = dma_info+chan;
    158  1.1  soda 	if (di->active) {
    159  1.1  soda 		log(LOG_ERR,"isadma_start: old request active on %d\n",chan);
    160  1.1  soda 		isadma_abort(chan);
    161  1.1  soda 	}
    162  1.1  soda 
    163  1.1  soda 	di->flags = flags;
    164  1.1  soda 	di->active = 1;
    165  1.1  soda 	di->addr = addr;
    166  1.1  soda 	di->nbytes = nbytes;
    167  1.1  soda 
    168  1.1  soda 	mflags = ISADMA_MAP_WAITOK | ISADMA_MAP_BOUNCE | ISADMA_MAP_CONTIG;
    169  1.1  soda 	mflags |= (chan & 4) ? ISADMA_MAP_16BIT : ISADMA_MAP_8BIT;
    170  1.1  soda 
    171  1.1  soda 	if (isadma_map(addr, nbytes, di->phys, mflags) != 1)
    172  1.1  soda 		panic("isadma_start: cannot map");
    173  1.1  soda 
    174  1.1  soda 	/* XXX Will this do what we want with DMAMODE_LOOP?  */
    175  1.1  soda 	if ((flags & DMAMODE_READ) == 0)
    176  1.1  soda 		isadma_copytobuf(addr, nbytes, 1, di->phys);
    177  1.1  soda 
    178  1.1  soda 	dma_finished &= ~(1 << chan);
    179  1.1  soda 
    180  1.1  soda 	if ((chan & 4) == 0) {
    181  1.1  soda 		ioh = sc->sc_ioh1;
    182  1.1  soda 		/*
    183  1.1  soda 		 * Program one of DMA channels 0..3.  These are
    184  1.1  soda 		 * byte mode channels.
    185  1.1  soda 		 */
    186  1.1  soda 		/* set dma channel mode, and reset address ff */
    187  1.2  soda 		bus_space_write_1(iot, ioh, DMA1_MODE, chan | dmamode[flags]);
    188  1.2  soda 		bus_space_write_1(iot, ioh, DMA1_FFC, 0);
    189  1.1  soda 
    190  1.1  soda 		/* send start address */
    191  1.1  soda 		waport = DMA1_CHN(chan);
    192  1.1  soda 		outb(dmapageport[0][chan], di->phys[0].addr>>16);
    193  1.1  soda 		outb(waport, di->phys[0].addr);
    194  1.1  soda 		outb(waport, di->phys[0].addr>>8);
    195  1.1  soda 
    196  1.1  soda 		/* send count */
    197  1.1  soda 		outb(waport + 1, --nbytes);
    198  1.1  soda 		outb(waport + 1, nbytes>>8);
    199  1.1  soda 
    200  1.1  soda 		/* unmask channel */
    201  1.2  soda 		bus_space_write_1(iot, ioh, DMA1_SMSK, chan | DMA37SM_CLEAR);
    202  1.1  soda 	} else {
    203  1.1  soda 		ioh = sc->sc_ioh2;
    204  1.1  soda 		/*
    205  1.1  soda 		 * Program one of DMA channels 4..7.  These are
    206  1.1  soda 		 * word mode channels.
    207  1.1  soda 		 */
    208  1.1  soda 		/* set dma channel mode, and reset address ff */
    209  1.2  soda 		bus_space_write_1(iot, ioh, DMA2_MODE, (chan & 3) | dmamode[flags]);
    210  1.2  soda 		bus_space_write_1(iot, ioh, DMA2_FFC, 0);
    211  1.1  soda 
    212  1.1  soda 		/* send start address */
    213  1.1  soda 		waport = DMA2_CHN(chan & 3);
    214  1.1  soda 		outb(dmapageport[1][chan], di->phys[0].addr>>16);
    215  1.1  soda 		outb(waport, di->phys[0].addr>>1);
    216  1.1  soda 		outb(waport, di->phys[0].addr>>9);
    217  1.1  soda 
    218  1.1  soda 		/* send count */
    219  1.1  soda 		nbytes >>= 1;
    220  1.1  soda 		outb(waport + 2, --nbytes);
    221  1.1  soda 		outb(waport + 2, nbytes>>8);
    222  1.1  soda 
    223  1.1  soda 		/* unmask channel */
    224  1.2  soda 		bus_space_write_1(iot, ioh, DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
    225  1.1  soda 	}
    226  1.1  soda }
    227  1.1  soda 
    228  1.1  soda void
    229  1.1  soda isadma_abort(chan)
    230  1.1  soda 	int chan;
    231  1.1  soda {
    232  1.1  soda 	struct dma_info *di;
    233  1.1  soda 	struct isadma_softc *sc = isadma_sc;
    234  1.2  soda 	bus_space_tag_t iot = sc->sc_iot;
    235  1.1  soda 
    236  1.1  soda #ifdef ISADMA_DEBUG
    237  1.1  soda 	if (chan < 0 || chan > 7)
    238  1.1  soda 		panic("isadma_abort: impossible request");
    239  1.1  soda #endif
    240  1.1  soda 
    241  1.1  soda 	di = dma_info+chan;
    242  1.1  soda 	if (! di->active) {
    243  1.1  soda 		log(LOG_ERR,"isadma_abort: no request active on %d\n",chan);
    244  1.1  soda 		return;
    245  1.1  soda 	}
    246  1.1  soda 
    247  1.1  soda 	/* mask channel */
    248  1.1  soda 	if ((chan & 4) == 0)
    249  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, DMA37SM_SET | chan);
    250  1.1  soda 	else
    251  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, DMA37SM_SET | (chan & 3));
    252  1.1  soda 
    253  1.1  soda 	isadma_unmap(di->addr, di->nbytes, 1, di->phys);
    254  1.1  soda 	di->active = 0;
    255  1.1  soda }
    256  1.1  soda 
    257  1.1  soda int
    258  1.1  soda isadma_finished(chan)
    259  1.1  soda 	int chan;
    260  1.1  soda {
    261  1.1  soda 	struct isadma_softc *sc = isadma_sc;
    262  1.2  soda 	bus_space_tag_t iot = sc->sc_iot;
    263  1.1  soda 
    264  1.1  soda #ifdef ISADMA_DEBUG
    265  1.1  soda 	if (chan < 0 || chan > 7)
    266  1.1  soda 		panic("isadma_finished: impossible request");
    267  1.1  soda #endif
    268  1.1  soda 
    269  1.1  soda 	/* check that the terminal count was reached */
    270  1.1  soda 	if ((chan & 4) == 0)
    271  1.2  soda 		dma_finished |= bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & 0x0f;
    272  1.1  soda 	else
    273  1.2  soda 		dma_finished |= (bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & 0x0f) << 4;
    274  1.1  soda 
    275  1.1  soda 	return ((dma_finished & (1 << chan)) != 0);
    276  1.1  soda }
    277  1.1  soda 
    278  1.1  soda void
    279  1.1  soda isadma_done(chan)
    280  1.1  soda 	int chan;
    281  1.1  soda {
    282  1.1  soda 	struct dma_info *di;
    283  1.1  soda 	u_char tc;
    284  1.1  soda 	struct isadma_softc *sc = isadma_sc;
    285  1.2  soda 	bus_space_tag_t iot = sc->sc_iot;
    286  1.1  soda 
    287  1.1  soda #ifdef DIAGNOSTIC
    288  1.1  soda 	if (chan < 0 || chan > 7)
    289  1.1  soda 		panic("isadma_done: impossible request");
    290  1.1  soda #endif
    291  1.1  soda 
    292  1.1  soda 	di = dma_info+chan;
    293  1.1  soda 	if (! di->active) {
    294  1.1  soda 		log(LOG_ERR,"isadma_done: no request active on %d\n",chan);
    295  1.1  soda 		return;
    296  1.1  soda 	}
    297  1.1  soda 
    298  1.1  soda 	/* check that the terminal count was reached */
    299  1.1  soda 	if ((chan & 4) == 0)
    300  1.2  soda 		tc = bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & (1 << chan);
    301  1.1  soda 	else
    302  1.2  soda 		tc = bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & (1 << (chan & 3));
    303  1.1  soda 	if (tc == 0)
    304  1.1  soda 		/* XXX probably should panic or something */
    305  1.1  soda 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    306  1.1  soda 
    307  1.1  soda 	/* mask channel */
    308  1.1  soda 	if ((chan & 4) == 0)
    309  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, DMA37SM_SET | chan);
    310  1.1  soda 	else
    311  1.2  soda 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, DMA37SM_SET | (chan & 3));
    312  1.1  soda 
    313  1.1  soda 	/* XXX Will this do what we want with DMAMODE_LOOP?  */
    314  1.1  soda 	if (di->flags & DMAMODE_READ)
    315  1.1  soda 		isadma_copyfrombuf(di->addr, di->nbytes, 1, di->phys);
    316  1.1  soda 
    317  1.1  soda 	isadma_unmap(di->addr, di->nbytes, 1, di->phys);
    318  1.1  soda 	di->active = 0;
    319  1.1  soda }
    320