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