2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * code to manage AT bus
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
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/module.h>
58 #include <vm/vm_param.h>
60 #include <isa/isareg.h>
61 #include <isa/isavar.h>
62 #include <isa/isa_dmareg.h>
64 #define ISARAM_END 0x1000000
66 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
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;
75 #define VALID_DMA_MASK (7)
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 };
81 * Setup a DMA channel's bounce buffer.
84 isa_dma_init(int chan, u_int bouncebufsize, int flag)
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.
96 if (dma_bouncebuf[chan] != NULL)
100 if (chan & ~VALID_DMA_MASK)
101 panic("isa_dma_init: channel out of range");
104 dma_bouncebufsize[chan] = bouncebufsize;
106 /* Try malloc() first. It works better if it works. */
107 buf = malloc(bouncebufsize, M_DEVBUF, flag);
109 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
110 dma_bouncebuf[chan] = buf;
115 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
116 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
119 dma_bouncebuf[chan] = buf;
124 * Register a DMA channel's usage. Usually called from a device driver
125 * in open() or during its initialization.
128 isa_dma_acquire(chan)
132 if (chan & ~VALID_DMA_MASK)
133 panic("isa_dma_acquire: channel out of range");
136 if (dma_inuse & (1 << chan)) {
137 printf("isa_dma_acquire: channel %d already in use\n", chan);
140 dma_inuse |= (1 << chan);
141 dma_auto_mode &= ~(1 << chan);
147 * Unregister a DMA channel's usage. Usually called from a device driver
148 * during close() or during its shutdown.
151 isa_dma_release(chan)
155 if (chan & ~VALID_DMA_MASK)
156 panic("isa_dma_release: channel out of range");
158 if ((dma_inuse & (1 << chan)) == 0)
159 printf("isa_dma_release: channel %d not in use\n", chan);
162 if (dma_busy & (1 << chan)) {
163 dma_busy &= ~(1 << chan);
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.
172 dma_inuse &= ~(1 << chan);
173 dma_auto_mode &= ~(1 << chan);
177 * isa_dmacascade(): program 8237 DMA controller channel to accept
178 * external dma control by a board.
185 if (chan & ~VALID_DMA_MASK)
186 panic("isa_dmacascade: channel out of range");
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);
194 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
195 outb(DMA2_SMSK, chan & 3);
200 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
201 * problems by using a bounce buffer.
204 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
213 if (chan & ~VALID_DMA_MASK)
214 panic("isa_dmastart: channel out of range");
216 if ((chan < 4 && nbytes > (1<<16))
217 || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
218 panic("isa_dmastart: impossible request");
220 if ((dma_inuse & (1 << chan)) == 0)
221 printf("isa_dmastart: channel %d not acquired\n", chan);
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.
230 if (dma_busy & (1 << chan))
231 printf("isa_dmastart: channel %d busy\n", chan);
234 dma_busy |= (1 << chan);
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];
243 /* copy bounce buffer on write */
244 if (!(flags & ISADMA_READ))
245 bcopy(addr, newaddr, nbytes);
249 /* translate to physical */
250 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
252 if (flags & ISADMA_RAW) {
253 dma_auto_mode |= (1 << chan);
255 dma_auto_mode &= ~(1 << chan);
258 if ((chan & 4) == 0) {
260 * Program one of DMA channels 0..3. These are
261 * byte mode channels.
263 /* set dma channel mode, and reset address ff */
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);
270 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
273 if (flags & ISADMA_READ)
274 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
276 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
279 /* send start address */
280 waport = DMA1_CHN(chan);
282 outb(waport, phys>>8);
283 outb(dmapageport[chan], phys>>16);
286 outb(waport + 1, --nbytes);
287 outb(waport + 1, nbytes>>8);
290 outb(DMA1_SMSK, chan);
293 * Program one of DMA channels 4..7. These are
294 * word mode channels.
296 /* set dma channel mode, and reset address ff */
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));
303 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
306 if (flags & ISADMA_READ)
307 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
309 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
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);
320 outb(waport + 2, --nbytes);
321 outb(waport + 2, nbytes>>8);
324 outb(DMA2_SMSK, chan & 3);
329 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
332 if (chan & ~VALID_DMA_MASK)
333 panic("isa_dmadone: channel out of range");
335 if ((dma_inuse & (1 << chan)) == 0)
336 printf("isa_dmadone: channel %d not acquired\n", chan);
339 if (((dma_busy & (1 << chan)) == 0) &&
340 (dma_auto_mode & (1 << chan)) == 0 )
341 printf("isa_dmadone: channel %d not busy\n", chan);
343 if ((dma_auto_mode & (1 << chan)) == 0)
344 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
346 if (dma_bounced & (1 << chan)) {
347 /* copy bounce buffer on read */
348 if (flags & ISADMA_READ)
349 bcopy(dma_bouncebuf[chan], addr, nbytes);
351 dma_bounced &= ~(1 << chan);
353 dma_busy &= ~(1 << chan);
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.
364 isa_dmarangecheck(caddr_t va, u_int length, int chan)
366 vm_paddr_t phys, priorpage = 0;
368 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
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));
376 panic("isa_dmacheck: no physical page present");
377 if (phys >= ISARAM_END)
380 if (priorpage + PAGE_SIZE != phys)
382 /* check if crossing a DMA page boundary */
383 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
392 * Query the progress of a transfer on a DMA channel.
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.
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.
403 * There are three gaps in which a rollover can occur :
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.
417 * In any other case, low1 and high1 can be considered to be correct.
419 * The function returns the number of bytes remaining in the transfer,
420 * or -1 if the channel requested is not active.
424 isa_dmastatus(int chan)
428 u_long low1, high1, low2, high2;
430 /* channel active? */
431 if ((dma_inuse & (1 << chan)) == 0) {
432 printf("isa_dmastatus: channel %d not active\n", chan);
437 if (((dma_busy & (1 << chan)) == 0) &&
438 (dma_auto_mode & (1 << chan)) == 0 ) {
439 printf("chan %d not busy\n", chan);
442 if (chan < 4) { /* low DMA controller */
444 waport = DMA1_CHN(chan) + 1;
445 } else { /* high DMA controller */
447 waport = DMA2_CHN(chan - 4) + 2;
450 disable_intr(); /* no interrupts Mr Jones! */
451 outb(ffport, 0); /* clear register LSB flipflop */
454 outb(ffport, 0); /* clear again */
457 enable_intr(); /* enable interrupts again */
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.
465 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
467 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
470 if (chan >= 4) /* high channels move words */
476 * Reached terminal count yet ?
483 return(inb(DMA1_STATUS) & (1 << chan));
485 return(inb(DMA2_STATUS) & (1 << (chan & 3)));
489 * Stop a DMA transfer currently in progress.
492 isa_dmastop(int chan)
494 if ((dma_inuse & (1 << chan)) == 0)
495 printf("isa_dmastop: channel %d not acquired\n", chan);
497 if (((dma_busy & (1 << chan)) == 0) &&
498 ((dma_auto_mode & (1 << chan)) == 0)) {
499 printf("chan %d not busy\n", chan);
503 if ((chan & 4) == 0) {
504 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
506 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
508 return(isa_dmastatus(chan));
512 * Attach to the ISA PnP descriptor for the AT DMA controller
514 static struct isa_pnp_id atdma_ids[] = {
515 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
520 atdma_probe(device_t dev)
524 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
530 atdma_attach(device_t dev)
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),
546 static driver_t atdma_driver = {
552 static devclass_t atdma_devclass;
554 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
555 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);