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