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