isadma.c revision 1.34 1 1.34 thorpej /* $NetBSD: isadma.c,v 1.34 1998/06/09 00:00:21 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.15 mycroft static u_int8_t dmamode[4] = {
71 1.17 mycroft DMA37MD_READ | DMA37MD_SINGLE,
72 1.15 mycroft DMA37MD_WRITE | DMA37MD_SINGLE,
73 1.25 mycroft DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
74 1.25 mycroft DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP
75 1.15 mycroft };
76 1.1 mycroft
77 1.34 thorpej static inline void _isa_dmaunmask __P((struct isa_dma_state *, int));
78 1.34 thorpej static inline void _isa_dmamask __P((struct isa_dma_state *, int));
79 1.19 christos
80 1.26 thorpej static inline void
81 1.34 thorpej _isa_dmaunmask(ids, chan)
82 1.34 thorpej struct isa_dma_state *ids;
83 1.23 mycroft int chan;
84 1.23 mycroft {
85 1.23 mycroft int ochan = chan & 3;
86 1.23 mycroft
87 1.23 mycroft /* set dma channel mode, and set dma channel mode */
88 1.23 mycroft if ((chan & 4) == 0)
89 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
90 1.26 thorpej DMA1_SMSK, ochan | DMA37SM_CLEAR);
91 1.23 mycroft else
92 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
93 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_CLEAR);
94 1.23 mycroft }
95 1.23 mycroft
96 1.26 thorpej static inline void
97 1.34 thorpej _isa_dmamask(ids, chan)
98 1.34 thorpej struct isa_dma_state *ids;
99 1.23 mycroft int chan;
100 1.23 mycroft {
101 1.23 mycroft int ochan = chan & 3;
102 1.23 mycroft
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_SET);
107 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
108 1.26 thorpej DMA1_FFC, 0);
109 1.23 mycroft } else {
110 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
111 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_SET);
112 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
113 1.26 thorpej DMA2_FFC, 0);
114 1.23 mycroft }
115 1.23 mycroft }
116 1.23 mycroft
117 1.1 mycroft /*
118 1.34 thorpej * _isa_dmainit(): Initialize the isa_dma_state for this chipset.
119 1.34 thorpej */
120 1.34 thorpej void
121 1.34 thorpej _isa_dmainit(ids, bst, dmat, dev)
122 1.34 thorpej struct isa_dma_state *ids;
123 1.34 thorpej bus_space_tag_t bst;
124 1.34 thorpej bus_dma_tag_t dmat;
125 1.34 thorpej struct device *dev;
126 1.34 thorpej {
127 1.34 thorpej
128 1.34 thorpej ids->ids_dev = dev;
129 1.34 thorpej
130 1.34 thorpej if (ids->ids_initialized) {
131 1.34 thorpej /*
132 1.34 thorpej * Some systems may have e.g. `ofisa' (OpenFirmware
133 1.34 thorpej * configuration of ISA bus) and a regular `isa'.
134 1.34 thorpej * We allow both to call the initialization function,
135 1.34 thorpej * and take the device name from the last caller
136 1.34 thorpej * (assuming it will be the indirect ISA bus). Since
137 1.34 thorpej * `ofisa' and `isa' are the same bus with different
138 1.34 thorpej * configuration mechanisms, the space and dma tags
139 1.34 thorpej * must be the same!
140 1.34 thorpej */
141 1.34 thorpej if (ids->ids_bst != bst || ids->ids_dmat != dmat)
142 1.34 thorpej panic("_isa_dmainit: inconsistent ISA tags");
143 1.34 thorpej } else {
144 1.34 thorpej ids->ids_bst = bst;
145 1.34 thorpej ids->ids_dmat = dmat;
146 1.34 thorpej
147 1.34 thorpej /*
148 1.34 thorpej * Map the registers used by the ISA DMA controller.
149 1.34 thorpej */
150 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA1, DMA1_IOSIZE, 0,
151 1.34 thorpej &ids->ids_dma1h))
152 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #1");
153 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA2, DMA2_IOSIZE, 0,
154 1.34 thorpej &ids->ids_dma2h))
155 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #2");
156 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMAPG, 0xf, 0,
157 1.34 thorpej &ids->ids_dmapgh))
158 1.34 thorpej panic("_isa_dmainit: unable to map DMA page registers");
159 1.34 thorpej
160 1.34 thorpej ids->ids_initialized = 1;
161 1.34 thorpej }
162 1.34 thorpej }
163 1.34 thorpej
164 1.34 thorpej /*
165 1.34 thorpej * _isa_dmacascade(): program 8237 DMA controller channel to accept
166 1.1 mycroft * external dma control by a board.
167 1.1 mycroft */
168 1.1 mycroft void
169 1.34 thorpej _isa_dmacascade(ids, chan)
170 1.34 thorpej struct isa_dma_state *ids;
171 1.4 mycroft int chan;
172 1.1 mycroft {
173 1.23 mycroft int ochan = chan & 3;
174 1.4 mycroft
175 1.26 thorpej if (chan < 0 || chan > 7) {
176 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
177 1.26 thorpej goto lose;
178 1.26 thorpej }
179 1.26 thorpej
180 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
181 1.34 thorpej printf("%s: DRQ %d is not free\n", ids->ids_dev->dv_xname,
182 1.34 thorpej chan);
183 1.26 thorpej goto lose;
184 1.26 thorpej }
185 1.26 thorpej
186 1.34 thorpej ISA_DMA_DRQ_ALLOC(ids, chan);
187 1.1 mycroft
188 1.1 mycroft /* set dma channel mode, and set dma channel mode */
189 1.23 mycroft if ((chan & 4) == 0)
190 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
191 1.26 thorpej DMA1_MODE, ochan | DMA37MD_CASCADE);
192 1.26 thorpej else
193 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
194 1.26 thorpej DMA2_MODE, ochan | DMA37MD_CASCADE);
195 1.26 thorpej
196 1.34 thorpej _isa_dmaunmask(ids, chan);
197 1.26 thorpej return;
198 1.26 thorpej
199 1.26 thorpej lose:
200 1.34 thorpej panic("_isa_dmacascade");
201 1.26 thorpej }
202 1.26 thorpej
203 1.26 thorpej int
204 1.34 thorpej _isa_dmamap_create(ids, chan, size, flags)
205 1.34 thorpej struct isa_dma_state *ids;
206 1.26 thorpej int chan;
207 1.26 thorpej bus_size_t size;
208 1.26 thorpej int flags;
209 1.26 thorpej {
210 1.26 thorpej bus_size_t maxsize;
211 1.26 thorpej
212 1.26 thorpej if (chan < 0 || chan > 7) {
213 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
214 1.26 thorpej goto lose;
215 1.26 thorpej }
216 1.26 thorpej
217 1.26 thorpej if (chan & 4)
218 1.26 thorpej maxsize = (1 << 17);
219 1.23 mycroft else
220 1.26 thorpej maxsize = (1 << 16);
221 1.26 thorpej
222 1.26 thorpej if (size > maxsize)
223 1.26 thorpej return (EINVAL);
224 1.26 thorpej
225 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
226 1.34 thorpej printf("%s: drq %d is not free\n", ids->ids_dev->dv_xname,
227 1.34 thorpej chan);
228 1.30 augustss goto lose;
229 1.26 thorpej }
230 1.15 mycroft
231 1.34 thorpej ISA_DMA_DRQ_ALLOC(ids, chan);
232 1.26 thorpej
233 1.34 thorpej return (bus_dmamap_create(ids->ids_dmat, size, 1, size, maxsize,
234 1.34 thorpej flags, &ids->ids_dmamaps[chan]));
235 1.26 thorpej
236 1.26 thorpej lose:
237 1.34 thorpej panic("_isa_dmamap_create");
238 1.26 thorpej }
239 1.26 thorpej
240 1.26 thorpej void
241 1.34 thorpej _isa_dmamap_destroy(ids, chan)
242 1.34 thorpej struct isa_dma_state *ids;
243 1.26 thorpej int chan;
244 1.26 thorpej {
245 1.26 thorpej
246 1.26 thorpej if (chan < 0 || chan > 7) {
247 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
248 1.26 thorpej goto lose;
249 1.26 thorpej }
250 1.26 thorpej
251 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan)) {
252 1.26 thorpej printf("%s: drq %d is already free\n",
253 1.34 thorpej ids->ids_dev->dv_xname, chan);
254 1.26 thorpej goto lose;
255 1.26 thorpej }
256 1.26 thorpej
257 1.34 thorpej ISA_DMA_DRQ_FREE(ids, chan);
258 1.26 thorpej
259 1.34 thorpej bus_dmamap_destroy(ids->ids_dmat, ids->ids_dmamaps[chan]);
260 1.26 thorpej return;
261 1.26 thorpej
262 1.26 thorpej lose:
263 1.34 thorpej panic("_isa_dmamap_destroy");
264 1.1 mycroft }
265 1.1 mycroft
266 1.1 mycroft /*
267 1.34 thorpej * _isa_dmastart(): program 8237 DMA controller channel and set it
268 1.26 thorpej * in motion.
269 1.1 mycroft */
270 1.26 thorpej int
271 1.34 thorpej _isa_dmastart(ids, chan, addr, nbytes, p, flags, busdmaflags)
272 1.34 thorpej struct isa_dma_state *ids;
273 1.26 thorpej int chan;
274 1.26 thorpej void *addr;
275 1.26 thorpej bus_size_t nbytes;
276 1.26 thorpej struct proc *p;
277 1.5 mycroft int flags;
278 1.26 thorpej int busdmaflags;
279 1.1 mycroft {
280 1.26 thorpej bus_dmamap_t dmam;
281 1.26 thorpej bus_addr_t dmaaddr;
282 1.1 mycroft int waport;
283 1.23 mycroft int ochan = chan & 3;
284 1.26 thorpej int error;
285 1.26 thorpej
286 1.26 thorpej if (chan < 0 || chan > 7) {
287 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
288 1.26 thorpej goto lose;
289 1.26 thorpej }
290 1.26 thorpej
291 1.26 thorpej #ifdef ISADMA_DEBUG
292 1.34 thorpej printf("_isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
293 1.26 thorpej "flags 0x%x, dmaflags 0x%x\n",
294 1.26 thorpej chan, addr, nbytes, p, flags, busdmaflags);
295 1.26 thorpej #endif
296 1.26 thorpej
297 1.26 thorpej if (chan & 4) {
298 1.26 thorpej if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
299 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
300 1.34 thorpej ids->ids_dev->dv_xname, chan, nbytes, addr);
301 1.26 thorpej goto lose;
302 1.26 thorpej }
303 1.26 thorpej } else {
304 1.26 thorpej if (nbytes > (1 << 16)) {
305 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx\n",
306 1.34 thorpej ids->ids_dev->dv_xname, chan, nbytes);
307 1.26 thorpej goto lose;
308 1.26 thorpej }
309 1.26 thorpej }
310 1.26 thorpej
311 1.34 thorpej dmam = ids->ids_dmamaps[chan];
312 1.31 augustss if (dmam == NULL)
313 1.34 thorpej panic("_isa_dmastart: no DMA map for chan %d\n", chan);
314 1.26 thorpej
315 1.34 thorpej error = bus_dmamap_load(ids->ids_dmat, dmam, addr, nbytes,
316 1.26 thorpej p, busdmaflags);
317 1.26 thorpej if (error)
318 1.26 thorpej return (error);
319 1.1 mycroft
320 1.13 mycroft #ifdef ISADMA_DEBUG
321 1.26 thorpej __asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
322 1.4 mycroft #endif
323 1.1 mycroft
324 1.26 thorpej if (flags & DMAMODE_READ) {
325 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
326 1.33 thorpej BUS_DMASYNC_PREREAD);
327 1.34 thorpej ids->ids_dmareads |= (1 << chan);
328 1.26 thorpej } else {
329 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
330 1.33 thorpej BUS_DMASYNC_PREWRITE);
331 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
332 1.1 mycroft }
333 1.1 mycroft
334 1.26 thorpej dmaaddr = dmam->dm_segs[0].ds_addr;
335 1.26 thorpej
336 1.26 thorpej #ifdef ISADMA_DEBUG
337 1.26 thorpej printf(" dmaaddr 0x%lx\n", dmaaddr);
338 1.26 thorpej
339 1.26 thorpej __asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
340 1.26 thorpej #endif
341 1.24 mycroft
342 1.34 thorpej ids->ids_dmalength[chan] = nbytes;
343 1.1 mycroft
344 1.34 thorpej _isa_dmamask(ids, chan);
345 1.34 thorpej ids->ids_dmafinished &= ~(1 << chan);
346 1.14 mycroft
347 1.1 mycroft if ((chan & 4) == 0) {
348 1.23 mycroft /* set dma channel mode */
349 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, DMA1_MODE,
350 1.26 thorpej ochan | dmamode[flags]);
351 1.1 mycroft
352 1.1 mycroft /* send start address */
353 1.23 mycroft waport = DMA1_CHN(ochan);
354 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
355 1.26 thorpej dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
356 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
357 1.26 thorpej dmaaddr & 0xff);
358 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
359 1.26 thorpej (dmaaddr >> 8) & 0xff);
360 1.1 mycroft
361 1.1 mycroft /* send count */
362 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
363 1.26 thorpej (--nbytes) & 0xff);
364 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
365 1.26 thorpej (nbytes >> 8) & 0xff);
366 1.1 mycroft } else {
367 1.23 mycroft /* set dma channel mode */
368 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, DMA2_MODE,
369 1.26 thorpej ochan | dmamode[flags]);
370 1.1 mycroft
371 1.1 mycroft /* send start address */
372 1.23 mycroft waport = DMA2_CHN(ochan);
373 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
374 1.26 thorpej dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
375 1.26 thorpej dmaaddr >>= 1;
376 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
377 1.26 thorpej dmaaddr & 0xff);
378 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
379 1.26 thorpej (dmaaddr >> 8) & 0xff);
380 1.1 mycroft
381 1.1 mycroft /* send count */
382 1.1 mycroft nbytes >>= 1;
383 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
384 1.26 thorpej (--nbytes) & 0xff);
385 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
386 1.26 thorpej (nbytes >> 8) & 0xff);
387 1.23 mycroft }
388 1.1 mycroft
389 1.34 thorpej _isa_dmaunmask(ids, chan);
390 1.26 thorpej return (0);
391 1.26 thorpej
392 1.26 thorpej lose:
393 1.34 thorpej panic("_isa_dmastart");
394 1.1 mycroft }
395 1.1 mycroft
396 1.1 mycroft void
397 1.34 thorpej _isa_dmaabort(ids, chan)
398 1.34 thorpej struct isa_dma_state *ids;
399 1.4 mycroft int chan;
400 1.1 mycroft {
401 1.1 mycroft
402 1.26 thorpej if (chan < 0 || chan > 7) {
403 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
404 1.34 thorpej panic("_isa_dmaabort");
405 1.26 thorpej }
406 1.1 mycroft
407 1.34 thorpej _isa_dmamask(ids, chan);
408 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, ids->ids_dmamaps[chan]);
409 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
410 1.22 mycroft }
411 1.22 mycroft
412 1.26 thorpej bus_size_t
413 1.34 thorpej _isa_dmacount(ids, chan)
414 1.34 thorpej struct isa_dma_state *ids;
415 1.22 mycroft int chan;
416 1.22 mycroft {
417 1.22 mycroft int waport;
418 1.26 thorpej bus_size_t nbytes;
419 1.23 mycroft int ochan = chan & 3;
420 1.22 mycroft
421 1.26 thorpej if (chan < 0 || chan > 7) {
422 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
423 1.26 thorpej panic("isa_dmacount");
424 1.26 thorpej }
425 1.22 mycroft
426 1.34 thorpej _isa_dmamask(ids, chan);
427 1.23 mycroft
428 1.24 mycroft /*
429 1.24 mycroft * We have to shift the byte count by 1. If we're in auto-initialize
430 1.24 mycroft * mode, the count may have wrapped around to the initial value. We
431 1.24 mycroft * can't use the TC bit to check for this case, so instead we compare
432 1.24 mycroft * against the original byte count.
433 1.24 mycroft * If we're not in auto-initialize mode, then the count will wrap to
434 1.24 mycroft * -1, so we also handle that case.
435 1.24 mycroft */
436 1.24 mycroft if ((chan & 4) == 0) {
437 1.24 mycroft waport = DMA1_CHN(ochan);
438 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
439 1.26 thorpej waport + 1) + 1;
440 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
441 1.26 thorpej waport + 1) << 8;
442 1.24 mycroft nbytes &= 0xffff;
443 1.24 mycroft } else {
444 1.24 mycroft waport = DMA2_CHN(ochan);
445 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
446 1.26 thorpej waport + 2) + 1;
447 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
448 1.26 thorpej waport + 2) << 8;
449 1.24 mycroft nbytes <<= 1;
450 1.24 mycroft nbytes &= 0x1ffff;
451 1.24 mycroft }
452 1.24 mycroft
453 1.34 thorpej if (nbytes == ids->ids_dmalength[chan])
454 1.23 mycroft nbytes = 0;
455 1.22 mycroft
456 1.34 thorpej _isa_dmaunmask(ids, chan);
457 1.22 mycroft return (nbytes);
458 1.1 mycroft }
459 1.1 mycroft
460 1.13 mycroft int
461 1.34 thorpej _isa_dmafinished(ids, chan)
462 1.34 thorpej struct isa_dma_state *ids;
463 1.5 mycroft int chan;
464 1.1 mycroft {
465 1.1 mycroft
466 1.26 thorpej if (chan < 0 || chan > 7) {
467 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
468 1.34 thorpej panic("_isa_dmafinished");
469 1.26 thorpej }
470 1.1 mycroft
471 1.1 mycroft /* check that the terminal count was reached */
472 1.1 mycroft if ((chan & 4) == 0)
473 1.34 thorpej ids->ids_dmafinished |= bus_space_read_1(ids->ids_bst,
474 1.34 thorpej ids->ids_dma1h, DMA1_SR) & 0x0f;
475 1.1 mycroft else
476 1.34 thorpej ids->ids_dmafinished |= (bus_space_read_1(ids->ids_bst,
477 1.34 thorpej ids->ids_dma2h, DMA2_SR) & 0x0f) << 4;
478 1.14 mycroft
479 1.34 thorpej return ((ids->ids_dmafinished & (1 << chan)) != 0);
480 1.13 mycroft }
481 1.13 mycroft
482 1.13 mycroft void
483 1.34 thorpej _isa_dmadone(ids, chan)
484 1.34 thorpej struct isa_dma_state *ids;
485 1.13 mycroft int chan;
486 1.13 mycroft {
487 1.26 thorpej bus_dmamap_t dmam;
488 1.13 mycroft
489 1.26 thorpej if (chan < 0 || chan > 7) {
490 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
491 1.34 thorpej panic("_isa_dmadone");
492 1.26 thorpej }
493 1.26 thorpej
494 1.34 thorpej dmam = ids->ids_dmamaps[chan];
495 1.26 thorpej
496 1.34 thorpej _isa_dmamask(ids, chan);
497 1.14 mycroft
498 1.34 thorpej if (_isa_dmafinished(ids, chan) == 0)
499 1.34 thorpej printf("%s: _isa_dmadone: channel %d not finished\n",
500 1.34 thorpej ids->ids_dev->dv_xname, chan);
501 1.23 mycroft
502 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
503 1.34 thorpej (ids->ids_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
504 1.26 thorpej BUS_DMASYNC_POSTWRITE);
505 1.9 mycroft
506 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, dmam);
507 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
508 1.1 mycroft }
509 1.1 mycroft
510 1.1 mycroft int
511 1.34 thorpej _isa_dmamem_alloc(ids, chan, size, addrp, flags)
512 1.34 thorpej struct isa_dma_state *ids;
513 1.26 thorpej int chan;
514 1.26 thorpej bus_size_t size;
515 1.26 thorpej bus_addr_t *addrp;
516 1.26 thorpej int flags;
517 1.26 thorpej {
518 1.26 thorpej bus_dma_segment_t seg;
519 1.26 thorpej int error, boundary, rsegs;
520 1.26 thorpej
521 1.26 thorpej if (chan < 0 || chan > 7) {
522 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
523 1.34 thorpej panic("_isa_dmamem_alloc");
524 1.1 mycroft }
525 1.26 thorpej
526 1.26 thorpej boundary = (chan & 4) ? (1 << 17) : (1 << 16);
527 1.26 thorpej
528 1.26 thorpej size = round_page(size);
529 1.26 thorpej
530 1.34 thorpej error = bus_dmamem_alloc(ids->ids_dmat, size, NBPG, boundary,
531 1.26 thorpej &seg, 1, &rsegs, flags);
532 1.26 thorpej if (error)
533 1.26 thorpej return (error);
534 1.26 thorpej
535 1.26 thorpej *addrp = seg.ds_addr;
536 1.26 thorpej return (0);
537 1.5 mycroft }
538 1.5 mycroft
539 1.26 thorpej void
540 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size)
541 1.34 thorpej struct isa_dma_state *ids;
542 1.26 thorpej int chan;
543 1.26 thorpej bus_addr_t addr;
544 1.26 thorpej bus_size_t size;
545 1.26 thorpej {
546 1.26 thorpej bus_dma_segment_t seg;
547 1.26 thorpej
548 1.26 thorpej if (chan < 0 || chan > 7) {
549 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
550 1.34 thorpej panic("_isa_dmamem_free");
551 1.26 thorpej }
552 1.26 thorpej
553 1.26 thorpej seg.ds_addr = addr;
554 1.26 thorpej seg.ds_len = size;
555 1.5 mycroft
556 1.34 thorpej bus_dmamem_free(ids->ids_dmat, &seg, 1);
557 1.26 thorpej }
558 1.5 mycroft
559 1.26 thorpej int
560 1.34 thorpej _isa_dmamem_map(ids, chan, addr, size, kvap, flags)
561 1.34 thorpej struct isa_dma_state *ids;
562 1.26 thorpej int chan;
563 1.26 thorpej bus_addr_t addr;
564 1.26 thorpej bus_size_t size;
565 1.26 thorpej caddr_t *kvap;
566 1.26 thorpej int flags;
567 1.26 thorpej {
568 1.26 thorpej bus_dma_segment_t seg;
569 1.26 thorpej
570 1.26 thorpej if (chan < 0 || chan > 7) {
571 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
572 1.34 thorpej panic("_isa_dmamem_map");
573 1.5 mycroft }
574 1.5 mycroft
575 1.26 thorpej seg.ds_addr = addr;
576 1.26 thorpej seg.ds_len = size;
577 1.26 thorpej
578 1.34 thorpej return (bus_dmamem_map(ids->ids_dmat, &seg, 1, size, kvap, flags));
579 1.5 mycroft }
580 1.5 mycroft
581 1.5 mycroft void
582 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size)
583 1.34 thorpej struct isa_dma_state *ids;
584 1.26 thorpej int chan;
585 1.26 thorpej caddr_t kva;
586 1.26 thorpej size_t size;
587 1.19 christos {
588 1.5 mycroft
589 1.26 thorpej if (chan < 0 || chan > 7) {
590 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
591 1.34 thorpej panic("_isa_dmamem_unmap");
592 1.5 mycroft }
593 1.26 thorpej
594 1.34 thorpej bus_dmamem_unmap(ids->ids_dmat, kva, size);
595 1.26 thorpej }
596 1.26 thorpej
597 1.26 thorpej int
598 1.34 thorpej _isa_dmamem_mmap(ids, chan, addr, size, off, prot, flags)
599 1.34 thorpej struct isa_dma_state *ids;
600 1.26 thorpej int chan;
601 1.26 thorpej bus_addr_t addr;
602 1.26 thorpej bus_size_t size;
603 1.26 thorpej int off, prot, flags;
604 1.26 thorpej {
605 1.26 thorpej bus_dma_segment_t seg;
606 1.26 thorpej
607 1.26 thorpej if (chan < 0 || chan > 7) {
608 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
609 1.34 thorpej panic("_isa_dmamem_mmap");
610 1.26 thorpej }
611 1.26 thorpej
612 1.26 thorpej seg.ds_addr = addr;
613 1.26 thorpej seg.ds_len = size;
614 1.26 thorpej
615 1.34 thorpej return (bus_dmamem_mmap(ids->ids_dmat, &seg, 1, off, prot, flags));
616 1.30 augustss }
617 1.30 augustss
618 1.30 augustss int
619 1.34 thorpej _isa_drq_isfree(ids, chan)
620 1.34 thorpej struct isa_dma_state *ids;
621 1.30 augustss int chan;
622 1.30 augustss {
623 1.34 thorpej
624 1.30 augustss if (chan < 0 || chan > 7) {
625 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
626 1.34 thorpej panic("_isa_drq_isfree");
627 1.30 augustss }
628 1.34 thorpej
629 1.34 thorpej return ISA_DMA_DRQ_ISFREE(ids, chan);
630 1.27 augustss }
631 1.27 augustss
632 1.27 augustss void *
633 1.34 thorpej _isa_malloc(ids, chan, size, pool, flags)
634 1.34 thorpej struct isa_dma_state *ids;
635 1.27 augustss int chan;
636 1.27 augustss size_t size;
637 1.27 augustss int pool;
638 1.27 augustss int flags;
639 1.27 augustss {
640 1.27 augustss bus_addr_t addr;
641 1.27 augustss caddr_t kva;
642 1.27 augustss int bflags;
643 1.27 augustss struct isa_mem *m;
644 1.27 augustss
645 1.27 augustss bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
646 1.27 augustss
647 1.34 thorpej if (_isa_dmamem_alloc(ids, chan, size, &addr, bflags))
648 1.27 augustss return 0;
649 1.34 thorpej if (_isa_dmamem_map(ids, chan, addr, size, &kva, bflags)) {
650 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
651 1.27 augustss return 0;
652 1.27 augustss }
653 1.27 augustss m = malloc(sizeof(*m), pool, flags);
654 1.27 augustss if (m == 0) {
655 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size);
656 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
657 1.27 augustss return 0;
658 1.27 augustss }
659 1.34 thorpej m->ids = ids;
660 1.27 augustss m->chan = chan;
661 1.27 augustss m->size = size;
662 1.27 augustss m->addr = addr;
663 1.27 augustss m->kva = kva;
664 1.27 augustss m->next = isa_mem_head;
665 1.27 augustss isa_mem_head = m;
666 1.27 augustss return (void *)kva;
667 1.27 augustss }
668 1.27 augustss
669 1.27 augustss void
670 1.34 thorpej _isa_free(addr, pool)
671 1.27 augustss void *addr;
672 1.27 augustss int pool;
673 1.27 augustss {
674 1.27 augustss struct isa_mem **mp, *m;
675 1.27 augustss caddr_t kva = (caddr_t)addr;
676 1.27 augustss
677 1.34 thorpej for(mp = &isa_mem_head; *mp && (*mp)->kva != kva;
678 1.34 thorpej mp = &(*mp)->next)
679 1.27 augustss ;
680 1.27 augustss m = *mp;
681 1.27 augustss if (!m) {
682 1.34 thorpej printf("_isa_free: freeing unallocted memory\n");
683 1.27 augustss return;
684 1.27 augustss }
685 1.27 augustss *mp = m->next;
686 1.34 thorpej _isa_dmamem_unmap(m->ids, m->chan, kva, m->size);
687 1.34 thorpej _isa_dmamem_free(m->ids, m->chan, m->addr, m->size);
688 1.27 augustss free(m, pool);
689 1.28 augustss }
690 1.28 augustss
691 1.28 augustss int
692 1.34 thorpej _isa_mappage(mem, off, prot)
693 1.28 augustss void *mem;
694 1.28 augustss int off;
695 1.28 augustss int prot;
696 1.28 augustss {
697 1.28 augustss struct isa_mem *m;
698 1.28 augustss
699 1.28 augustss for(m = isa_mem_head; m && m->kva != (caddr_t)mem; m = m->next)
700 1.28 augustss ;
701 1.28 augustss if (!m) {
702 1.34 thorpej printf("_isa_mappage: mapping unallocted memory\n");
703 1.28 augustss return -1;
704 1.28 augustss }
705 1.34 thorpej return _isa_dmamem_mmap(m->ids, m->chan, m->addr,
706 1.34 thorpej m->size, off, prot, BUS_DMA_WAITOK);
707 1.1 mycroft }
708