isadma.c revision 1.27 1 /* $NetBSD: isadma.c,v 1.27 1997/07/27 01:16:57 augustss Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Device driver for the ISA on-board DMA controller.
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/device.h>
48
49 #include <vm/vm.h>
50
51 #include <machine/bus.h>
52
53 #include <dev/isa/isareg.h>
54 #include <dev/isa/isavar.h>
55 #include <dev/isa/isadmavar.h>
56 #include <dev/isa/isadmareg.h>
57
58 /* Used by isa_malloc() */
59 #include <sys/malloc.h>
60 struct isa_mem {
61 struct device *isadev;
62 int chan;
63 bus_size_t size;
64 bus_addr_t addr;
65 caddr_t kva;
66 struct isa_mem *next;
67 } *isa_mem_head = 0;
68
69 /*
70 * High byte of DMA address is stored in this DMAPG register for
71 * the Nth DMA channel.
72 */
73 static int dmapageport[2][4] = {
74 {0x7, 0x3, 0x1, 0x2},
75 {0xf, 0xb, 0x9, 0xa}
76 };
77
78 static u_int8_t dmamode[4] = {
79 DMA37MD_READ | DMA37MD_SINGLE,
80 DMA37MD_WRITE | DMA37MD_SINGLE,
81 DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
82 DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP
83 };
84
85 static inline void isa_dmaunmask __P((struct isa_softc *, int));
86 static inline void isa_dmamask __P((struct isa_softc *, int));
87
88 static inline void
89 isa_dmaunmask(sc, chan)
90 struct isa_softc *sc;
91 int chan;
92 {
93 int ochan = chan & 3;
94
95 /* set dma channel mode, and set dma channel mode */
96 if ((chan & 4) == 0)
97 bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
98 DMA1_SMSK, ochan | DMA37SM_CLEAR);
99 else
100 bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
101 DMA2_SMSK, ochan | DMA37SM_CLEAR);
102 }
103
104 static inline void
105 isa_dmamask(sc, chan)
106 struct isa_softc *sc;
107 int chan;
108 {
109 int ochan = chan & 3;
110
111 /* set dma channel mode, and set dma channel mode */
112 if ((chan & 4) == 0) {
113 bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
114 DMA1_SMSK, ochan | DMA37SM_SET);
115 bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
116 DMA1_FFC, 0);
117 } else {
118 bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
119 DMA2_SMSK, ochan | DMA37SM_SET);
120 bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
121 DMA2_FFC, 0);
122 }
123 }
124
125 /*
126 * isa_dmacascade(): program 8237 DMA controller channel to accept
127 * external dma control by a board.
128 */
129 void
130 isa_dmacascade(isadev, chan)
131 struct device *isadev;
132 int chan;
133 {
134 struct isa_softc *sc = (struct isa_softc *)isadev;
135 int ochan = chan & 3;
136
137 if (chan < 0 || chan > 7) {
138 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
139 goto lose;
140 }
141
142 if (ISA_DRQ_ISFREE(sc, chan) == 0) {
143 printf("%s: DRQ %d is not free\n", sc->sc_dev.dv_xname, chan);
144 goto lose;
145 }
146
147 ISA_DRQ_ALLOC(sc, chan);
148
149 /* set dma channel mode, and set dma channel mode */
150 if ((chan & 4) == 0)
151 bus_space_write_1(sc->sc_iot, sc->sc_dma1h,
152 DMA1_MODE, ochan | DMA37MD_CASCADE);
153 else
154 bus_space_write_1(sc->sc_iot, sc->sc_dma2h,
155 DMA2_MODE, ochan | DMA37MD_CASCADE);
156
157 isa_dmaunmask(sc, chan);
158 return;
159
160 lose:
161 panic("isa_dmacascade");
162 }
163
164 int
165 isa_dmamap_create(isadev, chan, size, flags)
166 struct device *isadev;
167 int chan;
168 bus_size_t size;
169 int flags;
170 {
171 struct isa_softc *sc = (struct isa_softc *)isadev;
172 bus_size_t maxsize;
173
174 if (chan < 0 || chan > 7) {
175 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
176 goto lose;
177 }
178
179 if (chan & 4)
180 maxsize = (1 << 17);
181 else
182 maxsize = (1 << 16);
183
184 if (size > maxsize)
185 return (EINVAL);
186
187 if (ISA_DRQ_ISFREE(sc, chan) == 0) {
188 printf("%s: drq %d is not free\n", sc->sc_dev.dv_xname, chan);
189 goto lose;
190 }
191
192 ISA_DRQ_ALLOC(sc, chan);
193
194 return (bus_dmamap_create(sc->sc_dmat, size, 1, size, maxsize,
195 flags, &sc->sc_dmamaps[chan]));
196
197 lose:
198 panic("isa_dmamap_create");
199 }
200
201 void
202 isa_dmamap_destroy(isadev, chan)
203 struct device *isadev;
204 int chan;
205 {
206 struct isa_softc *sc = (struct isa_softc *)isadev;
207
208 if (chan < 0 || chan > 7) {
209 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
210 goto lose;
211 }
212
213 if (ISA_DRQ_ISFREE(sc, chan)) {
214 printf("%s: drq %d is already free\n",
215 sc->sc_dev.dv_xname, chan);
216 goto lose;
217 }
218
219 ISA_DRQ_FREE(sc, chan);
220
221 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamaps[chan]);
222 return;
223
224 lose:
225 panic("isa_dmamap_destroy");
226 }
227
228 /*
229 * isa_dmastart(): program 8237 DMA controller channel and set it
230 * in motion.
231 */
232 int
233 isa_dmastart(isadev, chan, addr, nbytes, p, flags, busdmaflags)
234 struct device *isadev;
235 int chan;
236 void *addr;
237 bus_size_t nbytes;
238 struct proc *p;
239 int flags;
240 int busdmaflags;
241 {
242 struct isa_softc *sc = (struct isa_softc *)isadev;
243 bus_dmamap_t dmam;
244 bus_addr_t dmaaddr;
245 int waport;
246 int ochan = chan & 3;
247 int error;
248
249 if (chan < 0 || chan > 7) {
250 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
251 goto lose;
252 }
253
254 #ifdef ISADMA_DEBUG
255 printf("isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
256 "flags 0x%x, dmaflags 0x%x\n",
257 chan, addr, nbytes, p, flags, busdmaflags);
258 #endif
259
260 if (chan & 4) {
261 if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
262 printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
263 sc->sc_dev.dv_xname, chan, nbytes, addr);
264 goto lose;
265 }
266 } else {
267 if (nbytes > (1 << 16)) {
268 printf("%s: drq %d, nbytes 0x%lx\n",
269 sc->sc_dev.dv_xname, chan, nbytes);
270 goto lose;
271 }
272 }
273
274 dmam = sc->sc_dmamaps[chan];
275
276 error = bus_dmamap_load(sc->sc_dmat, dmam, addr, nbytes,
277 p, busdmaflags);
278 if (error)
279 return (error);
280
281 #ifdef ISADMA_DEBUG
282 __asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
283 #endif
284
285 if (flags & DMAMODE_READ) {
286 bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREREAD);
287 sc->sc_dmareads |= (1 << chan);
288 } else {
289 bus_dmamap_sync(sc->sc_dmat, dmam, BUS_DMASYNC_PREWRITE);
290 sc->sc_dmareads &= ~(1 << chan);
291 }
292
293 dmaaddr = dmam->dm_segs[0].ds_addr;
294
295 #ifdef ISADMA_DEBUG
296 printf(" dmaaddr 0x%lx\n", dmaaddr);
297
298 __asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
299 #endif
300
301 sc->sc_dmalength[chan] = nbytes;
302
303 isa_dmamask(sc, chan);
304 sc->sc_dmafinished &= ~(1 << chan);
305
306 if ((chan & 4) == 0) {
307 /* set dma channel mode */
308 bus_space_write_1(sc->sc_iot, sc->sc_dma1h, DMA1_MODE,
309 ochan | dmamode[flags]);
310
311 /* send start address */
312 waport = DMA1_CHN(ochan);
313 bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
314 dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
315 bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
316 dmaaddr & 0xff);
317 bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport,
318 (dmaaddr >> 8) & 0xff);
319
320 /* send count */
321 bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
322 (--nbytes) & 0xff);
323 bus_space_write_1(sc->sc_iot, sc->sc_dma1h, waport + 1,
324 (nbytes >> 8) & 0xff);
325 } else {
326 /* set dma channel mode */
327 bus_space_write_1(sc->sc_iot, sc->sc_dma2h, DMA2_MODE,
328 ochan | dmamode[flags]);
329
330 /* send start address */
331 waport = DMA2_CHN(ochan);
332 bus_space_write_1(sc->sc_iot, sc->sc_dmapgh,
333 dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
334 dmaaddr >>= 1;
335 bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
336 dmaaddr & 0xff);
337 bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport,
338 (dmaaddr >> 8) & 0xff);
339
340 /* send count */
341 nbytes >>= 1;
342 bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
343 (--nbytes) & 0xff);
344 bus_space_write_1(sc->sc_iot, sc->sc_dma2h, waport + 2,
345 (nbytes >> 8) & 0xff);
346 }
347
348 isa_dmaunmask(sc, chan);
349 return (0);
350
351 lose:
352 panic("isa_dmastart");
353 }
354
355 void
356 isa_dmaabort(isadev, chan)
357 struct device *isadev;
358 int chan;
359 {
360 struct isa_softc *sc = (struct isa_softc *)isadev;
361
362 if (chan < 0 || chan > 7) {
363 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
364 panic("isa_dmaabort");
365 }
366
367 isa_dmamask(sc, chan);
368 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamaps[chan]);
369 sc->sc_dmareads &= ~(1 << chan);
370 }
371
372 bus_size_t
373 isa_dmacount(isadev, chan)
374 struct device *isadev;
375 int chan;
376 {
377 struct isa_softc *sc = (struct isa_softc *)isadev;
378 int waport;
379 bus_size_t nbytes;
380 int ochan = chan & 3;
381
382 if (chan < 0 || chan > 7) {
383 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
384 panic("isa_dmacount");
385 }
386
387 isa_dmamask(sc, chan);
388
389 /*
390 * We have to shift the byte count by 1. If we're in auto-initialize
391 * mode, the count may have wrapped around to the initial value. We
392 * can't use the TC bit to check for this case, so instead we compare
393 * against the original byte count.
394 * If we're not in auto-initialize mode, then the count will wrap to
395 * -1, so we also handle that case.
396 */
397 if ((chan & 4) == 0) {
398 waport = DMA1_CHN(ochan);
399 nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
400 waport + 1) + 1;
401 nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma1h,
402 waport + 1) << 8;
403 nbytes &= 0xffff;
404 } else {
405 waport = DMA2_CHN(ochan);
406 nbytes = bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
407 waport + 2) + 1;
408 nbytes += bus_space_read_1(sc->sc_iot, sc->sc_dma2h,
409 waport + 2) << 8;
410 nbytes <<= 1;
411 nbytes &= 0x1ffff;
412 }
413
414 if (nbytes == sc->sc_dmalength[chan])
415 nbytes = 0;
416
417 isa_dmaunmask(sc, chan);
418 return (nbytes);
419 }
420
421 int
422 isa_dmafinished(isadev, chan)
423 struct device *isadev;
424 int chan;
425 {
426 struct isa_softc *sc = (struct isa_softc *)isadev;
427
428 if (chan < 0 || chan > 7) {
429 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
430 panic("isa_dmafinished");
431 }
432
433 /* check that the terminal count was reached */
434 if ((chan & 4) == 0)
435 sc->sc_dmafinished |= bus_space_read_1(sc->sc_iot,
436 sc->sc_dma1h, DMA1_SR) & 0x0f;
437 else
438 sc->sc_dmafinished |= (bus_space_read_1(sc->sc_iot,
439 sc->sc_dma2h, DMA2_SR) & 0x0f) << 4;
440
441 return ((sc->sc_dmafinished & (1 << chan)) != 0);
442 }
443
444 void
445 isa_dmadone(isadev, chan)
446 struct device *isadev;
447 int chan;
448 {
449 struct isa_softc *sc = (struct isa_softc *)isadev;
450 bus_dmamap_t dmam;
451
452 if (chan < 0 || chan > 7) {
453 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
454 panic("isa_dmadone");
455 }
456
457 dmam = sc->sc_dmamaps[chan];
458
459 isa_dmamask(sc, chan);
460
461 if (isa_dmafinished(isadev, chan) == 0)
462 printf("%s: isa_dmadone: channel %d not finished\n",
463 sc->sc_dev.dv_xname, chan);
464
465 bus_dmamap_sync(sc->sc_dmat, dmam,
466 (sc->sc_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
467 BUS_DMASYNC_POSTWRITE);
468
469 bus_dmamap_unload(sc->sc_dmat, dmam);
470 sc->sc_dmareads &= ~(1 << chan);
471 }
472
473 int
474 isa_dmamem_alloc(isadev, chan, size, addrp, flags)
475 struct device *isadev;
476 int chan;
477 bus_size_t size;
478 bus_addr_t *addrp;
479 int flags;
480 {
481 struct isa_softc *sc = (struct isa_softc *)isadev;
482 bus_dma_segment_t seg;
483 int error, boundary, rsegs;
484
485 if (chan < 0 || chan > 7) {
486 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
487 panic("isa_dmamem_alloc");
488 }
489
490 boundary = (chan & 4) ? (1 << 17) : (1 << 16);
491
492 size = round_page(size);
493
494 error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, boundary,
495 &seg, 1, &rsegs, flags);
496 if (error)
497 return (error);
498
499 *addrp = seg.ds_addr;
500 return (0);
501 }
502
503 void
504 isa_dmamem_free(isadev, chan, addr, size)
505 struct device *isadev;
506 int chan;
507 bus_addr_t addr;
508 bus_size_t size;
509 {
510 struct isa_softc *sc = (struct isa_softc *)isadev;
511 bus_dma_segment_t seg;
512
513 if (chan < 0 || chan > 7) {
514 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
515 panic("isa_dmamem_free");
516 }
517
518 seg.ds_addr = addr;
519 seg.ds_len = size;
520
521 bus_dmamem_free(sc->sc_dmat, &seg, 1);
522 }
523
524 int
525 isa_dmamem_map(isadev, chan, addr, size, kvap, flags)
526 struct device *isadev;
527 int chan;
528 bus_addr_t addr;
529 bus_size_t size;
530 caddr_t *kvap;
531 int flags;
532 {
533 struct isa_softc *sc = (struct isa_softc *)isadev;
534 bus_dma_segment_t seg;
535
536 if (chan < 0 || chan > 7) {
537 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
538 panic("isa_dmamem_map");
539 }
540
541 seg.ds_addr = addr;
542 seg.ds_len = size;
543
544 return (bus_dmamem_map(sc->sc_dmat, &seg, 1, size, kvap, flags));
545 }
546
547 void
548 isa_dmamem_unmap(isadev, chan, kva, size)
549 struct device *isadev;
550 int chan;
551 caddr_t kva;
552 size_t size;
553 {
554 struct isa_softc *sc = (struct isa_softc *)isadev;
555
556 if (chan < 0 || chan > 7) {
557 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
558 panic("isa_dmamem_unmap");
559 }
560
561 bus_dmamem_unmap(sc->sc_dmat, kva, size);
562 }
563
564 int
565 isa_dmamem_mmap(isadev, chan, addr, size, off, prot, flags)
566 struct device *isadev;
567 int chan;
568 bus_addr_t addr;
569 bus_size_t size;
570 int off, prot, flags;
571 {
572 struct isa_softc *sc = (struct isa_softc *)isadev;
573 bus_dma_segment_t seg;
574
575 if (chan < 0 || chan > 7) {
576 printf("%s: bogus drq %d\n", sc->sc_dev.dv_xname, chan);
577 panic("isa_dmamem_mmap");
578 }
579
580 seg.ds_addr = addr;
581 seg.ds_len = size;
582
583 return (bus_dmamem_mmap(sc->sc_dmat, &seg, 1, off, prot, flags));
584 }
585
586 void *
587 isa_malloc(isadev, chan, size, pool, flags)
588 struct device *isadev;
589 int chan;
590 size_t size;
591 int pool;
592 int flags;
593 {
594 bus_addr_t addr;
595 caddr_t kva;
596 int bflags;
597 struct isa_mem *m;
598
599 bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
600
601 if (isa_dmamem_alloc(isadev, chan, size, &addr, bflags))
602 return 0;
603 if (isa_dmamem_map(isadev, chan, addr, size, &kva, bflags)) {
604 isa_dmamem_free(isadev, chan, addr, size);
605 return 0;
606 }
607 m = malloc(sizeof(*m), pool, flags);
608 if (m == 0) {
609 isa_dmamem_unmap(isadev, chan, kva, size);
610 isa_dmamem_free(isadev, chan, addr, size);
611 return 0;
612 }
613 m->isadev = isadev;
614 m->chan = chan;
615 m->size = size;
616 m->addr = addr;
617 m->kva = kva;
618 m->next = isa_mem_head;
619 isa_mem_head = m;
620 return (void *)kva;
621 }
622
623 void
624 isa_free(addr, pool)
625 void *addr;
626 int pool;
627 {
628 struct isa_mem **mp, *m;
629 caddr_t kva = (caddr_t)addr;
630
631 for(mp = &isa_mem_head; *mp && (*mp)->kva != kva; mp = &(*mp)->next)
632 ;
633 m = *mp;
634 if (!m) {
635 printf("isa_free: freeing unallocted memory\n");
636 return;
637 }
638 *mp = m->next;
639 isa_dmamem_unmap(m->isadev, m->chan, kva, m->size);
640 isa_dmamem_free(m->isadev, m->chan, m->addr, m->size);
641 free(m, pool);
642 }
643