1 /*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/sys/amd64/isa/isa_dma.c,v 1.22 2005/05/14 10:14:56 nyan Exp $");
37
38 /*
39 * code to manage AT bus
40 *
41 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com):
42 * Fixed uninitialized variable problem and added code to deal
43 * with DMA page boundaries in isa_dmarangecheck(). Fixed word
44 * mode DMA count compution and reorganized DMA setup code in
45 * isa_dmastart()
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/bus.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/lock.h>
54 #include <sys/proc.h>
55 #include <sys/mutex.h>
56 #include <sys/module.h>
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <isa/isareg.h>
61 #include <isa/isavar.h>
62 #include <isa/isa_dmareg.h>
63
64 #define ISARAM_END 0x1000000
65
66 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
67
68 static caddr_t dma_bouncebuf[8];
69 static u_int dma_bouncebufsize[8];
70 static u_int8_t dma_bounced = 0;
71 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
72 static u_int8_t dma_inuse = 0; /* User for acquire/release */
73 static u_int8_t dma_auto_mode = 0;
74
75 #define VALID_DMA_MASK (7)
76
77 /* high byte of address is stored in this port for i-th dma channel */
78 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
79
80 /*
81 * Setup a DMA channel's bounce buffer.
82 */
83 int
84 isa_dma_init(int chan, u_int bouncebufsize, int flag)
85 {
86 void *buf;
87
88 /*
89 * If a DMA channel is shared, both drivers have to call isa_dma_init
90 * since they don't know that the other driver will do it.
91 * Just return if we're already set up good.
92 * XXX: this only works if they agree on the bouncebuf size. This
93 * XXX: is typically the case since they are multiple instances of
94 * XXX: the same driver.
95 */
96 if (dma_bouncebuf[chan] != NULL)
97 return (0);
98
99 #ifdef DIAGNOSTIC
100 if (chan & ~VALID_DMA_MASK)
101 panic("isa_dma_init: channel out of range");
102 #endif
103
104 dma_bouncebufsize[chan] = bouncebufsize;
105
106 /* Try malloc() first. It works better if it works. */
107 buf = malloc(bouncebufsize, M_DEVBUF, flag);
108 if (buf != NULL) {
109 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
110 dma_bouncebuf[chan] = buf;
111 return (0);
112 }
113 free(buf, M_DEVBUF);
114 }
115 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
116 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
117 if (buf == NULL)
118 return (ENOMEM);
119 dma_bouncebuf[chan] = buf;
120 return (0);
121 }
122
123 /*
124 * Register a DMA channel's usage. Usually called from a device driver
125 * in open() or during its initialization.
126 */
127 int
128 isa_dma_acquire(chan)
129 int chan;
130 {
131 #ifdef DIAGNOSTIC
132 if (chan & ~VALID_DMA_MASK)
133 panic("isa_dma_acquire: channel out of range");
134 #endif
135
136 if (dma_inuse & (1 << chan)) {
137 printf("isa_dma_acquire: channel %d already in use\n", chan);
138 return (EBUSY);
139 }
140 dma_inuse |= (1 << chan);
141 dma_auto_mode &= ~(1 << chan);
142
143 return (0);
144 }
145
146 /*
147 * Unregister a DMA channel's usage. Usually called from a device driver
148 * during close() or during its shutdown.
149 */
150 void
151 isa_dma_release(chan)
152 int chan;
153 {
154 #ifdef DIAGNOSTIC
155 if (chan & ~VALID_DMA_MASK)
156 panic("isa_dma_release: channel out of range");
157
158 if ((dma_inuse & (1 << chan)) == 0)
159 printf("isa_dma_release: channel %d not in use\n", chan);
160 #endif
161
162 if (dma_busy & (1 << chan)) {
163 dma_busy &= ~(1 << chan);
164 /*
165 * XXX We should also do "dma_bounced &= (1 << chan);"
166 * because we are acting on behalf of isa_dmadone() which
167 * was not called to end the last DMA operation. This does
168 * not matter now, but it may in the future.
169 */
170 }
171
172 dma_inuse &= ~(1 << chan);
173 dma_auto_mode &= ~(1 << chan);
174 }
175
176 /*
177 * isa_dmacascade(): program 8237 DMA controller channel to accept
178 * external dma control by a board.
179 */
180 void
181 isa_dmacascade(chan)
182 int chan;
183 {
184 #ifdef DIAGNOSTIC
185 if (chan & ~VALID_DMA_MASK)
186 panic("isa_dmacascade: channel out of range");
187 #endif
188
189 /* set dma channel mode, and set dma channel mode */
190 if ((chan & 4) == 0) {
191 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
192 outb(DMA1_SMSK, chan);
193 } else {
194 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
195 outb(DMA2_SMSK, chan & 3);
196 }
197 }
198
199 /*
200 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
201 * problems by using a bounce buffer.
202 */
203 void
204 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
205 {
206 vm_paddr_t phys;
207 int waport;
208 caddr_t newaddr;
209
210 GIANT_REQUIRED;
211
212 #ifdef DIAGNOSTIC
213 if (chan & ~VALID_DMA_MASK)
214 panic("isa_dmastart: channel out of range");
215
216 if ((chan < 4 && nbytes > (1<<16))
217 || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
218 panic("isa_dmastart: impossible request");
219
220 if ((dma_inuse & (1 << chan)) == 0)
221 printf("isa_dmastart: channel %d not acquired\n", chan);
222 #endif
223
224 #if 0
225 /*
226 * XXX This should be checked, but drivers like ad1848 only call
227 * isa_dmastart() once because they use Auto DMA mode. If we
228 * leave this in, drivers that do this will print this continuously.
229 */
230 if (dma_busy & (1 << chan))
231 printf("isa_dmastart: channel %d busy\n", chan);
232 #endif
233
234 dma_busy |= (1 << chan);
235
236 if (isa_dmarangecheck(addr, nbytes, chan)) {
237 if (dma_bouncebuf[chan] == NULL
238 || dma_bouncebufsize[chan] < nbytes)
239 panic("isa_dmastart: bad bounce buffer");
240 dma_bounced |= (1 << chan);
241 newaddr = dma_bouncebuf[chan];
242
243 /* copy bounce buffer on write */
244 if (!(flags & ISADMA_READ))
245 bcopy(addr, newaddr, nbytes);
246 addr = newaddr;
247 }
248
249 /* translate to physical */
250 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
251
252 if (flags & ISADMA_RAW) {
253 dma_auto_mode |= (1 << chan);
254 } else {
255 dma_auto_mode &= ~(1 << chan);
256 }
257
258 if ((chan & 4) == 0) {
259 /*
260 * Program one of DMA channels 0..3. These are
261 * byte mode channels.
262 */
263 /* set dma channel mode, and reset address ff */
264
265 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
266 if (flags & ISADMA_RAW) {
267 if (flags & ISADMA_READ)
268 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
269 else
270 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
271 }
272 else
273 if (flags & ISADMA_READ)
274 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
275 else
276 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
277 outb(DMA1_FFC, 0);
278
279 /* send start address */
280 waport = DMA1_CHN(chan);
281 outb(waport, phys);
282 outb(waport, phys>>8);
283 outb(dmapageport[chan], phys>>16);
284
285 /* send count */
286 outb(waport + 1, --nbytes);
287 outb(waport + 1, nbytes>>8);
288
289 /* unmask channel */
290 outb(DMA1_SMSK, chan);
291 } else {
292 /*
293 * Program one of DMA channels 4..7. These are
294 * word mode channels.
295 */
296 /* set dma channel mode, and reset address ff */
297
298 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
299 if (flags & ISADMA_RAW) {
300 if (flags & ISADMA_READ)
301 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
302 else
303 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
304 }
305 else
306 if (flags & ISADMA_READ)
307 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
308 else
309 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
310 outb(DMA2_FFC, 0);
311
312 /* send start address */
313 waport = DMA2_CHN(chan - 4);
314 outb(waport, phys>>1);
315 outb(waport, phys>>9);
316 outb(dmapageport[chan], phys>>16);
317
318 /* send count */
319 nbytes >>= 1;
320 outb(waport + 2, --nbytes);
321 outb(waport + 2, nbytes>>8);
322
323 /* unmask channel */
324 outb(DMA2_SMSK, chan & 3);
325 }
326 }
327
328 void
329 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
330 {
331 #ifdef DIAGNOSTIC
332 if (chan & ~VALID_DMA_MASK)
333 panic("isa_dmadone: channel out of range");
334
335 if ((dma_inuse & (1 << chan)) == 0)
336 printf("isa_dmadone: channel %d not acquired\n", chan);
337 #endif
338
339 if (((dma_busy & (1 << chan)) == 0) &&
340 (dma_auto_mode & (1 << chan)) == 0 )
341 printf("isa_dmadone: channel %d not busy\n", chan);
342
343 if ((dma_auto_mode & (1 << chan)) == 0)
344 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
345
346 if (dma_bounced & (1 << chan)) {
347 /* copy bounce buffer on read */
348 if (flags & ISADMA_READ)
349 bcopy(dma_bouncebuf[chan], addr, nbytes);
350
351 dma_bounced &= ~(1 << chan);
352 }
353 dma_busy &= ~(1 << chan);
354 }
355
356 /*
357 * Check for problems with the address range of a DMA transfer
358 * (non-contiguous physical pages, outside of bus address space,
359 * crossing DMA page boundaries).
360 * Return true if special handling needed.
361 */
362
363 static int
364 isa_dmarangecheck(caddr_t va, u_int length, int chan)
365 {
366 vm_paddr_t phys, priorpage = 0;
367 vm_offset_t endva;
368 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
369
370 GIANT_REQUIRED;
371
372 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
373 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
374 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
375 if (phys == 0)
376 panic("isa_dmacheck: no physical page present");
377 if (phys >= ISARAM_END)
378 return (1);
379 if (priorpage) {
380 if (priorpage + PAGE_SIZE != phys)
381 return (1);
382 /* check if crossing a DMA page boundary */
383 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
384 return (1);
385 }
386 priorpage = phys;
387 }
388 return (0);
389 }
390
391 /*
392 * Query the progress of a transfer on a DMA channel.
393 *
394 * To avoid having to interrupt a transfer in progress, we sample
395 * each of the high and low databytes twice, and apply the following
396 * logic to determine the correct count.
397 *
398 * Reads are performed with interrupts disabled, thus it is to be
399 * expected that the time between reads is very small. At most
400 * one rollover in the low count byte can be expected within the
401 * four reads that are performed.
402 *
403 * There are three gaps in which a rollover can occur :
404 *
405 * - read low1
406 * gap1
407 * - read high1
408 * gap2
409 * - read low2
410 * gap3
411 * - read high2
412 *
413 * If a rollover occurs in gap1 or gap2, the low2 value will be
414 * greater than the low1 value. In this case, low2 and high2 are a
415 * corresponding pair.
416 *
417 * In any other case, low1 and high1 can be considered to be correct.
418 *
419 * The function returns the number of bytes remaining in the transfer,
420 * or -1 if the channel requested is not active.
421 *
422 */
423 int
424 isa_dmastatus(int chan)
425 {
426 u_long cnt = 0;
427 int ffport, waport;
428 u_long low1, high1, low2, high2;
429
430 /* channel active? */
431 if ((dma_inuse & (1 << chan)) == 0) {
432 printf("isa_dmastatus: channel %d not active\n", chan);
433 return(-1);
434 }
435 /* channel busy? */
436
437 if (((dma_busy & (1 << chan)) == 0) &&
438 (dma_auto_mode & (1 << chan)) == 0 ) {
439 printf("chan %d not busy\n", chan);
440 return -2 ;
441 }
442 if (chan < 4) { /* low DMA controller */
443 ffport = DMA1_FFC;
444 waport = DMA1_CHN(chan) + 1;
445 } else { /* high DMA controller */
446 ffport = DMA2_FFC;
447 waport = DMA2_CHN(chan - 4) + 2;
448 }
449
450 disable_intr(); /* no interrupts Mr Jones! */
451 outb(ffport, 0); /* clear register LSB flipflop */
452 low1 = inb(waport);
453 high1 = inb(waport);
454 outb(ffport, 0); /* clear again */
455 low2 = inb(waport);
456 high2 = inb(waport);
457 enable_intr(); /* enable interrupts again */
458
459 /*
460 * Now decide if a wrap has tried to skew our results.
461 * Note that after TC, the count will read 0xffff, while we want
462 * to return zero, so we add and then mask to compensate.
463 */
464 if (low1 >= low2) {
465 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
466 } else {
467 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
468 }
469
470 if (chan >= 4) /* high channels move words */
471 cnt *= 2;
472 return(cnt);
473 }
474
475 /*
476 * Reached terminal count yet ?
477 */
478 int
479 isa_dmatc(int chan)
480 {
481
482 if (chan < 4)
483 return(inb(DMA1_STATUS) & (1 << chan));
484 else
485 return(inb(DMA2_STATUS) & (1 << (chan & 3)));
486 }
487
488 /*
489 * Stop a DMA transfer currently in progress.
490 */
491 int
492 isa_dmastop(int chan)
493 {
494 if ((dma_inuse & (1 << chan)) == 0)
495 printf("isa_dmastop: channel %d not acquired\n", chan);
496
497 if (((dma_busy & (1 << chan)) == 0) &&
498 ((dma_auto_mode & (1 << chan)) == 0)) {
499 printf("chan %d not busy\n", chan);
500 return -2 ;
501 }
502
503 if ((chan & 4) == 0) {
504 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
505 } else {
506 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
507 }
508 return(isa_dmastatus(chan));
509 }
510
511 /*
512 * Attach to the ISA PnP descriptor for the AT DMA controller
513 */
514 static struct isa_pnp_id atdma_ids[] = {
515 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
516 { 0 }
517 };
518
519 static int
520 atdma_probe(device_t dev)
521 {
522 int result;
523
524 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
525 device_quiet(dev);
526 return(result);
527 }
528
529 static int
530 atdma_attach(device_t dev)
531 {
532 return(0);
533 }
534
535 static device_method_t atdma_methods[] = {
536 /* Device interface */
537 DEVMETHOD(device_probe, atdma_probe),
538 DEVMETHOD(device_attach, atdma_attach),
539 DEVMETHOD(device_detach, bus_generic_detach),
540 DEVMETHOD(device_shutdown, bus_generic_shutdown),
541 DEVMETHOD(device_suspend, bus_generic_suspend),
542 DEVMETHOD(device_resume, bus_generic_resume),
543 { 0, 0 }
544 };
545
546 static driver_t atdma_driver = {
547 "atdma",
548 atdma_methods,
549 1, /* no softc */
550 };
551
552 static devclass_t atdma_devclass;
553
554 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
555 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
556 Cache object: dae862ede29fef773095928285e28145
|