isadma.c revision 1.37 1 1.37 thorpej /* $NetBSD: isadma.c,v 1.37 1998/06/28 06:59:35 thorpej Exp $ */
2 1.26 thorpej
3 1.26 thorpej /*-
4 1.33 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.26 thorpej * All rights reserved.
6 1.26 thorpej *
7 1.26 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.26 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.26 thorpej * NASA Ames Research Center.
10 1.26 thorpej *
11 1.26 thorpej * Redistribution and use in source and binary forms, with or without
12 1.26 thorpej * modification, are permitted provided that the following conditions
13 1.26 thorpej * are met:
14 1.26 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.26 thorpej * notice, this list of conditions and the following disclaimer.
16 1.26 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.26 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.26 thorpej * documentation and/or other materials provided with the distribution.
19 1.26 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.26 thorpej * must display the following acknowledgement:
21 1.26 thorpej * This product includes software developed by the NetBSD
22 1.26 thorpej * Foundation, Inc. and its contributors.
23 1.26 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.26 thorpej * contributors may be used to endorse or promote products derived
25 1.26 thorpej * from this software without specific prior written permission.
26 1.26 thorpej *
27 1.26 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.26 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.26 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.26 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.26 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.26 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.26 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.26 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.26 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.26 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.26 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.26 thorpej */
39 1.26 thorpej
40 1.26 thorpej /*
41 1.26 thorpej * Device driver for the ISA on-board DMA controller.
42 1.26 thorpej */
43 1.8 cgd
44 1.3 mycroft #include <sys/param.h>
45 1.3 mycroft #include <sys/systm.h>
46 1.19 christos #include <sys/proc.h>
47 1.26 thorpej #include <sys/device.h>
48 1.34 thorpej #include <sys/malloc.h>
49 1.5 mycroft
50 1.3 mycroft #include <vm/vm.h>
51 1.3 mycroft
52 1.26 thorpej #include <machine/bus.h>
53 1.3 mycroft
54 1.12 cgd #include <dev/isa/isareg.h>
55 1.26 thorpej #include <dev/isa/isavar.h>
56 1.12 cgd #include <dev/isa/isadmavar.h>
57 1.12 cgd #include <dev/isa/isadmareg.h>
58 1.1 mycroft
59 1.34 thorpej struct isa_mem *isa_mem_head;
60 1.27 augustss
61 1.26 thorpej /*
62 1.26 thorpej * High byte of DMA address is stored in this DMAPG register for
63 1.26 thorpej * the Nth DMA channel.
64 1.26 thorpej */
65 1.18 mycroft static int dmapageport[2][4] = {
66 1.26 thorpej {0x7, 0x3, 0x1, 0x2},
67 1.26 thorpej {0xf, 0xb, 0x9, 0xa}
68 1.15 mycroft };
69 1.15 mycroft
70 1.37 thorpej static u_int8_t dmamode[] = {
71 1.37 thorpej /* write to device/read from device */
72 1.17 mycroft DMA37MD_READ | DMA37MD_SINGLE,
73 1.15 mycroft DMA37MD_WRITE | DMA37MD_SINGLE,
74 1.37 thorpej
75 1.37 thorpej /* write to device/read from device - DMAMODE_LOOP */
76 1.25 mycroft DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
77 1.37 thorpej DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP,
78 1.37 thorpej
79 1.37 thorpej /* write to device/read from device - DMAMODE_LOOPDEMAND */
80 1.37 thorpej DMA37MD_READ | DMA37MD_DEMAND | DMA37MD_LOOP,
81 1.37 thorpej DMA37MD_WRITE | DMA37MD_DEMAND | DMA37MD_LOOP,
82 1.15 mycroft };
83 1.1 mycroft
84 1.34 thorpej static inline void _isa_dmaunmask __P((struct isa_dma_state *, int));
85 1.34 thorpej static inline void _isa_dmamask __P((struct isa_dma_state *, int));
86 1.19 christos
87 1.26 thorpej static inline void
88 1.34 thorpej _isa_dmaunmask(ids, chan)
89 1.34 thorpej struct isa_dma_state *ids;
90 1.23 mycroft int chan;
91 1.23 mycroft {
92 1.23 mycroft int ochan = chan & 3;
93 1.23 mycroft
94 1.35 thorpej ISA_DMA_MASK_CLR(ids, chan);
95 1.35 thorpej
96 1.35 thorpej /*
97 1.35 thorpej * If DMA is frozen, don't unmask it now. It will be
98 1.35 thorpej * unmasked when DMA is thawed again.
99 1.35 thorpej */
100 1.35 thorpej if (ids->ids_frozen)
101 1.35 thorpej return;
102 1.35 thorpej
103 1.23 mycroft /* set dma channel mode, and set dma channel mode */
104 1.23 mycroft if ((chan & 4) == 0)
105 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
106 1.26 thorpej DMA1_SMSK, ochan | DMA37SM_CLEAR);
107 1.23 mycroft else
108 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
109 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_CLEAR);
110 1.23 mycroft }
111 1.23 mycroft
112 1.26 thorpej static inline void
113 1.34 thorpej _isa_dmamask(ids, chan)
114 1.34 thorpej struct isa_dma_state *ids;
115 1.23 mycroft int chan;
116 1.23 mycroft {
117 1.23 mycroft int ochan = chan & 3;
118 1.23 mycroft
119 1.35 thorpej ISA_DMA_MASK_SET(ids, chan);
120 1.35 thorpej
121 1.35 thorpej /*
122 1.35 thorpej * XXX Should we avoid masking the channel if DMA is
123 1.35 thorpej * XXX frozen? It seems like what we're doing should
124 1.35 thorpej * XXX be safe, and we do need to reset FFC...
125 1.35 thorpej */
126 1.35 thorpej
127 1.23 mycroft /* set dma channel mode, and set dma channel mode */
128 1.23 mycroft if ((chan & 4) == 0) {
129 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
130 1.26 thorpej DMA1_SMSK, ochan | DMA37SM_SET);
131 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
132 1.26 thorpej DMA1_FFC, 0);
133 1.23 mycroft } else {
134 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
135 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_SET);
136 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
137 1.26 thorpej DMA2_FFC, 0);
138 1.23 mycroft }
139 1.23 mycroft }
140 1.23 mycroft
141 1.1 mycroft /*
142 1.34 thorpej * _isa_dmainit(): Initialize the isa_dma_state for this chipset.
143 1.34 thorpej */
144 1.34 thorpej void
145 1.34 thorpej _isa_dmainit(ids, bst, dmat, dev)
146 1.34 thorpej struct isa_dma_state *ids;
147 1.34 thorpej bus_space_tag_t bst;
148 1.34 thorpej bus_dma_tag_t dmat;
149 1.34 thorpej struct device *dev;
150 1.34 thorpej {
151 1.34 thorpej
152 1.34 thorpej ids->ids_dev = dev;
153 1.34 thorpej
154 1.34 thorpej if (ids->ids_initialized) {
155 1.34 thorpej /*
156 1.34 thorpej * Some systems may have e.g. `ofisa' (OpenFirmware
157 1.34 thorpej * configuration of ISA bus) and a regular `isa'.
158 1.34 thorpej * We allow both to call the initialization function,
159 1.34 thorpej * and take the device name from the last caller
160 1.34 thorpej * (assuming it will be the indirect ISA bus). Since
161 1.34 thorpej * `ofisa' and `isa' are the same bus with different
162 1.34 thorpej * configuration mechanisms, the space and dma tags
163 1.34 thorpej * must be the same!
164 1.34 thorpej */
165 1.34 thorpej if (ids->ids_bst != bst || ids->ids_dmat != dmat)
166 1.34 thorpej panic("_isa_dmainit: inconsistent ISA tags");
167 1.34 thorpej } else {
168 1.34 thorpej ids->ids_bst = bst;
169 1.34 thorpej ids->ids_dmat = dmat;
170 1.34 thorpej
171 1.34 thorpej /*
172 1.34 thorpej * Map the registers used by the ISA DMA controller.
173 1.34 thorpej */
174 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA1, DMA1_IOSIZE, 0,
175 1.34 thorpej &ids->ids_dma1h))
176 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #1");
177 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA2, DMA2_IOSIZE, 0,
178 1.34 thorpej &ids->ids_dma2h))
179 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #2");
180 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMAPG, 0xf, 0,
181 1.34 thorpej &ids->ids_dmapgh))
182 1.34 thorpej panic("_isa_dmainit: unable to map DMA page registers");
183 1.34 thorpej
184 1.35 thorpej /*
185 1.35 thorpej * All 8 DMA channels start out "masked".
186 1.35 thorpej */
187 1.35 thorpej ids->ids_masked = 0xff;
188 1.35 thorpej
189 1.34 thorpej ids->ids_initialized = 1;
190 1.34 thorpej }
191 1.34 thorpej }
192 1.34 thorpej
193 1.34 thorpej /*
194 1.34 thorpej * _isa_dmacascade(): program 8237 DMA controller channel to accept
195 1.1 mycroft * external dma control by a board.
196 1.1 mycroft */
197 1.36 thorpej int
198 1.34 thorpej _isa_dmacascade(ids, chan)
199 1.34 thorpej struct isa_dma_state *ids;
200 1.4 mycroft int chan;
201 1.1 mycroft {
202 1.23 mycroft int ochan = chan & 3;
203 1.4 mycroft
204 1.26 thorpej if (chan < 0 || chan > 7) {
205 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
206 1.36 thorpej return (EINVAL);
207 1.26 thorpej }
208 1.26 thorpej
209 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
210 1.34 thorpej printf("%s: DRQ %d is not free\n", ids->ids_dev->dv_xname,
211 1.34 thorpej chan);
212 1.36 thorpej return (EAGAIN);
213 1.26 thorpej }
214 1.26 thorpej
215 1.34 thorpej ISA_DMA_DRQ_ALLOC(ids, chan);
216 1.1 mycroft
217 1.1 mycroft /* set dma channel mode, and set dma channel mode */
218 1.23 mycroft if ((chan & 4) == 0)
219 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
220 1.26 thorpej DMA1_MODE, ochan | DMA37MD_CASCADE);
221 1.26 thorpej else
222 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
223 1.26 thorpej DMA2_MODE, ochan | DMA37MD_CASCADE);
224 1.26 thorpej
225 1.34 thorpej _isa_dmaunmask(ids, chan);
226 1.36 thorpej return (0);
227 1.26 thorpej }
228 1.26 thorpej
229 1.26 thorpej int
230 1.34 thorpej _isa_dmamap_create(ids, chan, size, flags)
231 1.34 thorpej struct isa_dma_state *ids;
232 1.26 thorpej int chan;
233 1.26 thorpej bus_size_t size;
234 1.26 thorpej int flags;
235 1.26 thorpej {
236 1.26 thorpej bus_size_t maxsize;
237 1.26 thorpej
238 1.26 thorpej if (chan < 0 || chan > 7) {
239 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
240 1.36 thorpej return (EINVAL);
241 1.26 thorpej }
242 1.26 thorpej
243 1.26 thorpej if (chan & 4)
244 1.26 thorpej maxsize = (1 << 17);
245 1.23 mycroft else
246 1.26 thorpej maxsize = (1 << 16);
247 1.26 thorpej
248 1.26 thorpej if (size > maxsize)
249 1.26 thorpej return (EINVAL);
250 1.26 thorpej
251 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
252 1.34 thorpej printf("%s: drq %d is not free\n", ids->ids_dev->dv_xname,
253 1.34 thorpej chan);
254 1.36 thorpej return (EAGAIN);
255 1.26 thorpej }
256 1.15 mycroft
257 1.34 thorpej ISA_DMA_DRQ_ALLOC(ids, chan);
258 1.26 thorpej
259 1.34 thorpej return (bus_dmamap_create(ids->ids_dmat, size, 1, size, maxsize,
260 1.34 thorpej flags, &ids->ids_dmamaps[chan]));
261 1.26 thorpej }
262 1.26 thorpej
263 1.26 thorpej void
264 1.34 thorpej _isa_dmamap_destroy(ids, chan)
265 1.34 thorpej struct isa_dma_state *ids;
266 1.26 thorpej int chan;
267 1.26 thorpej {
268 1.26 thorpej
269 1.26 thorpej if (chan < 0 || chan > 7) {
270 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
271 1.26 thorpej goto lose;
272 1.26 thorpej }
273 1.26 thorpej
274 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan)) {
275 1.26 thorpej printf("%s: drq %d is already free\n",
276 1.34 thorpej ids->ids_dev->dv_xname, chan);
277 1.26 thorpej goto lose;
278 1.26 thorpej }
279 1.26 thorpej
280 1.34 thorpej ISA_DMA_DRQ_FREE(ids, chan);
281 1.26 thorpej
282 1.34 thorpej bus_dmamap_destroy(ids->ids_dmat, ids->ids_dmamaps[chan]);
283 1.26 thorpej return;
284 1.26 thorpej
285 1.26 thorpej lose:
286 1.34 thorpej panic("_isa_dmamap_destroy");
287 1.1 mycroft }
288 1.1 mycroft
289 1.1 mycroft /*
290 1.34 thorpej * _isa_dmastart(): program 8237 DMA controller channel and set it
291 1.26 thorpej * in motion.
292 1.1 mycroft */
293 1.26 thorpej int
294 1.34 thorpej _isa_dmastart(ids, chan, addr, nbytes, p, flags, busdmaflags)
295 1.34 thorpej struct isa_dma_state *ids;
296 1.26 thorpej int chan;
297 1.26 thorpej void *addr;
298 1.26 thorpej bus_size_t nbytes;
299 1.26 thorpej struct proc *p;
300 1.5 mycroft int flags;
301 1.26 thorpej int busdmaflags;
302 1.1 mycroft {
303 1.26 thorpej bus_dmamap_t dmam;
304 1.26 thorpej bus_addr_t dmaaddr;
305 1.1 mycroft int waport;
306 1.23 mycroft int ochan = chan & 3;
307 1.26 thorpej int error;
308 1.26 thorpej
309 1.26 thorpej if (chan < 0 || chan > 7) {
310 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
311 1.26 thorpej goto lose;
312 1.26 thorpej }
313 1.26 thorpej
314 1.26 thorpej #ifdef ISADMA_DEBUG
315 1.34 thorpej printf("_isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
316 1.26 thorpej "flags 0x%x, dmaflags 0x%x\n",
317 1.26 thorpej chan, addr, nbytes, p, flags, busdmaflags);
318 1.26 thorpej #endif
319 1.37 thorpej
320 1.37 thorpej /* Can't specify LOOP and LOOPDEMAND together. */
321 1.37 thorpej if ((flags & (DMAMODE_LOOP|DMAMODE_LOOPDEMAND)) ==
322 1.37 thorpej (DMAMODE_LOOP|DMAMODE_LOOPDEMAND))
323 1.37 thorpej return (EINVAL);
324 1.26 thorpej
325 1.26 thorpej if (chan & 4) {
326 1.26 thorpej if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
327 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
328 1.34 thorpej ids->ids_dev->dv_xname, chan, nbytes, addr);
329 1.26 thorpej goto lose;
330 1.26 thorpej }
331 1.26 thorpej } else {
332 1.26 thorpej if (nbytes > (1 << 16)) {
333 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx\n",
334 1.34 thorpej ids->ids_dev->dv_xname, chan, nbytes);
335 1.26 thorpej goto lose;
336 1.26 thorpej }
337 1.26 thorpej }
338 1.26 thorpej
339 1.34 thorpej dmam = ids->ids_dmamaps[chan];
340 1.31 augustss if (dmam == NULL)
341 1.34 thorpej panic("_isa_dmastart: no DMA map for chan %d\n", chan);
342 1.26 thorpej
343 1.34 thorpej error = bus_dmamap_load(ids->ids_dmat, dmam, addr, nbytes,
344 1.26 thorpej p, busdmaflags);
345 1.26 thorpej if (error)
346 1.26 thorpej return (error);
347 1.1 mycroft
348 1.13 mycroft #ifdef ISADMA_DEBUG
349 1.26 thorpej __asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
350 1.4 mycroft #endif
351 1.1 mycroft
352 1.26 thorpej if (flags & DMAMODE_READ) {
353 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
354 1.33 thorpej BUS_DMASYNC_PREREAD);
355 1.34 thorpej ids->ids_dmareads |= (1 << chan);
356 1.26 thorpej } else {
357 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
358 1.33 thorpej BUS_DMASYNC_PREWRITE);
359 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
360 1.1 mycroft }
361 1.1 mycroft
362 1.26 thorpej dmaaddr = dmam->dm_segs[0].ds_addr;
363 1.26 thorpej
364 1.26 thorpej #ifdef ISADMA_DEBUG
365 1.26 thorpej printf(" dmaaddr 0x%lx\n", dmaaddr);
366 1.26 thorpej
367 1.26 thorpej __asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
368 1.26 thorpej #endif
369 1.24 mycroft
370 1.34 thorpej ids->ids_dmalength[chan] = nbytes;
371 1.1 mycroft
372 1.34 thorpej _isa_dmamask(ids, chan);
373 1.34 thorpej ids->ids_dmafinished &= ~(1 << chan);
374 1.14 mycroft
375 1.1 mycroft if ((chan & 4) == 0) {
376 1.23 mycroft /* set dma channel mode */
377 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, DMA1_MODE,
378 1.26 thorpej ochan | dmamode[flags]);
379 1.1 mycroft
380 1.1 mycroft /* send start address */
381 1.23 mycroft waport = DMA1_CHN(ochan);
382 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
383 1.26 thorpej dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
384 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
385 1.26 thorpej dmaaddr & 0xff);
386 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
387 1.26 thorpej (dmaaddr >> 8) & 0xff);
388 1.1 mycroft
389 1.1 mycroft /* send count */
390 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
391 1.26 thorpej (--nbytes) & 0xff);
392 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
393 1.26 thorpej (nbytes >> 8) & 0xff);
394 1.1 mycroft } else {
395 1.23 mycroft /* set dma channel mode */
396 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, DMA2_MODE,
397 1.26 thorpej ochan | dmamode[flags]);
398 1.1 mycroft
399 1.1 mycroft /* send start address */
400 1.23 mycroft waport = DMA2_CHN(ochan);
401 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
402 1.26 thorpej dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
403 1.26 thorpej dmaaddr >>= 1;
404 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
405 1.26 thorpej dmaaddr & 0xff);
406 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
407 1.26 thorpej (dmaaddr >> 8) & 0xff);
408 1.1 mycroft
409 1.1 mycroft /* send count */
410 1.1 mycroft nbytes >>= 1;
411 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
412 1.26 thorpej (--nbytes) & 0xff);
413 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
414 1.26 thorpej (nbytes >> 8) & 0xff);
415 1.23 mycroft }
416 1.1 mycroft
417 1.34 thorpej _isa_dmaunmask(ids, chan);
418 1.26 thorpej return (0);
419 1.26 thorpej
420 1.26 thorpej lose:
421 1.34 thorpej panic("_isa_dmastart");
422 1.1 mycroft }
423 1.1 mycroft
424 1.1 mycroft void
425 1.34 thorpej _isa_dmaabort(ids, chan)
426 1.34 thorpej struct isa_dma_state *ids;
427 1.4 mycroft int chan;
428 1.1 mycroft {
429 1.1 mycroft
430 1.26 thorpej if (chan < 0 || chan > 7) {
431 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
432 1.34 thorpej panic("_isa_dmaabort");
433 1.26 thorpej }
434 1.1 mycroft
435 1.34 thorpej _isa_dmamask(ids, chan);
436 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, ids->ids_dmamaps[chan]);
437 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
438 1.22 mycroft }
439 1.22 mycroft
440 1.26 thorpej bus_size_t
441 1.34 thorpej _isa_dmacount(ids, chan)
442 1.34 thorpej struct isa_dma_state *ids;
443 1.22 mycroft int chan;
444 1.22 mycroft {
445 1.22 mycroft int waport;
446 1.26 thorpej bus_size_t nbytes;
447 1.23 mycroft int ochan = chan & 3;
448 1.22 mycroft
449 1.26 thorpej if (chan < 0 || chan > 7) {
450 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
451 1.26 thorpej panic("isa_dmacount");
452 1.26 thorpej }
453 1.22 mycroft
454 1.34 thorpej _isa_dmamask(ids, chan);
455 1.23 mycroft
456 1.24 mycroft /*
457 1.24 mycroft * We have to shift the byte count by 1. If we're in auto-initialize
458 1.24 mycroft * mode, the count may have wrapped around to the initial value. We
459 1.24 mycroft * can't use the TC bit to check for this case, so instead we compare
460 1.24 mycroft * against the original byte count.
461 1.24 mycroft * If we're not in auto-initialize mode, then the count will wrap to
462 1.24 mycroft * -1, so we also handle that case.
463 1.24 mycroft */
464 1.24 mycroft if ((chan & 4) == 0) {
465 1.24 mycroft waport = DMA1_CHN(ochan);
466 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
467 1.26 thorpej waport + 1) + 1;
468 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
469 1.26 thorpej waport + 1) << 8;
470 1.24 mycroft nbytes &= 0xffff;
471 1.24 mycroft } else {
472 1.24 mycroft waport = DMA2_CHN(ochan);
473 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
474 1.26 thorpej waport + 2) + 1;
475 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
476 1.26 thorpej waport + 2) << 8;
477 1.24 mycroft nbytes <<= 1;
478 1.24 mycroft nbytes &= 0x1ffff;
479 1.24 mycroft }
480 1.24 mycroft
481 1.34 thorpej if (nbytes == ids->ids_dmalength[chan])
482 1.23 mycroft nbytes = 0;
483 1.22 mycroft
484 1.34 thorpej _isa_dmaunmask(ids, chan);
485 1.22 mycroft return (nbytes);
486 1.1 mycroft }
487 1.1 mycroft
488 1.13 mycroft int
489 1.34 thorpej _isa_dmafinished(ids, chan)
490 1.34 thorpej struct isa_dma_state *ids;
491 1.5 mycroft int chan;
492 1.1 mycroft {
493 1.1 mycroft
494 1.26 thorpej if (chan < 0 || chan > 7) {
495 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
496 1.34 thorpej panic("_isa_dmafinished");
497 1.26 thorpej }
498 1.1 mycroft
499 1.1 mycroft /* check that the terminal count was reached */
500 1.1 mycroft if ((chan & 4) == 0)
501 1.34 thorpej ids->ids_dmafinished |= bus_space_read_1(ids->ids_bst,
502 1.34 thorpej ids->ids_dma1h, DMA1_SR) & 0x0f;
503 1.1 mycroft else
504 1.34 thorpej ids->ids_dmafinished |= (bus_space_read_1(ids->ids_bst,
505 1.34 thorpej ids->ids_dma2h, DMA2_SR) & 0x0f) << 4;
506 1.14 mycroft
507 1.34 thorpej return ((ids->ids_dmafinished & (1 << chan)) != 0);
508 1.13 mycroft }
509 1.13 mycroft
510 1.13 mycroft void
511 1.34 thorpej _isa_dmadone(ids, chan)
512 1.34 thorpej struct isa_dma_state *ids;
513 1.13 mycroft int chan;
514 1.13 mycroft {
515 1.26 thorpej bus_dmamap_t dmam;
516 1.13 mycroft
517 1.26 thorpej if (chan < 0 || chan > 7) {
518 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
519 1.34 thorpej panic("_isa_dmadone");
520 1.26 thorpej }
521 1.26 thorpej
522 1.34 thorpej dmam = ids->ids_dmamaps[chan];
523 1.26 thorpej
524 1.34 thorpej _isa_dmamask(ids, chan);
525 1.14 mycroft
526 1.34 thorpej if (_isa_dmafinished(ids, chan) == 0)
527 1.34 thorpej printf("%s: _isa_dmadone: channel %d not finished\n",
528 1.34 thorpej ids->ids_dev->dv_xname, chan);
529 1.23 mycroft
530 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
531 1.34 thorpej (ids->ids_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
532 1.26 thorpej BUS_DMASYNC_POSTWRITE);
533 1.9 mycroft
534 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, dmam);
535 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
536 1.35 thorpej }
537 1.35 thorpej
538 1.35 thorpej void
539 1.35 thorpej _isa_dmafreeze(ids)
540 1.35 thorpej struct isa_dma_state *ids;
541 1.35 thorpej {
542 1.35 thorpej int s;
543 1.35 thorpej
544 1.35 thorpej s = splhigh();
545 1.35 thorpej
546 1.35 thorpej if (ids->ids_frozen == 0) {
547 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
548 1.35 thorpej DMA1_MASK, 0x0f);
549 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
550 1.35 thorpej DMA2_MASK, 0x0f);
551 1.35 thorpej }
552 1.35 thorpej
553 1.35 thorpej ids->ids_frozen++;
554 1.35 thorpej if (ids->ids_frozen < 1)
555 1.35 thorpej panic("_isa_dmafreeze: overflow");
556 1.35 thorpej
557 1.35 thorpej splx(s);
558 1.35 thorpej }
559 1.35 thorpej
560 1.35 thorpej void
561 1.35 thorpej _isa_dmathaw(ids)
562 1.35 thorpej struct isa_dma_state *ids;
563 1.35 thorpej {
564 1.35 thorpej int s;
565 1.35 thorpej
566 1.35 thorpej s = splhigh();
567 1.35 thorpej
568 1.35 thorpej ids->ids_frozen--;
569 1.35 thorpej if (ids->ids_frozen < 0)
570 1.35 thorpej panic("_isa_dmathaw: underflow");
571 1.35 thorpej
572 1.35 thorpej if (ids->ids_frozen == 0) {
573 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
574 1.35 thorpej DMA1_MASK, ids->ids_masked & 0x0f);
575 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
576 1.35 thorpej DMA2_MASK, (ids->ids_masked >> 4) & 0x0f);
577 1.35 thorpej }
578 1.35 thorpej
579 1.35 thorpej splx(s);
580 1.1 mycroft }
581 1.1 mycroft
582 1.1 mycroft int
583 1.34 thorpej _isa_dmamem_alloc(ids, chan, size, addrp, flags)
584 1.34 thorpej struct isa_dma_state *ids;
585 1.26 thorpej int chan;
586 1.26 thorpej bus_size_t size;
587 1.26 thorpej bus_addr_t *addrp;
588 1.26 thorpej int flags;
589 1.26 thorpej {
590 1.26 thorpej bus_dma_segment_t seg;
591 1.26 thorpej int error, boundary, rsegs;
592 1.26 thorpej
593 1.26 thorpej if (chan < 0 || chan > 7) {
594 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
595 1.34 thorpej panic("_isa_dmamem_alloc");
596 1.1 mycroft }
597 1.26 thorpej
598 1.26 thorpej boundary = (chan & 4) ? (1 << 17) : (1 << 16);
599 1.26 thorpej
600 1.26 thorpej size = round_page(size);
601 1.26 thorpej
602 1.34 thorpej error = bus_dmamem_alloc(ids->ids_dmat, size, NBPG, boundary,
603 1.26 thorpej &seg, 1, &rsegs, flags);
604 1.26 thorpej if (error)
605 1.26 thorpej return (error);
606 1.26 thorpej
607 1.26 thorpej *addrp = seg.ds_addr;
608 1.26 thorpej return (0);
609 1.5 mycroft }
610 1.5 mycroft
611 1.26 thorpej void
612 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size)
613 1.34 thorpej struct isa_dma_state *ids;
614 1.26 thorpej int chan;
615 1.26 thorpej bus_addr_t addr;
616 1.26 thorpej bus_size_t size;
617 1.26 thorpej {
618 1.26 thorpej bus_dma_segment_t seg;
619 1.26 thorpej
620 1.26 thorpej if (chan < 0 || chan > 7) {
621 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
622 1.34 thorpej panic("_isa_dmamem_free");
623 1.26 thorpej }
624 1.26 thorpej
625 1.26 thorpej seg.ds_addr = addr;
626 1.26 thorpej seg.ds_len = size;
627 1.5 mycroft
628 1.34 thorpej bus_dmamem_free(ids->ids_dmat, &seg, 1);
629 1.26 thorpej }
630 1.5 mycroft
631 1.26 thorpej int
632 1.34 thorpej _isa_dmamem_map(ids, chan, addr, size, kvap, flags)
633 1.34 thorpej struct isa_dma_state *ids;
634 1.26 thorpej int chan;
635 1.26 thorpej bus_addr_t addr;
636 1.26 thorpej bus_size_t size;
637 1.26 thorpej caddr_t *kvap;
638 1.26 thorpej int flags;
639 1.26 thorpej {
640 1.26 thorpej bus_dma_segment_t seg;
641 1.26 thorpej
642 1.26 thorpej if (chan < 0 || chan > 7) {
643 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
644 1.34 thorpej panic("_isa_dmamem_map");
645 1.5 mycroft }
646 1.5 mycroft
647 1.26 thorpej seg.ds_addr = addr;
648 1.26 thorpej seg.ds_len = size;
649 1.26 thorpej
650 1.34 thorpej return (bus_dmamem_map(ids->ids_dmat, &seg, 1, size, kvap, flags));
651 1.5 mycroft }
652 1.5 mycroft
653 1.5 mycroft void
654 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size)
655 1.34 thorpej struct isa_dma_state *ids;
656 1.26 thorpej int chan;
657 1.26 thorpej caddr_t kva;
658 1.26 thorpej size_t size;
659 1.19 christos {
660 1.5 mycroft
661 1.26 thorpej if (chan < 0 || chan > 7) {
662 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
663 1.34 thorpej panic("_isa_dmamem_unmap");
664 1.5 mycroft }
665 1.26 thorpej
666 1.34 thorpej bus_dmamem_unmap(ids->ids_dmat, kva, size);
667 1.26 thorpej }
668 1.26 thorpej
669 1.26 thorpej int
670 1.34 thorpej _isa_dmamem_mmap(ids, chan, addr, size, off, prot, flags)
671 1.34 thorpej struct isa_dma_state *ids;
672 1.26 thorpej int chan;
673 1.26 thorpej bus_addr_t addr;
674 1.26 thorpej bus_size_t size;
675 1.26 thorpej int off, prot, flags;
676 1.26 thorpej {
677 1.26 thorpej bus_dma_segment_t seg;
678 1.26 thorpej
679 1.26 thorpej if (chan < 0 || chan > 7) {
680 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
681 1.34 thorpej panic("_isa_dmamem_mmap");
682 1.26 thorpej }
683 1.26 thorpej
684 1.26 thorpej seg.ds_addr = addr;
685 1.26 thorpej seg.ds_len = size;
686 1.26 thorpej
687 1.34 thorpej return (bus_dmamem_mmap(ids->ids_dmat, &seg, 1, off, prot, flags));
688 1.30 augustss }
689 1.30 augustss
690 1.30 augustss int
691 1.34 thorpej _isa_drq_isfree(ids, chan)
692 1.34 thorpej struct isa_dma_state *ids;
693 1.30 augustss int chan;
694 1.30 augustss {
695 1.34 thorpej
696 1.30 augustss if (chan < 0 || chan > 7) {
697 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
698 1.34 thorpej panic("_isa_drq_isfree");
699 1.30 augustss }
700 1.34 thorpej
701 1.34 thorpej return ISA_DMA_DRQ_ISFREE(ids, chan);
702 1.27 augustss }
703 1.27 augustss
704 1.27 augustss void *
705 1.34 thorpej _isa_malloc(ids, chan, size, pool, flags)
706 1.34 thorpej struct isa_dma_state *ids;
707 1.27 augustss int chan;
708 1.27 augustss size_t size;
709 1.27 augustss int pool;
710 1.27 augustss int flags;
711 1.27 augustss {
712 1.27 augustss bus_addr_t addr;
713 1.27 augustss caddr_t kva;
714 1.27 augustss int bflags;
715 1.27 augustss struct isa_mem *m;
716 1.27 augustss
717 1.27 augustss bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
718 1.27 augustss
719 1.34 thorpej if (_isa_dmamem_alloc(ids, chan, size, &addr, bflags))
720 1.27 augustss return 0;
721 1.34 thorpej if (_isa_dmamem_map(ids, chan, addr, size, &kva, bflags)) {
722 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
723 1.27 augustss return 0;
724 1.27 augustss }
725 1.27 augustss m = malloc(sizeof(*m), pool, flags);
726 1.27 augustss if (m == 0) {
727 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size);
728 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
729 1.27 augustss return 0;
730 1.27 augustss }
731 1.34 thorpej m->ids = ids;
732 1.27 augustss m->chan = chan;
733 1.27 augustss m->size = size;
734 1.27 augustss m->addr = addr;
735 1.27 augustss m->kva = kva;
736 1.27 augustss m->next = isa_mem_head;
737 1.27 augustss isa_mem_head = m;
738 1.27 augustss return (void *)kva;
739 1.27 augustss }
740 1.27 augustss
741 1.27 augustss void
742 1.34 thorpej _isa_free(addr, pool)
743 1.27 augustss void *addr;
744 1.27 augustss int pool;
745 1.27 augustss {
746 1.27 augustss struct isa_mem **mp, *m;
747 1.27 augustss caddr_t kva = (caddr_t)addr;
748 1.27 augustss
749 1.34 thorpej for(mp = &isa_mem_head; *mp && (*mp)->kva != kva;
750 1.34 thorpej mp = &(*mp)->next)
751 1.27 augustss ;
752 1.27 augustss m = *mp;
753 1.27 augustss if (!m) {
754 1.34 thorpej printf("_isa_free: freeing unallocted memory\n");
755 1.27 augustss return;
756 1.27 augustss }
757 1.27 augustss *mp = m->next;
758 1.34 thorpej _isa_dmamem_unmap(m->ids, m->chan, kva, m->size);
759 1.34 thorpej _isa_dmamem_free(m->ids, m->chan, m->addr, m->size);
760 1.27 augustss free(m, pool);
761 1.28 augustss }
762 1.28 augustss
763 1.28 augustss int
764 1.34 thorpej _isa_mappage(mem, off, prot)
765 1.28 augustss void *mem;
766 1.28 augustss int off;
767 1.28 augustss int prot;
768 1.28 augustss {
769 1.28 augustss struct isa_mem *m;
770 1.28 augustss
771 1.28 augustss for(m = isa_mem_head; m && m->kva != (caddr_t)mem; m = m->next)
772 1.28 augustss ;
773 1.28 augustss if (!m) {
774 1.34 thorpej printf("_isa_mappage: mapping unallocted memory\n");
775 1.28 augustss return -1;
776 1.28 augustss }
777 1.34 thorpej return _isa_dmamem_mmap(m->ids, m->chan, m->addr,
778 1.34 thorpej m->size, off, prot, BUS_DMA_WAITOK);
779 1.1 mycroft }
780