2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
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>
57 #include <sys/mutex.h>
58 #include <sys/module.h>
60 #include <vm/vm_param.h>
62 #include <isa/isareg.h>
63 #include <isa/isavar.h>
64 #include <isa/isa_dmareg.h>
66 #define ISARAM_END 0x1000000
68 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
70 static caddr_t dma_bouncebuf[8];
71 static u_int dma_bouncebufsize[8];
72 static u_int8_t dma_bounced = 0;
73 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
74 static u_int8_t dma_inuse = 0; /* User for acquire/release */
75 static u_int8_t dma_auto_mode = 0;
76 static struct mtx isa_dma_lock;
77 MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
79 #define VALID_DMA_MASK (7)
81 /* high byte of address is stored in this port for i-th dma channel */
82 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
85 * Setup a DMA channel's bounce buffer.
88 isa_dma_init(int chan, u_int bouncebufsize, int flag)
94 if (chan & ~VALID_DMA_MASK)
95 panic("isa_dma_init: channel out of range");
98 /* Try malloc() first. It works better if it works. */
99 buf = malloc(bouncebufsize, M_DEVBUF, flag);
101 if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
109 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
110 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
117 mtx_lock(&isa_dma_lock);
119 * If a DMA channel is shared, both drivers have to call isa_dma_init
120 * since they don't know that the other driver will do it.
121 * Just return if we're already set up good.
122 * XXX: this only works if they agree on the bouncebuf size. This
123 * XXX: is typically the case since they are multiple instances of
124 * XXX: the same driver.
126 if (dma_bouncebuf[chan] != NULL) {
128 contigfree(buf, bouncebufsize, M_DEVBUF);
131 mtx_unlock(&isa_dma_lock);
135 dma_bouncebufsize[chan] = bouncebufsize;
136 dma_bouncebuf[chan] = buf;
138 mtx_unlock(&isa_dma_lock);
144 * Register a DMA channel's usage. Usually called from a device driver
145 * in open() or during its initialization.
148 isa_dma_acquire(int chan)
151 if (chan & ~VALID_DMA_MASK)
152 panic("isa_dma_acquire: channel out of range");
155 mtx_lock(&isa_dma_lock);
156 if (dma_inuse & (1 << chan)) {
157 printf("isa_dma_acquire: channel %d already in use\n", chan);
158 mtx_unlock(&isa_dma_lock);
161 dma_inuse |= (1 << chan);
162 dma_auto_mode &= ~(1 << chan);
163 mtx_unlock(&isa_dma_lock);
169 * Unregister a DMA channel's usage. Usually called from a device driver
170 * during close() or during its shutdown.
173 isa_dma_release(int chan)
176 if (chan & ~VALID_DMA_MASK)
177 panic("isa_dma_release: channel out of range");
179 mtx_lock(&isa_dma_lock);
180 if ((dma_inuse & (1 << chan)) == 0)
181 printf("isa_dma_release: channel %d not in use\n", chan);
183 mtx_lock(&isa_dma_lock);
186 if (dma_busy & (1 << chan)) {
187 dma_busy &= ~(1 << chan);
189 * XXX We should also do "dma_bounced &= (1 << chan);"
190 * because we are acting on behalf of isa_dmadone() which
191 * was not called to end the last DMA operation. This does
192 * not matter now, but it may in the future.
196 dma_inuse &= ~(1 << chan);
197 dma_auto_mode &= ~(1 << chan);
199 mtx_unlock(&isa_dma_lock);
203 * isa_dmacascade(): program 8237 DMA controller channel to accept
204 * external dma control by a board.
207 isa_dmacascade(int chan)
210 if (chan & ~VALID_DMA_MASK)
211 panic("isa_dmacascade: channel out of range");
214 mtx_lock(&isa_dma_lock);
215 /* set dma channel mode, and set dma channel mode */
216 if ((chan & 4) == 0) {
217 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
218 outb(DMA1_SMSK, chan);
220 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
221 outb(DMA2_SMSK, chan & 3);
223 mtx_unlock(&isa_dma_lock);
227 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
228 * problems by using a bounce buffer.
231 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
236 int dma_range_checked;
238 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
241 if (chan & ~VALID_DMA_MASK)
242 panic("isa_dmastart: channel out of range");
244 if ((chan < 4 && nbytes > (1<<16))
245 || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
246 panic("isa_dmastart: impossible request");
248 mtx_lock(&isa_dma_lock);
249 if ((dma_inuse & (1 << chan)) == 0)
250 printf("isa_dmastart: channel %d not acquired\n", chan);
252 mtx_lock(&isa_dma_lock);
257 * XXX This should be checked, but drivers like ad1848 only call
258 * isa_dmastart() once because they use Auto DMA mode. If we
259 * leave this in, drivers that do this will print this continuously.
261 if (dma_busy & (1 << chan))
262 printf("isa_dmastart: channel %d busy\n", chan);
265 dma_busy |= (1 << chan);
267 if (dma_range_checked) {
268 if (dma_bouncebuf[chan] == NULL
269 || dma_bouncebufsize[chan] < nbytes)
270 panic("isa_dmastart: bad bounce buffer");
271 dma_bounced |= (1 << chan);
272 newaddr = dma_bouncebuf[chan];
274 /* copy bounce buffer on write */
275 if (!(flags & ISADMA_READ))
276 bcopy(addr, newaddr, nbytes);
280 /* translate to physical */
281 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
283 if (flags & ISADMA_RAW) {
284 dma_auto_mode |= (1 << chan);
286 dma_auto_mode &= ~(1 << chan);
289 if ((chan & 4) == 0) {
291 * Program one of DMA channels 0..3. These are
292 * byte mode channels.
294 /* set dma channel mode, and reset address ff */
296 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
297 if (flags & ISADMA_RAW) {
298 if (flags & ISADMA_READ)
299 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
301 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
304 if (flags & ISADMA_READ)
305 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
307 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
310 /* send start address */
311 waport = DMA1_CHN(chan);
313 outb(waport, phys>>8);
314 outb(dmapageport[chan], phys>>16);
317 outb(waport + 1, --nbytes);
318 outb(waport + 1, nbytes>>8);
321 outb(DMA1_SMSK, chan);
324 * Program one of DMA channels 4..7. These are
325 * word mode channels.
327 /* set dma channel mode, and reset address ff */
329 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
330 if (flags & ISADMA_RAW) {
331 if (flags & ISADMA_READ)
332 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
334 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
337 if (flags & ISADMA_READ)
338 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
340 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
343 /* send start address */
344 waport = DMA2_CHN(chan - 4);
345 outb(waport, phys>>1);
346 outb(waport, phys>>9);
347 outb(dmapageport[chan], phys>>16);
351 outb(waport + 2, --nbytes);
352 outb(waport + 2, nbytes>>8);
355 outb(DMA2_SMSK, chan & 3);
357 mtx_unlock(&isa_dma_lock);
361 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
364 if (chan & ~VALID_DMA_MASK)
365 panic("isa_dmadone: channel out of range");
367 if ((dma_inuse & (1 << chan)) == 0)
368 printf("isa_dmadone: channel %d not acquired\n", chan);
371 mtx_lock(&isa_dma_lock);
372 if (((dma_busy & (1 << chan)) == 0) &&
373 (dma_auto_mode & (1 << chan)) == 0 )
374 printf("isa_dmadone: channel %d not busy\n", chan);
376 if ((dma_auto_mode & (1 << chan)) == 0)
377 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
379 if (dma_bounced & (1 << chan)) {
380 /* copy bounce buffer on read */
381 if (flags & ISADMA_READ)
382 bcopy(dma_bouncebuf[chan], addr, nbytes);
384 dma_bounced &= ~(1 << chan);
386 dma_busy &= ~(1 << chan);
387 mtx_unlock(&isa_dma_lock);
391 * Check for problems with the address range of a DMA transfer
392 * (non-contiguous physical pages, outside of bus address space,
393 * crossing DMA page boundaries).
394 * Return true if special handling needed.
398 isa_dmarangecheck(caddr_t va, u_int length, int chan)
400 vm_paddr_t phys, priorpage = 0;
402 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
404 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
405 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
406 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
408 panic("isa_dmacheck: no physical page present");
409 if (phys >= ISARAM_END)
412 if (priorpage + PAGE_SIZE != phys)
414 /* check if crossing a DMA page boundary */
415 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
424 * Query the progress of a transfer on a DMA channel.
426 * To avoid having to interrupt a transfer in progress, we sample
427 * each of the high and low databytes twice, and apply the following
428 * logic to determine the correct count.
430 * Reads are performed with interrupts disabled, thus it is to be
431 * expected that the time between reads is very small. At most
432 * one rollover in the low count byte can be expected within the
433 * four reads that are performed.
435 * There are three gaps in which a rollover can occur :
445 * If a rollover occurs in gap1 or gap2, the low2 value will be
446 * greater than the low1 value. In this case, low2 and high2 are a
447 * corresponding pair.
449 * In any other case, low1 and high1 can be considered to be correct.
451 * The function returns the number of bytes remaining in the transfer,
452 * or -1 if the channel requested is not active.
456 isa_dmastatus_locked(int chan)
460 u_long low1, high1, low2, high2;
462 mtx_assert(&isa_dma_lock, MA_OWNED);
464 /* channel active? */
465 if ((dma_inuse & (1 << chan)) == 0) {
466 printf("isa_dmastatus: channel %d not active\n", chan);
471 if (((dma_busy & (1 << chan)) == 0) &&
472 (dma_auto_mode & (1 << chan)) == 0 ) {
473 printf("chan %d not busy\n", chan);
476 if (chan < 4) { /* low DMA controller */
478 waport = DMA1_CHN(chan) + 1;
479 } else { /* high DMA controller */
481 waport = DMA2_CHN(chan - 4) + 2;
484 disable_intr(); /* no interrupts Mr Jones! */
485 outb(ffport, 0); /* clear register LSB flipflop */
488 outb(ffport, 0); /* clear again */
491 enable_intr(); /* enable interrupts again */
494 * Now decide if a wrap has tried to skew our results.
495 * Note that after TC, the count will read 0xffff, while we want
496 * to return zero, so we add and then mask to compensate.
499 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
501 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
504 if (chan >= 4) /* high channels move words */
510 isa_dmastatus(int chan)
514 mtx_lock(&isa_dma_lock);
515 status = isa_dmastatus_locked(chan);
516 mtx_unlock(&isa_dma_lock);
522 * Reached terminal count yet ?
529 return(inb(DMA1_STATUS) & (1 << chan));
531 return(inb(DMA2_STATUS) & (1 << (chan & 3)));
535 * Stop a DMA transfer currently in progress.
538 isa_dmastop(int chan)
542 mtx_lock(&isa_dma_lock);
543 if ((dma_inuse & (1 << chan)) == 0)
544 printf("isa_dmastop: channel %d not acquired\n", chan);
546 if (((dma_busy & (1 << chan)) == 0) &&
547 ((dma_auto_mode & (1 << chan)) == 0)) {
548 printf("chan %d not busy\n", chan);
549 mtx_unlock(&isa_dma_lock);
553 if ((chan & 4) == 0) {
554 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
556 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
559 status = isa_dmastatus_locked(chan);
561 mtx_unlock(&isa_dma_lock);
567 * Attach to the ISA PnP descriptor for the AT DMA controller
569 static struct isa_pnp_id atdma_ids[] = {
570 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
575 atdma_probe(device_t dev)
579 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
585 atdma_attach(device_t dev)
590 static device_method_t atdma_methods[] = {
591 /* Device interface */
592 DEVMETHOD(device_probe, atdma_probe),
593 DEVMETHOD(device_attach, atdma_attach),
594 DEVMETHOD(device_detach, bus_generic_detach),
595 DEVMETHOD(device_shutdown, bus_generic_shutdown),
596 DEVMETHOD(device_suspend, bus_generic_suspend),
597 DEVMETHOD(device_resume, bus_generic_resume),
601 static driver_t atdma_driver = {
607 static devclass_t atdma_devclass;
609 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
610 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
611 ISA_PNP_INFO(atdma_ids);