1 /* $NetBSD: isadma.c,v 1.10 2007/03/04 05:59:35 christos 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.10 2007/03/04 05:59:35 christos 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 void *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 uint8_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 uint8_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(struct device *, struct cfdata *, void *); 52 void isadmaattach(struct device *, struct device *, void *); 53 int isadmaprint(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(struct device *parent, struct cfdata *match, void *aux) 70 { 71 struct isa_attach_args *ia = aux; 72 73 /* Sure we exist */ 74 ia->ia_iosize = 0; 75 return 1; 76 } 77 78 void 79 isadmaattach(struct device *parent, struct device *self, void *aux) 80 { 81 struct isadma_softc *sc = (void *)self; 82 struct isa_attach_args *ia = aux; 83 bus_space_tag_t iot; 84 bus_space_handle_t ioh; 85 86 printf("\n"); 87 88 iot = sc->sc_iot = ia->ia_iot; 89 if (bus_space_map(iot, IO_DMA1, DMA_NREGS, 0, &ioh)) 90 panic("isadmaattach: couldn't map I/O ports"); 91 sc->sc_ioh1 = ioh; 92 if (bus_space_map(iot, IO_DMA2, DMA_NREGS*2, 0, &ioh)) 93 panic("isadmaattach: couldn't map I/O ports"); 94 sc->sc_ioh2 = ioh; 95 isadma_sc = sc; 96 } 97 98 /* 99 * isadma_cascade(): program 8237 DMA controller channel to accept 100 * external dma control by a board. 101 */ 102 void 103 isadma_cascade(int chan) 104 { 105 struct isadma_softc *sc = isadma_sc; 106 bus_space_tag_t iot = sc->sc_iot; 107 108 #ifdef ISADMA_DEBUG 109 if (chan < 0 || chan > 7) 110 panic("isadma_cascade: impossible request"); 111 #endif 112 113 /* set dma channel mode, and set dma channel mode */ 114 if ((chan & 4) == 0) { 115 bus_space_write_1(iot, sc->sc_ioh1, DMA1_MODE, 116 chan | DMA37MD_CASCADE); 117 bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, chan); 118 } else { 119 chan &= 3; 120 121 bus_space_write_1(iot, sc->sc_ioh2, DMA2_MODE, 122 chan | DMA37MD_CASCADE); 123 bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, chan); 124 } 125 } 126 127 /* 128 * isadma_start(): program 8237 DMA controller channel, avoid page alignment 129 * problems by using a bounce buffer. 130 */ 131 void 132 isadma_start(void *addr, bus_size_t nbytes, int chan, int flags) 133 { 134 struct dma_info *di; 135 int waport; 136 int mflags; 137 struct isadma_softc *sc = isadma_sc; 138 bus_space_tag_t iot = sc->sc_iot; 139 bus_space_handle_t ioh; 140 141 #ifdef ISADMA_DEBUG 142 if (chan < 0 || chan > 7 || 143 (((flags & DMAMODE_READ) != 0) + ((flags & DMAMODE_WRITE) != 0) + 144 ((flags & DMAMODE_LOOP) != 0) != 1) || 145 ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) : 146 (nbytes >= (1<<16)))) 147 panic("isadma_start: impossible request"); 148 #endif 149 150 di = dma_info+chan; 151 if (di->active) { 152 log(LOG_ERR,"isadma_start: old request active on %d\n",chan); 153 isadma_abort(chan); 154 } 155 156 di->flags = flags; 157 di->active = 1; 158 di->addr = addr; 159 di->nbytes = nbytes; 160 161 mflags = ISADMA_MAP_WAITOK | ISADMA_MAP_BOUNCE | ISADMA_MAP_CONTIG; 162 mflags |= (chan & 4) ? ISADMA_MAP_16BIT : ISADMA_MAP_8BIT; 163 164 if (isadma_map(addr, nbytes, di->phys, mflags) != 1) 165 panic("isadma_start: cannot map"); 166 167 /* XXX Will this do what we want with DMAMODE_LOOP? */ 168 if ((flags & DMAMODE_READ) == 0) 169 isadma_copytobuf(addr, nbytes, 1, di->phys); 170 171 dma_finished &= ~(1 << chan); 172 173 if ((chan & 4) == 0) { 174 ioh = sc->sc_ioh1; 175 /* 176 * Program one of DMA channels 0..3. These are 177 * byte mode channels. 178 */ 179 /* set dma channel mode, and reset address ff */ 180 bus_space_write_1(iot, ioh, DMA1_MODE, chan | dmamode[flags]); 181 bus_space_write_1(iot, ioh, DMA1_FFC, 0); 182 183 /* send start address */ 184 waport = DMA1_CHN(chan); 185 outb(dmapageport[0][chan], di->phys[0].addr>>16); 186 outb(waport, di->phys[0].addr); 187 outb(waport, di->phys[0].addr>>8); 188 189 /* send count */ 190 outb(waport + 1, --nbytes); 191 outb(waport + 1, nbytes>>8); 192 193 /* unmask channel */ 194 bus_space_write_1(iot, ioh, DMA1_SMSK, chan | DMA37SM_CLEAR); 195 } else { 196 ioh = sc->sc_ioh2; 197 /* 198 * Program one of DMA channels 4..7. These are 199 * word mode channels. 200 */ 201 /* set dma channel mode, and reset address ff */ 202 bus_space_write_1(iot, ioh, DMA2_MODE, 203 (chan & 3) | dmamode[flags]); 204 bus_space_write_1(iot, ioh, DMA2_FFC, 0); 205 206 /* send start address */ 207 waport = DMA2_CHN(chan & 3); 208 outb(dmapageport[1][chan], di->phys[0].addr>>16); 209 outb(waport, di->phys[0].addr>>1); 210 outb(waport, di->phys[0].addr>>9); 211 212 /* send count */ 213 nbytes >>= 1; 214 outb(waport + 2, --nbytes); 215 outb(waport + 2, nbytes>>8); 216 217 /* unmask channel */ 218 bus_space_write_1(iot, ioh, DMA2_SMSK, 219 (chan & 3) | DMA37SM_CLEAR); 220 } 221 } 222 223 void 224 isadma_abort(int chan) 225 { 226 struct dma_info *di; 227 struct isadma_softc *sc = isadma_sc; 228 bus_space_tag_t iot = sc->sc_iot; 229 230 #ifdef ISADMA_DEBUG 231 if (chan < 0 || chan > 7) 232 panic("isadma_abort: impossible request"); 233 #endif 234 235 di = dma_info+chan; 236 if (! di->active) { 237 log(LOG_ERR,"isadma_abort: no request active on %d\n",chan); 238 return; 239 } 240 241 /* mask channel */ 242 if ((chan & 4) == 0) 243 bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, 244 DMA37SM_SET | chan); 245 else 246 bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, 247 DMA37SM_SET | (chan & 3)); 248 249 isadma_unmap(di->addr, di->nbytes, 1, di->phys); 250 di->active = 0; 251 } 252 253 int 254 isadma_finished(int chan) 255 { 256 struct isadma_softc *sc = isadma_sc; 257 bus_space_tag_t iot = sc->sc_iot; 258 259 #ifdef ISADMA_DEBUG 260 if (chan < 0 || chan > 7) 261 panic("isadma_finished: impossible request"); 262 #endif 263 264 /* check that the terminal count was reached */ 265 if ((chan & 4) == 0) 266 dma_finished |= 267 bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & 0x0f; 268 else 269 dma_finished |= 270 (bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & 0x0f) << 4; 271 272 return (dma_finished & (1 << chan)) != 0; 273 } 274 275 void 276 isadma_done(int chan) 277 { 278 struct dma_info *di; 279 u_char tc; 280 struct isadma_softc *sc = isadma_sc; 281 bus_space_tag_t iot = sc->sc_iot; 282 283 #ifdef DIAGNOSTIC 284 if (chan < 0 || chan > 7) 285 panic("isadma_done: impossible request"); 286 #endif 287 288 di = dma_info+chan; 289 if (! di->active) { 290 log(LOG_ERR,"isadma_done: no request active on %d\n",chan); 291 return; 292 } 293 294 /* check that the terminal count was reached */ 295 if ((chan & 4) == 0) 296 tc = bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & (1 << chan); 297 else 298 tc = bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & 299 (1 << (chan & 3)); 300 if (tc == 0) 301 /* XXX probably should panic or something */ 302 log(LOG_ERR, "dma channel %d not finished\n", chan); 303 304 /* mask channel */ 305 if ((chan & 4) == 0) 306 bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, 307 DMA37SM_SET | chan); 308 else 309 bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, 310 DMA37SM_SET | (chan & 3)); 311 312 /* XXX Will this do what we want with DMAMODE_LOOP? */ 313 if (di->flags & DMAMODE_READ) 314 isadma_copyfrombuf(di->addr, di->nbytes, 1, di->phys); 315 316 isadma_unmap(di->addr, di->nbytes, 1, di->phys); 317 di->active = 0; 318 } 319