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