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