isadma.c revision 1.55 1 1.55 christos /* $NetBSD: isadma.c,v 1.55 2007/03/04 06:02:12 christos Exp $ */
2 1.26 thorpej
3 1.26 thorpej /*-
4 1.43 thorpej * Copyright (c) 1997, 1998, 2000 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.49 lukem
44 1.49 lukem #include <sys/cdefs.h>
45 1.55 christos __KERNEL_RCSID(0, "$NetBSD: isadma.c,v 1.55 2007/03/04 06:02:12 christos Exp $");
46 1.8 cgd
47 1.3 mycroft #include <sys/param.h>
48 1.3 mycroft #include <sys/systm.h>
49 1.19 christos #include <sys/proc.h>
50 1.26 thorpej #include <sys/device.h>
51 1.34 thorpej #include <sys/malloc.h>
52 1.5 mycroft
53 1.45 mrg #include <machine/bus.h>
54 1.3 mycroft
55 1.45 mrg #include <uvm/uvm_extern.h>
56 1.3 mycroft
57 1.12 cgd #include <dev/isa/isareg.h>
58 1.26 thorpej #include <dev/isa/isavar.h>
59 1.12 cgd #include <dev/isa/isadmavar.h>
60 1.12 cgd #include <dev/isa/isadmareg.h>
61 1.1 mycroft
62 1.34 thorpej struct isa_mem *isa_mem_head;
63 1.27 augustss
64 1.26 thorpej /*
65 1.26 thorpej * High byte of DMA address is stored in this DMAPG register for
66 1.26 thorpej * the Nth DMA channel.
67 1.26 thorpej */
68 1.18 mycroft static int dmapageport[2][4] = {
69 1.26 thorpej {0x7, 0x3, 0x1, 0x2},
70 1.26 thorpej {0xf, 0xb, 0x9, 0xa}
71 1.15 mycroft };
72 1.15 mycroft
73 1.37 thorpej static u_int8_t dmamode[] = {
74 1.37 thorpej /* write to device/read from device */
75 1.17 mycroft DMA37MD_READ | DMA37MD_SINGLE,
76 1.15 mycroft DMA37MD_WRITE | DMA37MD_SINGLE,
77 1.40 mycroft
78 1.40 mycroft /* write to device/read from device */
79 1.40 mycroft DMA37MD_READ | DMA37MD_DEMAND,
80 1.40 mycroft DMA37MD_WRITE | DMA37MD_DEMAND,
81 1.37 thorpej
82 1.37 thorpej /* write to device/read from device - DMAMODE_LOOP */
83 1.25 mycroft DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
84 1.37 thorpej DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP,
85 1.37 thorpej
86 1.37 thorpej /* write to device/read from device - DMAMODE_LOOPDEMAND */
87 1.37 thorpej DMA37MD_READ | DMA37MD_DEMAND | DMA37MD_LOOP,
88 1.37 thorpej DMA37MD_WRITE | DMA37MD_DEMAND | DMA37MD_LOOP,
89 1.15 mycroft };
90 1.1 mycroft
91 1.53 perry static inline void _isa_dmaunmask(struct isa_dma_state *, int);
92 1.53 perry static inline void _isa_dmamask(struct isa_dma_state *, int);
93 1.19 christos
94 1.26 thorpej static inline void
95 1.34 thorpej _isa_dmaunmask(ids, chan)
96 1.34 thorpej struct isa_dma_state *ids;
97 1.23 mycroft int chan;
98 1.23 mycroft {
99 1.23 mycroft int ochan = chan & 3;
100 1.23 mycroft
101 1.35 thorpej ISA_DMA_MASK_CLR(ids, chan);
102 1.35 thorpej
103 1.35 thorpej /*
104 1.35 thorpej * If DMA is frozen, don't unmask it now. It will be
105 1.35 thorpej * unmasked when DMA is thawed again.
106 1.35 thorpej */
107 1.35 thorpej if (ids->ids_frozen)
108 1.35 thorpej return;
109 1.35 thorpej
110 1.23 mycroft /* set dma channel mode, and set dma channel mode */
111 1.23 mycroft if ((chan & 4) == 0)
112 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
113 1.26 thorpej DMA1_SMSK, ochan | DMA37SM_CLEAR);
114 1.23 mycroft else
115 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
116 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_CLEAR);
117 1.23 mycroft }
118 1.23 mycroft
119 1.26 thorpej static inline void
120 1.34 thorpej _isa_dmamask(ids, chan)
121 1.34 thorpej struct isa_dma_state *ids;
122 1.23 mycroft int chan;
123 1.23 mycroft {
124 1.23 mycroft int ochan = chan & 3;
125 1.23 mycroft
126 1.35 thorpej ISA_DMA_MASK_SET(ids, chan);
127 1.35 thorpej
128 1.35 thorpej /*
129 1.35 thorpej * XXX Should we avoid masking the channel if DMA is
130 1.35 thorpej * XXX frozen? It seems like what we're doing should
131 1.35 thorpej * XXX be safe, and we do need to reset FFC...
132 1.35 thorpej */
133 1.35 thorpej
134 1.23 mycroft /* set dma channel mode, and set dma channel mode */
135 1.23 mycroft if ((chan & 4) == 0) {
136 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
137 1.26 thorpej DMA1_SMSK, ochan | DMA37SM_SET);
138 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
139 1.26 thorpej DMA1_FFC, 0);
140 1.23 mycroft } else {
141 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
142 1.26 thorpej DMA2_SMSK, ochan | DMA37SM_SET);
143 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
144 1.26 thorpej DMA2_FFC, 0);
145 1.23 mycroft }
146 1.23 mycroft }
147 1.23 mycroft
148 1.1 mycroft /*
149 1.34 thorpej * _isa_dmainit(): Initialize the isa_dma_state for this chipset.
150 1.34 thorpej */
151 1.34 thorpej void
152 1.34 thorpej _isa_dmainit(ids, bst, dmat, dev)
153 1.34 thorpej struct isa_dma_state *ids;
154 1.34 thorpej bus_space_tag_t bst;
155 1.34 thorpej bus_dma_tag_t dmat;
156 1.34 thorpej struct device *dev;
157 1.34 thorpej {
158 1.43 thorpej int chan;
159 1.34 thorpej
160 1.34 thorpej ids->ids_dev = dev;
161 1.34 thorpej
162 1.34 thorpej if (ids->ids_initialized) {
163 1.34 thorpej /*
164 1.34 thorpej * Some systems may have e.g. `ofisa' (OpenFirmware
165 1.34 thorpej * configuration of ISA bus) and a regular `isa'.
166 1.34 thorpej * We allow both to call the initialization function,
167 1.34 thorpej * and take the device name from the last caller
168 1.34 thorpej * (assuming it will be the indirect ISA bus). Since
169 1.34 thorpej * `ofisa' and `isa' are the same bus with different
170 1.34 thorpej * configuration mechanisms, the space and dma tags
171 1.34 thorpej * must be the same!
172 1.34 thorpej */
173 1.34 thorpej if (ids->ids_bst != bst || ids->ids_dmat != dmat)
174 1.34 thorpej panic("_isa_dmainit: inconsistent ISA tags");
175 1.34 thorpej } else {
176 1.34 thorpej ids->ids_bst = bst;
177 1.34 thorpej ids->ids_dmat = dmat;
178 1.34 thorpej
179 1.34 thorpej /*
180 1.34 thorpej * Map the registers used by the ISA DMA controller.
181 1.34 thorpej */
182 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA1, DMA1_IOSIZE, 0,
183 1.34 thorpej &ids->ids_dma1h))
184 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #1");
185 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMA2, DMA2_IOSIZE, 0,
186 1.34 thorpej &ids->ids_dma2h))
187 1.34 thorpej panic("_isa_dmainit: unable to map DMA controller #2");
188 1.34 thorpej if (bus_space_map(ids->ids_bst, IO_DMAPG, 0xf, 0,
189 1.34 thorpej &ids->ids_dmapgh))
190 1.34 thorpej panic("_isa_dmainit: unable to map DMA page registers");
191 1.34 thorpej
192 1.35 thorpej /*
193 1.35 thorpej * All 8 DMA channels start out "masked".
194 1.35 thorpej */
195 1.35 thorpej ids->ids_masked = 0xff;
196 1.35 thorpej
197 1.43 thorpej /*
198 1.43 thorpej * Initialize the max transfer size for each channel, if
199 1.43 thorpej * it is not initialized already (i.e. by a bus-dependent
200 1.43 thorpej * front-end).
201 1.43 thorpej */
202 1.43 thorpej for (chan = 0; chan < 8; chan++) {
203 1.43 thorpej if (ids->ids_maxsize[chan] == 0)
204 1.43 thorpej ids->ids_maxsize[chan] =
205 1.43 thorpej ISA_DMA_MAXSIZE_DEFAULT(chan);
206 1.43 thorpej }
207 1.43 thorpej
208 1.34 thorpej ids->ids_initialized = 1;
209 1.38 thorpej
210 1.38 thorpej /*
211 1.38 thorpej * DRQ 4 is used to chain the two 8237s together; make
212 1.38 thorpej * sure it's always cascaded, and that it will be unmasked
213 1.38 thorpej * when DMA is thawed.
214 1.38 thorpej */
215 1.38 thorpej _isa_dmacascade(ids, 4);
216 1.34 thorpej }
217 1.34 thorpej }
218 1.34 thorpej
219 1.34 thorpej /*
220 1.34 thorpej * _isa_dmacascade(): program 8237 DMA controller channel to accept
221 1.1 mycroft * external dma control by a board.
222 1.1 mycroft */
223 1.36 thorpej int
224 1.34 thorpej _isa_dmacascade(ids, chan)
225 1.34 thorpej struct isa_dma_state *ids;
226 1.4 mycroft int chan;
227 1.1 mycroft {
228 1.23 mycroft int ochan = chan & 3;
229 1.4 mycroft
230 1.26 thorpej if (chan < 0 || chan > 7) {
231 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
232 1.36 thorpej return (EINVAL);
233 1.26 thorpej }
234 1.26 thorpej
235 1.34 thorpej if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
236 1.34 thorpej printf("%s: DRQ %d is not free\n", ids->ids_dev->dv_xname,
237 1.34 thorpej chan);
238 1.36 thorpej return (EAGAIN);
239 1.26 thorpej }
240 1.26 thorpej
241 1.34 thorpej ISA_DMA_DRQ_ALLOC(ids, chan);
242 1.1 mycroft
243 1.1 mycroft /* set dma channel mode, and set dma channel mode */
244 1.23 mycroft if ((chan & 4) == 0)
245 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
246 1.26 thorpej DMA1_MODE, ochan | DMA37MD_CASCADE);
247 1.26 thorpej else
248 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
249 1.26 thorpej DMA2_MODE, ochan | DMA37MD_CASCADE);
250 1.26 thorpej
251 1.34 thorpej _isa_dmaunmask(ids, chan);
252 1.36 thorpej return (0);
253 1.26 thorpej }
254 1.26 thorpej
255 1.52 fvdl int
256 1.52 fvdl _isa_drq_alloc(ids, chan)
257 1.52 fvdl struct isa_dma_state *ids;
258 1.52 fvdl int chan;
259 1.52 fvdl {
260 1.52 fvdl if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0)
261 1.52 fvdl return EBUSY;
262 1.52 fvdl ISA_DMA_DRQ_ALLOC(ids, chan);
263 1.52 fvdl return 0;
264 1.52 fvdl }
265 1.52 fvdl
266 1.52 fvdl int
267 1.52 fvdl _isa_drq_free(ids, chan)
268 1.52 fvdl struct isa_dma_state *ids;
269 1.52 fvdl int chan;
270 1.52 fvdl {
271 1.52 fvdl if (ISA_DMA_DRQ_ISFREE(ids, chan))
272 1.52 fvdl return EINVAL;
273 1.52 fvdl ISA_DMA_DRQ_FREE(ids, chan);
274 1.52 fvdl return 0;
275 1.52 fvdl }
276 1.52 fvdl
277 1.43 thorpej bus_size_t
278 1.43 thorpej _isa_dmamaxsize(ids, chan)
279 1.43 thorpej struct isa_dma_state *ids;
280 1.43 thorpej int chan;
281 1.43 thorpej {
282 1.43 thorpej
283 1.43 thorpej if (chan < 0 || chan > 7) {
284 1.43 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
285 1.43 thorpej return (0);
286 1.43 thorpej }
287 1.43 thorpej
288 1.43 thorpej return (ids->ids_maxsize[chan]);
289 1.43 thorpej }
290 1.43 thorpej
291 1.26 thorpej int
292 1.34 thorpej _isa_dmamap_create(ids, chan, size, flags)
293 1.34 thorpej struct isa_dma_state *ids;
294 1.26 thorpej int chan;
295 1.26 thorpej bus_size_t size;
296 1.26 thorpej int flags;
297 1.26 thorpej {
298 1.42 mycroft int error;
299 1.26 thorpej
300 1.26 thorpej if (chan < 0 || chan > 7) {
301 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
302 1.36 thorpej return (EINVAL);
303 1.26 thorpej }
304 1.26 thorpej
305 1.43 thorpej if (size > ids->ids_maxsize[chan])
306 1.26 thorpej return (EINVAL);
307 1.26 thorpej
308 1.43 thorpej error = bus_dmamap_create(ids->ids_dmat, size, 1, size,
309 1.43 thorpej ids->ids_maxsize[chan], flags, &ids->ids_dmamaps[chan]);
310 1.42 mycroft
311 1.42 mycroft return (error);
312 1.26 thorpej }
313 1.26 thorpej
314 1.26 thorpej void
315 1.34 thorpej _isa_dmamap_destroy(ids, chan)
316 1.34 thorpej struct isa_dma_state *ids;
317 1.26 thorpej int chan;
318 1.26 thorpej {
319 1.26 thorpej
320 1.26 thorpej if (chan < 0 || chan > 7) {
321 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
322 1.26 thorpej goto lose;
323 1.26 thorpej }
324 1.26 thorpej
325 1.34 thorpej bus_dmamap_destroy(ids->ids_dmat, ids->ids_dmamaps[chan]);
326 1.26 thorpej return;
327 1.26 thorpej
328 1.26 thorpej lose:
329 1.34 thorpej panic("_isa_dmamap_destroy");
330 1.1 mycroft }
331 1.1 mycroft
332 1.1 mycroft /*
333 1.34 thorpej * _isa_dmastart(): program 8237 DMA controller channel and set it
334 1.26 thorpej * in motion.
335 1.1 mycroft */
336 1.26 thorpej int
337 1.34 thorpej _isa_dmastart(ids, chan, addr, nbytes, p, flags, busdmaflags)
338 1.34 thorpej struct isa_dma_state *ids;
339 1.26 thorpej int chan;
340 1.26 thorpej void *addr;
341 1.26 thorpej bus_size_t nbytes;
342 1.26 thorpej struct proc *p;
343 1.5 mycroft int flags;
344 1.26 thorpej int busdmaflags;
345 1.1 mycroft {
346 1.26 thorpej bus_dmamap_t dmam;
347 1.26 thorpej bus_addr_t dmaaddr;
348 1.1 mycroft int waport;
349 1.23 mycroft int ochan = chan & 3;
350 1.26 thorpej int error;
351 1.26 thorpej
352 1.26 thorpej if (chan < 0 || chan > 7) {
353 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
354 1.26 thorpej goto lose;
355 1.26 thorpej }
356 1.26 thorpej
357 1.26 thorpej #ifdef ISADMA_DEBUG
358 1.34 thorpej printf("_isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
359 1.26 thorpej "flags 0x%x, dmaflags 0x%x\n",
360 1.26 thorpej chan, addr, nbytes, p, flags, busdmaflags);
361 1.26 thorpej #endif
362 1.52 fvdl
363 1.52 fvdl if (ISA_DMA_DRQ_ISFREE(ids, chan)) {
364 1.52 fvdl printf("%s: dma start on free channel %d\n",
365 1.52 fvdl ids->ids_dev->dv_xname, chan);
366 1.52 fvdl goto lose;
367 1.52 fvdl }
368 1.26 thorpej
369 1.26 thorpej if (chan & 4) {
370 1.26 thorpej if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
371 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
372 1.47 briggs ids->ids_dev->dv_xname, chan,
373 1.47 briggs (unsigned long) nbytes, addr);
374 1.26 thorpej goto lose;
375 1.26 thorpej }
376 1.26 thorpej } else {
377 1.26 thorpej if (nbytes > (1 << 16)) {
378 1.26 thorpej printf("%s: drq %d, nbytes 0x%lx\n",
379 1.47 briggs ids->ids_dev->dv_xname, chan,
380 1.47 briggs (unsigned long) nbytes);
381 1.26 thorpej goto lose;
382 1.26 thorpej }
383 1.26 thorpej }
384 1.26 thorpej
385 1.34 thorpej dmam = ids->ids_dmamaps[chan];
386 1.31 augustss if (dmam == NULL)
387 1.50 provos panic("_isa_dmastart: no DMA map for chan %d", chan);
388 1.26 thorpej
389 1.34 thorpej error = bus_dmamap_load(ids->ids_dmat, dmam, addr, nbytes,
390 1.48 thorpej p, busdmaflags |
391 1.48 thorpej ((flags & DMAMODE_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
392 1.26 thorpej if (error)
393 1.26 thorpej return (error);
394 1.1 mycroft
395 1.13 mycroft #ifdef ISADMA_DEBUG
396 1.26 thorpej __asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
397 1.4 mycroft #endif
398 1.1 mycroft
399 1.26 thorpej if (flags & DMAMODE_READ) {
400 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
401 1.33 thorpej BUS_DMASYNC_PREREAD);
402 1.34 thorpej ids->ids_dmareads |= (1 << chan);
403 1.26 thorpej } else {
404 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
405 1.33 thorpej BUS_DMASYNC_PREWRITE);
406 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
407 1.1 mycroft }
408 1.1 mycroft
409 1.26 thorpej dmaaddr = dmam->dm_segs[0].ds_addr;
410 1.26 thorpej
411 1.26 thorpej #ifdef ISADMA_DEBUG
412 1.26 thorpej printf(" dmaaddr 0x%lx\n", dmaaddr);
413 1.26 thorpej
414 1.26 thorpej __asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
415 1.26 thorpej #endif
416 1.24 mycroft
417 1.34 thorpej ids->ids_dmalength[chan] = nbytes;
418 1.1 mycroft
419 1.34 thorpej _isa_dmamask(ids, chan);
420 1.34 thorpej ids->ids_dmafinished &= ~(1 << chan);
421 1.14 mycroft
422 1.1 mycroft if ((chan & 4) == 0) {
423 1.23 mycroft /* set dma channel mode */
424 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, DMA1_MODE,
425 1.26 thorpej ochan | dmamode[flags]);
426 1.1 mycroft
427 1.1 mycroft /* send start address */
428 1.23 mycroft waport = DMA1_CHN(ochan);
429 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
430 1.26 thorpej dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
431 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
432 1.26 thorpej dmaaddr & 0xff);
433 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
434 1.26 thorpej (dmaaddr >> 8) & 0xff);
435 1.1 mycroft
436 1.1 mycroft /* send count */
437 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
438 1.26 thorpej (--nbytes) & 0xff);
439 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
440 1.26 thorpej (nbytes >> 8) & 0xff);
441 1.1 mycroft } else {
442 1.23 mycroft /* set dma channel mode */
443 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, DMA2_MODE,
444 1.26 thorpej ochan | dmamode[flags]);
445 1.1 mycroft
446 1.1 mycroft /* send start address */
447 1.23 mycroft waport = DMA2_CHN(ochan);
448 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
449 1.26 thorpej dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
450 1.26 thorpej dmaaddr >>= 1;
451 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
452 1.26 thorpej dmaaddr & 0xff);
453 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
454 1.26 thorpej (dmaaddr >> 8) & 0xff);
455 1.1 mycroft
456 1.1 mycroft /* send count */
457 1.1 mycroft nbytes >>= 1;
458 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
459 1.26 thorpej (--nbytes) & 0xff);
460 1.34 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
461 1.26 thorpej (nbytes >> 8) & 0xff);
462 1.23 mycroft }
463 1.1 mycroft
464 1.34 thorpej _isa_dmaunmask(ids, chan);
465 1.26 thorpej return (0);
466 1.26 thorpej
467 1.26 thorpej lose:
468 1.34 thorpej panic("_isa_dmastart");
469 1.1 mycroft }
470 1.1 mycroft
471 1.1 mycroft void
472 1.34 thorpej _isa_dmaabort(ids, chan)
473 1.34 thorpej struct isa_dma_state *ids;
474 1.4 mycroft int chan;
475 1.1 mycroft {
476 1.1 mycroft
477 1.26 thorpej if (chan < 0 || chan > 7) {
478 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
479 1.34 thorpej panic("_isa_dmaabort");
480 1.26 thorpej }
481 1.1 mycroft
482 1.34 thorpej _isa_dmamask(ids, chan);
483 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, ids->ids_dmamaps[chan]);
484 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
485 1.22 mycroft }
486 1.22 mycroft
487 1.26 thorpej bus_size_t
488 1.34 thorpej _isa_dmacount(ids, chan)
489 1.34 thorpej struct isa_dma_state *ids;
490 1.22 mycroft int chan;
491 1.22 mycroft {
492 1.22 mycroft int waport;
493 1.26 thorpej bus_size_t nbytes;
494 1.23 mycroft int ochan = chan & 3;
495 1.22 mycroft
496 1.26 thorpej if (chan < 0 || chan > 7) {
497 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
498 1.26 thorpej panic("isa_dmacount");
499 1.26 thorpej }
500 1.22 mycroft
501 1.34 thorpej _isa_dmamask(ids, chan);
502 1.23 mycroft
503 1.24 mycroft /*
504 1.24 mycroft * We have to shift the byte count by 1. If we're in auto-initialize
505 1.24 mycroft * mode, the count may have wrapped around to the initial value. We
506 1.24 mycroft * can't use the TC bit to check for this case, so instead we compare
507 1.24 mycroft * against the original byte count.
508 1.24 mycroft * If we're not in auto-initialize mode, then the count will wrap to
509 1.24 mycroft * -1, so we also handle that case.
510 1.24 mycroft */
511 1.24 mycroft if ((chan & 4) == 0) {
512 1.24 mycroft waport = DMA1_CHN(ochan);
513 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
514 1.26 thorpej waport + 1) + 1;
515 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
516 1.26 thorpej waport + 1) << 8;
517 1.24 mycroft nbytes &= 0xffff;
518 1.24 mycroft } else {
519 1.24 mycroft waport = DMA2_CHN(ochan);
520 1.34 thorpej nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
521 1.26 thorpej waport + 2) + 1;
522 1.34 thorpej nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
523 1.26 thorpej waport + 2) << 8;
524 1.24 mycroft nbytes <<= 1;
525 1.24 mycroft nbytes &= 0x1ffff;
526 1.24 mycroft }
527 1.24 mycroft
528 1.34 thorpej if (nbytes == ids->ids_dmalength[chan])
529 1.23 mycroft nbytes = 0;
530 1.22 mycroft
531 1.34 thorpej _isa_dmaunmask(ids, chan);
532 1.22 mycroft return (nbytes);
533 1.1 mycroft }
534 1.1 mycroft
535 1.13 mycroft int
536 1.34 thorpej _isa_dmafinished(ids, chan)
537 1.34 thorpej struct isa_dma_state *ids;
538 1.5 mycroft int chan;
539 1.1 mycroft {
540 1.1 mycroft
541 1.26 thorpej if (chan < 0 || chan > 7) {
542 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
543 1.34 thorpej panic("_isa_dmafinished");
544 1.26 thorpej }
545 1.1 mycroft
546 1.1 mycroft /* check that the terminal count was reached */
547 1.1 mycroft if ((chan & 4) == 0)
548 1.34 thorpej ids->ids_dmafinished |= bus_space_read_1(ids->ids_bst,
549 1.34 thorpej ids->ids_dma1h, DMA1_SR) & 0x0f;
550 1.1 mycroft else
551 1.34 thorpej ids->ids_dmafinished |= (bus_space_read_1(ids->ids_bst,
552 1.34 thorpej ids->ids_dma2h, DMA2_SR) & 0x0f) << 4;
553 1.14 mycroft
554 1.34 thorpej return ((ids->ids_dmafinished & (1 << chan)) != 0);
555 1.13 mycroft }
556 1.13 mycroft
557 1.13 mycroft void
558 1.34 thorpej _isa_dmadone(ids, chan)
559 1.34 thorpej struct isa_dma_state *ids;
560 1.13 mycroft int chan;
561 1.13 mycroft {
562 1.26 thorpej bus_dmamap_t dmam;
563 1.13 mycroft
564 1.26 thorpej if (chan < 0 || chan > 7) {
565 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
566 1.34 thorpej panic("_isa_dmadone");
567 1.26 thorpej }
568 1.26 thorpej
569 1.34 thorpej dmam = ids->ids_dmamaps[chan];
570 1.26 thorpej
571 1.34 thorpej _isa_dmamask(ids, chan);
572 1.14 mycroft
573 1.34 thorpej if (_isa_dmafinished(ids, chan) == 0)
574 1.34 thorpej printf("%s: _isa_dmadone: channel %d not finished\n",
575 1.34 thorpej ids->ids_dev->dv_xname, chan);
576 1.23 mycroft
577 1.34 thorpej bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
578 1.34 thorpej (ids->ids_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
579 1.26 thorpej BUS_DMASYNC_POSTWRITE);
580 1.9 mycroft
581 1.34 thorpej bus_dmamap_unload(ids->ids_dmat, dmam);
582 1.34 thorpej ids->ids_dmareads &= ~(1 << chan);
583 1.35 thorpej }
584 1.35 thorpej
585 1.35 thorpej void
586 1.35 thorpej _isa_dmafreeze(ids)
587 1.35 thorpej struct isa_dma_state *ids;
588 1.35 thorpej {
589 1.35 thorpej int s;
590 1.35 thorpej
591 1.35 thorpej s = splhigh();
592 1.35 thorpej
593 1.35 thorpej if (ids->ids_frozen == 0) {
594 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
595 1.35 thorpej DMA1_MASK, 0x0f);
596 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
597 1.35 thorpej DMA2_MASK, 0x0f);
598 1.35 thorpej }
599 1.35 thorpej
600 1.35 thorpej ids->ids_frozen++;
601 1.35 thorpej if (ids->ids_frozen < 1)
602 1.35 thorpej panic("_isa_dmafreeze: overflow");
603 1.35 thorpej
604 1.35 thorpej splx(s);
605 1.35 thorpej }
606 1.35 thorpej
607 1.35 thorpej void
608 1.35 thorpej _isa_dmathaw(ids)
609 1.35 thorpej struct isa_dma_state *ids;
610 1.35 thorpej {
611 1.35 thorpej int s;
612 1.35 thorpej
613 1.35 thorpej s = splhigh();
614 1.35 thorpej
615 1.35 thorpej ids->ids_frozen--;
616 1.35 thorpej if (ids->ids_frozen < 0)
617 1.35 thorpej panic("_isa_dmathaw: underflow");
618 1.35 thorpej
619 1.35 thorpej if (ids->ids_frozen == 0) {
620 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
621 1.35 thorpej DMA1_MASK, ids->ids_masked & 0x0f);
622 1.35 thorpej bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
623 1.35 thorpej DMA2_MASK, (ids->ids_masked >> 4) & 0x0f);
624 1.35 thorpej }
625 1.35 thorpej
626 1.35 thorpej splx(s);
627 1.1 mycroft }
628 1.1 mycroft
629 1.1 mycroft int
630 1.34 thorpej _isa_dmamem_alloc(ids, chan, size, addrp, flags)
631 1.34 thorpej struct isa_dma_state *ids;
632 1.26 thorpej int chan;
633 1.26 thorpej bus_size_t size;
634 1.26 thorpej bus_addr_t *addrp;
635 1.26 thorpej int flags;
636 1.26 thorpej {
637 1.26 thorpej bus_dma_segment_t seg;
638 1.26 thorpej int error, boundary, rsegs;
639 1.26 thorpej
640 1.26 thorpej if (chan < 0 || chan > 7) {
641 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
642 1.34 thorpej panic("_isa_dmamem_alloc");
643 1.1 mycroft }
644 1.26 thorpej
645 1.26 thorpej boundary = (chan & 4) ? (1 << 17) : (1 << 16);
646 1.26 thorpej
647 1.26 thorpej size = round_page(size);
648 1.26 thorpej
649 1.46 thorpej error = bus_dmamem_alloc(ids->ids_dmat, size, PAGE_SIZE, boundary,
650 1.26 thorpej &seg, 1, &rsegs, flags);
651 1.26 thorpej if (error)
652 1.26 thorpej return (error);
653 1.26 thorpej
654 1.26 thorpej *addrp = seg.ds_addr;
655 1.26 thorpej return (0);
656 1.5 mycroft }
657 1.5 mycroft
658 1.26 thorpej void
659 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size)
660 1.34 thorpej struct isa_dma_state *ids;
661 1.26 thorpej int chan;
662 1.26 thorpej bus_addr_t addr;
663 1.26 thorpej bus_size_t size;
664 1.26 thorpej {
665 1.26 thorpej bus_dma_segment_t seg;
666 1.26 thorpej
667 1.26 thorpej if (chan < 0 || chan > 7) {
668 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
669 1.34 thorpej panic("_isa_dmamem_free");
670 1.26 thorpej }
671 1.26 thorpej
672 1.26 thorpej seg.ds_addr = addr;
673 1.26 thorpej seg.ds_len = size;
674 1.5 mycroft
675 1.34 thorpej bus_dmamem_free(ids->ids_dmat, &seg, 1);
676 1.26 thorpej }
677 1.5 mycroft
678 1.26 thorpej int
679 1.34 thorpej _isa_dmamem_map(ids, chan, addr, size, kvap, flags)
680 1.34 thorpej struct isa_dma_state *ids;
681 1.26 thorpej int chan;
682 1.26 thorpej bus_addr_t addr;
683 1.26 thorpej bus_size_t size;
684 1.55 christos void **kvap;
685 1.26 thorpej int flags;
686 1.26 thorpej {
687 1.26 thorpej bus_dma_segment_t seg;
688 1.26 thorpej
689 1.26 thorpej if (chan < 0 || chan > 7) {
690 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
691 1.34 thorpej panic("_isa_dmamem_map");
692 1.5 mycroft }
693 1.5 mycroft
694 1.26 thorpej seg.ds_addr = addr;
695 1.26 thorpej seg.ds_len = size;
696 1.26 thorpej
697 1.34 thorpej return (bus_dmamem_map(ids->ids_dmat, &seg, 1, size, kvap, flags));
698 1.5 mycroft }
699 1.5 mycroft
700 1.5 mycroft void
701 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size)
702 1.34 thorpej struct isa_dma_state *ids;
703 1.26 thorpej int chan;
704 1.55 christos void *kva;
705 1.26 thorpej size_t size;
706 1.19 christos {
707 1.5 mycroft
708 1.26 thorpej if (chan < 0 || chan > 7) {
709 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
710 1.34 thorpej panic("_isa_dmamem_unmap");
711 1.5 mycroft }
712 1.26 thorpej
713 1.34 thorpej bus_dmamem_unmap(ids->ids_dmat, kva, size);
714 1.26 thorpej }
715 1.26 thorpej
716 1.44 simonb paddr_t
717 1.34 thorpej _isa_dmamem_mmap(ids, chan, addr, size, off, prot, flags)
718 1.34 thorpej struct isa_dma_state *ids;
719 1.26 thorpej int chan;
720 1.26 thorpej bus_addr_t addr;
721 1.26 thorpej bus_size_t size;
722 1.44 simonb off_t off;
723 1.44 simonb int prot, flags;
724 1.26 thorpej {
725 1.26 thorpej bus_dma_segment_t seg;
726 1.26 thorpej
727 1.26 thorpej if (chan < 0 || chan > 7) {
728 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
729 1.34 thorpej panic("_isa_dmamem_mmap");
730 1.26 thorpej }
731 1.39 mrg
732 1.39 mrg if (off < 0)
733 1.39 mrg return (-1);
734 1.26 thorpej
735 1.26 thorpej seg.ds_addr = addr;
736 1.26 thorpej seg.ds_len = size;
737 1.26 thorpej
738 1.34 thorpej return (bus_dmamem_mmap(ids->ids_dmat, &seg, 1, off, prot, flags));
739 1.30 augustss }
740 1.30 augustss
741 1.30 augustss int
742 1.34 thorpej _isa_drq_isfree(ids, chan)
743 1.34 thorpej struct isa_dma_state *ids;
744 1.30 augustss int chan;
745 1.30 augustss {
746 1.34 thorpej
747 1.30 augustss if (chan < 0 || chan > 7) {
748 1.34 thorpej printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
749 1.34 thorpej panic("_isa_drq_isfree");
750 1.30 augustss }
751 1.34 thorpej
752 1.34 thorpej return ISA_DMA_DRQ_ISFREE(ids, chan);
753 1.27 augustss }
754 1.27 augustss
755 1.27 augustss void *
756 1.34 thorpej _isa_malloc(ids, chan, size, pool, flags)
757 1.34 thorpej struct isa_dma_state *ids;
758 1.27 augustss int chan;
759 1.27 augustss size_t size;
760 1.51 thorpej struct malloc_type *pool;
761 1.27 augustss int flags;
762 1.27 augustss {
763 1.27 augustss bus_addr_t addr;
764 1.55 christos void *kva;
765 1.27 augustss int bflags;
766 1.27 augustss struct isa_mem *m;
767 1.27 augustss
768 1.27 augustss bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
769 1.27 augustss
770 1.34 thorpej if (_isa_dmamem_alloc(ids, chan, size, &addr, bflags))
771 1.27 augustss return 0;
772 1.34 thorpej if (_isa_dmamem_map(ids, chan, addr, size, &kva, bflags)) {
773 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
774 1.27 augustss return 0;
775 1.27 augustss }
776 1.27 augustss m = malloc(sizeof(*m), pool, flags);
777 1.27 augustss if (m == 0) {
778 1.34 thorpej _isa_dmamem_unmap(ids, chan, kva, size);
779 1.34 thorpej _isa_dmamem_free(ids, chan, addr, size);
780 1.27 augustss return 0;
781 1.27 augustss }
782 1.34 thorpej m->ids = ids;
783 1.27 augustss m->chan = chan;
784 1.27 augustss m->size = size;
785 1.27 augustss m->addr = addr;
786 1.27 augustss m->kva = kva;
787 1.27 augustss m->next = isa_mem_head;
788 1.27 augustss isa_mem_head = m;
789 1.27 augustss return (void *)kva;
790 1.27 augustss }
791 1.27 augustss
792 1.27 augustss void
793 1.34 thorpej _isa_free(addr, pool)
794 1.27 augustss void *addr;
795 1.51 thorpej struct malloc_type *pool;
796 1.27 augustss {
797 1.27 augustss struct isa_mem **mp, *m;
798 1.55 christos void *kva = (void *)addr;
799 1.27 augustss
800 1.34 thorpej for(mp = &isa_mem_head; *mp && (*mp)->kva != kva;
801 1.34 thorpej mp = &(*mp)->next)
802 1.27 augustss ;
803 1.27 augustss m = *mp;
804 1.27 augustss if (!m) {
805 1.34 thorpej printf("_isa_free: freeing unallocted memory\n");
806 1.27 augustss return;
807 1.27 augustss }
808 1.27 augustss *mp = m->next;
809 1.34 thorpej _isa_dmamem_unmap(m->ids, m->chan, kva, m->size);
810 1.34 thorpej _isa_dmamem_free(m->ids, m->chan, m->addr, m->size);
811 1.27 augustss free(m, pool);
812 1.28 augustss }
813 1.28 augustss
814 1.44 simonb paddr_t
815 1.34 thorpej _isa_mappage(mem, off, prot)
816 1.28 augustss void *mem;
817 1.44 simonb off_t off;
818 1.28 augustss int prot;
819 1.28 augustss {
820 1.28 augustss struct isa_mem *m;
821 1.28 augustss
822 1.55 christos for(m = isa_mem_head; m && m->kva != (void *)mem; m = m->next)
823 1.28 augustss ;
824 1.28 augustss if (!m) {
825 1.34 thorpej printf("_isa_mappage: mapping unallocted memory\n");
826 1.28 augustss return -1;
827 1.28 augustss }
828 1.34 thorpej return _isa_dmamem_mmap(m->ids, m->chan, m->addr,
829 1.34 thorpej m->size, off, prot, BUS_DMA_WAITOK);
830 1.1 mycroft }
831