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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
41 * code to manage AT bus
43 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com):
44 * Fixed uninitialized variable problem and added code to deal
45 * with DMA page boundaries in isa_dmarangecheck(). Fixed word
46 * mode DMA count compution and reorganized DMA setup code in
50 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/module.h>
59 #include <vm/vm_param.h>
61 #include <i386/isa/isa.h>
62 #include <i386/isa/ic/i8237.h>
63 #include <isa/isavar.h>
66 ** Register definitions for DMA controller 1 (channels 0..3):
68 #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
69 #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
70 #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
71 #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
74 ** Register definitions for DMA controller 2 (channels 4..7):
76 #define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */
77 #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
78 #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
79 #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
81 static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
83 static caddr_t dma_bouncebuf[8];
84 static u_int dma_bouncebufsize[8];
85 static u_int8_t dma_bounced = 0;
86 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
87 static u_int8_t dma_inuse = 0; /* User for acquire/release */
88 static u_int8_t dma_auto_mode = 0;
90 #define VALID_DMA_MASK (7)
92 /* high byte of address is stored in this port for i-th dma channel */
93 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
96 * Setup a DMA channel's bounce buffer.
99 isa_dmainit(chan, bouncebufsize)
106 if (chan & ~VALID_DMA_MASK)
107 panic("isa_dmainit: channel out of range");
109 if (dma_bouncebuf[chan] != NULL)
110 panic("isa_dmainit: impossible request");
113 dma_bouncebufsize[chan] = bouncebufsize;
115 /* Try malloc() first. It works better if it works. */
116 buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
118 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
119 dma_bouncebuf[chan] = buf;
124 buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
125 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
127 printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
129 dma_bouncebuf[chan] = buf;
133 * Register a DMA channel's usage. Usually called from a device driver
134 * in open() or during its initialization.
137 isa_dma_acquire(chan)
141 if (chan & ~VALID_DMA_MASK)
142 panic("isa_dma_acquire: channel out of range");
145 if (dma_inuse & (1 << chan)) {
146 printf("isa_dma_acquire: channel %d already in use\n", chan);
149 dma_inuse |= (1 << chan);
150 dma_auto_mode &= ~(1 << chan);
156 * Unregister a DMA channel's usage. Usually called from a device driver
157 * during close() or during its shutdown.
160 isa_dma_release(chan)
164 if (chan & ~VALID_DMA_MASK)
165 panic("isa_dma_release: channel out of range");
167 if ((dma_inuse & (1 << chan)) == 0)
168 printf("isa_dma_release: channel %d not in use\n", chan);
171 if (dma_busy & (1 << chan)) {
172 dma_busy &= ~(1 << chan);
174 * XXX We should also do "dma_bounced &= (1 << chan);"
175 * because we are acting on behalf of isa_dmadone() which
176 * was not called to end the last DMA operation. This does
177 * not matter now, but it may in the future.
181 dma_inuse &= ~(1 << chan);
182 dma_auto_mode &= ~(1 << chan);
186 * isa_dmacascade(): program 8237 DMA controller channel to accept
187 * external dma control by a board.
194 if (chan & ~VALID_DMA_MASK)
195 panic("isa_dmacascade: channel out of range");
198 /* set dma channel mode, and set dma channel mode */
199 if ((chan & 4) == 0) {
200 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
201 outb(DMA1_SMSK, chan);
203 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
204 outb(DMA2_SMSK, chan & 3);
209 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
210 * problems by using a bounce buffer.
213 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
220 if (chan & ~VALID_DMA_MASK)
221 panic("isa_dmastart: channel out of range");
223 if ((chan < 4 && nbytes > (1<<16))
224 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
225 panic("isa_dmastart: impossible request");
227 if ((dma_inuse & (1 << chan)) == 0)
228 printf("isa_dmastart: channel %d not acquired\n", chan);
233 * XXX This should be checked, but drivers like ad1848 only call
234 * isa_dmastart() once because they use Auto DMA mode. If we
235 * leave this in, drivers that do this will print this continuously.
237 if (dma_busy & (1 << chan))
238 printf("isa_dmastart: channel %d busy\n", chan);
241 dma_busy |= (1 << chan);
243 if (isa_dmarangecheck(addr, nbytes, chan)) {
244 if (dma_bouncebuf[chan] == NULL
245 || dma_bouncebufsize[chan] < nbytes)
246 panic("isa_dmastart: bad bounce buffer");
247 dma_bounced |= (1 << chan);
248 newaddr = dma_bouncebuf[chan];
250 /* copy bounce buffer on write */
251 if (!(flags & ISADMA_READ))
252 bcopy(addr, newaddr, nbytes);
256 /* translate to physical */
257 mtx_lock(&vm_mtx); /*
258 * XXX: need to hold for longer period to
259 * ensure that mappings don't change
261 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
264 if (flags & ISADMA_RAW) {
265 dma_auto_mode |= (1 << chan);
267 dma_auto_mode &= ~(1 << chan);
270 if ((chan & 4) == 0) {
272 * Program one of DMA channels 0..3. These are
273 * byte mode channels.
275 /* set dma channel mode, and reset address ff */
277 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
278 if (flags & ISADMA_RAW) {
279 if (flags & ISADMA_READ)
280 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
282 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
285 if (flags & ISADMA_READ)
286 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
288 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
291 /* send start address */
292 waport = DMA1_CHN(chan);
294 outb(waport, phys>>8);
295 outb(dmapageport[chan], phys>>16);
298 outb(waport + 1, --nbytes);
299 outb(waport + 1, nbytes>>8);
302 outb(DMA1_SMSK, chan);
305 * Program one of DMA channels 4..7. These are
306 * word mode channels.
308 /* set dma channel mode, and reset address ff */
310 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
311 if (flags & ISADMA_RAW) {
312 if (flags & ISADMA_READ)
313 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
315 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
318 if (flags & ISADMA_READ)
319 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
321 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
324 /* send start address */
325 waport = DMA2_CHN(chan - 4);
326 outb(waport, phys>>1);
327 outb(waport, phys>>9);
328 outb(dmapageport[chan], phys>>16);
332 outb(waport + 2, --nbytes);
333 outb(waport + 2, nbytes>>8);
336 outb(DMA2_SMSK, chan & 3);
341 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
344 if (chan & ~VALID_DMA_MASK)
345 panic("isa_dmadone: channel out of range");
347 if ((dma_inuse & (1 << chan)) == 0)
348 printf("isa_dmadone: channel %d not acquired\n", chan);
351 if (((dma_busy & (1 << chan)) == 0) &&
352 (dma_auto_mode & (1 << chan)) == 0 )
353 printf("isa_dmadone: channel %d not busy\n", chan);
355 if ((dma_auto_mode & (1 << chan)) == 0)
356 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
358 if (dma_bounced & (1 << chan)) {
359 /* copy bounce buffer on read */
360 if (flags & ISADMA_READ)
361 bcopy(dma_bouncebuf[chan], addr, nbytes);
363 dma_bounced &= ~(1 << chan);
365 dma_busy &= ~(1 << chan);
369 * Check for problems with the address range of a DMA transfer
370 * (non-contiguous physical pages, outside of bus address space,
371 * crossing DMA page boundaries).
372 * Return true if special handling needed.
376 isa_dmarangecheck(caddr_t va, u_int length, int chan)
378 vm_offset_t phys, priorpage = 0, endva;
379 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
381 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
382 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
384 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
386 #define ISARAM_END RAM_END
388 panic("isa_dmacheck: no physical page present");
389 if (phys >= ISARAM_END)
392 if (priorpage + PAGE_SIZE != phys)
394 /* check if crossing a DMA page boundary */
395 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
404 * Query the progress of a transfer on a DMA channel.
406 * To avoid having to interrupt a transfer in progress, we sample
407 * each of the high and low databytes twice, and apply the following
408 * logic to determine the correct count.
410 * Reads are performed with interrupts disabled, thus it is to be
411 * expected that the time between reads is very small. At most
412 * one rollover in the low count byte can be expected within the
413 * four reads that are performed.
415 * There are three gaps in which a rollover can occur :
425 * If a rollover occurs in gap1 or gap2, the low2 value will be
426 * greater than the low1 value. In this case, low2 and high2 are a
427 * corresponding pair.
429 * In any other case, low1 and high1 can be considered to be correct.
431 * The function returns the number of bytes remaining in the transfer,
432 * or -1 if the channel requested is not active.
436 isa_dmastatus(int chan)
440 u_long low1, high1, low2, high2;
442 /* channel active? */
443 if ((dma_inuse & (1 << chan)) == 0) {
444 printf("isa_dmastatus: channel %d not active\n", chan);
449 if (((dma_busy & (1 << chan)) == 0) &&
450 (dma_auto_mode & (1 << chan)) == 0 ) {
451 printf("chan %d not busy\n", chan);
454 if (chan < 4) { /* low DMA controller */
456 waport = DMA1_CHN(chan) + 1;
457 } else { /* high DMA controller */
459 waport = DMA2_CHN(chan - 4) + 2;
462 disable_intr(); /* no interrupts Mr Jones! */
463 outb(ffport, 0); /* clear register LSB flipflop */
466 outb(ffport, 0); /* clear again */
469 enable_intr(); /* enable interrupts again */
472 * Now decide if a wrap has tried to skew our results.
473 * Note that after TC, the count will read 0xffff, while we want
474 * to return zero, so we add and then mask to compensate.
477 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
479 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
482 if (chan >= 4) /* high channels move words */
488 * Stop a DMA transfer currently in progress.
491 isa_dmastop(int chan)
493 if ((dma_inuse & (1 << chan)) == 0)
494 printf("isa_dmastop: channel %d not acquired\n", chan);
496 if (((dma_busy & (1 << chan)) == 0) &&
497 ((dma_auto_mode & (1 << chan)) == 0)) {
498 printf("chan %d not busy\n", chan);
502 if ((chan & 4) == 0) {
503 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
505 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
507 return(isa_dmastatus(chan));
511 * Attach to the ISA PnP descriptor for the AT DMA controller
513 static struct isa_pnp_id atdma_ids[] = {
514 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
519 atdma_probe(device_t dev)
523 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
529 atdma_attach(device_t dev)
534 static device_method_t atdma_methods[] = {
535 /* Device interface */
536 DEVMETHOD(device_probe, atdma_probe),
537 DEVMETHOD(device_attach, atdma_attach),
538 DEVMETHOD(device_detach, bus_generic_detach),
539 DEVMETHOD(device_shutdown, bus_generic_shutdown),
540 DEVMETHOD(device_suspend, bus_generic_suspend),
541 DEVMETHOD(device_resume, bus_generic_resume),
545 static driver_t atdma_driver = {
551 static devclass_t atdma_devclass;
553 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);