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");
99 /* Try malloc() first. It works better if it works. */
100 buf = malloc(bouncebufsize, M_DEVBUF, flag);
102 if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
110 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
111 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
118 mtx_lock(&isa_dma_lock);
120 * If a DMA channel is shared, both drivers have to call isa_dma_init
121 * since they don't know that the other driver will do it.
122 * Just return if we're already set up good.
123 * XXX: this only works if they agree on the bouncebuf size. This
124 * XXX: is typically the case since they are multiple instances of
125 * XXX: the same driver.
127 if (dma_bouncebuf[chan] != NULL) {
129 contigfree(buf, bouncebufsize, M_DEVBUF);
132 mtx_unlock(&isa_dma_lock);
136 dma_bouncebufsize[chan] = bouncebufsize;
137 dma_bouncebuf[chan] = buf;
139 mtx_unlock(&isa_dma_lock);
145 * Register a DMA channel's usage. Usually called from a device driver
146 * in open() or during its initialization.
149 isa_dma_acquire(chan)
153 if (chan & ~VALID_DMA_MASK)
154 panic("isa_dma_acquire: channel out of range");
157 mtx_lock(&isa_dma_lock);
158 if (dma_inuse & (1 << chan)) {
159 printf("isa_dma_acquire: channel %d already in use\n", chan);
160 mtx_unlock(&isa_dma_lock);
163 dma_inuse |= (1 << chan);
164 dma_auto_mode &= ~(1 << chan);
165 mtx_unlock(&isa_dma_lock);
171 * Unregister a DMA channel's usage. Usually called from a device driver
172 * during close() or during its shutdown.
175 isa_dma_release(chan)
179 if (chan & ~VALID_DMA_MASK)
180 panic("isa_dma_release: channel out of range");
182 mtx_lock(&isa_dma_lock);
183 if ((dma_inuse & (1 << chan)) == 0)
184 printf("isa_dma_release: channel %d not in use\n", chan);
186 mtx_lock(&isa_dma_lock);
189 if (dma_busy & (1 << chan)) {
190 dma_busy &= ~(1 << chan);
192 * XXX We should also do "dma_bounced &= (1 << chan);"
193 * because we are acting on behalf of isa_dmadone() which
194 * was not called to end the last DMA operation. This does
195 * not matter now, but it may in the future.
199 dma_inuse &= ~(1 << chan);
200 dma_auto_mode &= ~(1 << chan);
202 mtx_unlock(&isa_dma_lock);
206 * isa_dmacascade(): program 8237 DMA controller channel to accept
207 * external dma control by a board.
214 if (chan & ~VALID_DMA_MASK)
215 panic("isa_dmacascade: channel out of range");
218 mtx_lock(&isa_dma_lock);
219 /* set dma channel mode, and set dma channel mode */
220 if ((chan & 4) == 0) {
221 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
222 outb(DMA1_SMSK, chan);
224 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
225 outb(DMA2_SMSK, chan & 3);
227 mtx_unlock(&isa_dma_lock);
231 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
232 * problems by using a bounce buffer.
235 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
240 int dma_range_checked;
242 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
245 if (chan & ~VALID_DMA_MASK)
246 panic("isa_dmastart: channel out of range");
248 if ((chan < 4 && nbytes > (1<<16))
249 || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
250 panic("isa_dmastart: impossible request");
252 mtx_lock(&isa_dma_lock);
253 if ((dma_inuse & (1 << chan)) == 0)
254 printf("isa_dmastart: channel %d not acquired\n", chan);
256 mtx_lock(&isa_dma_lock);
261 * XXX This should be checked, but drivers like ad1848 only call
262 * isa_dmastart() once because they use Auto DMA mode. If we
263 * leave this in, drivers that do this will print this continuously.
265 if (dma_busy & (1 << chan))
266 printf("isa_dmastart: channel %d busy\n", chan);
269 dma_busy |= (1 << chan);
271 if (dma_range_checked) {
272 if (dma_bouncebuf[chan] == NULL
273 || dma_bouncebufsize[chan] < nbytes)
274 panic("isa_dmastart: bad bounce buffer");
275 dma_bounced |= (1 << chan);
276 newaddr = dma_bouncebuf[chan];
278 /* copy bounce buffer on write */
279 if (!(flags & ISADMA_READ))
280 bcopy(addr, newaddr, nbytes);
284 /* translate to physical */
285 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
287 if (flags & ISADMA_RAW) {
288 dma_auto_mode |= (1 << chan);
290 dma_auto_mode &= ~(1 << chan);
293 if ((chan & 4) == 0) {
295 * Program one of DMA channels 0..3. These are
296 * byte mode channels.
298 /* set dma channel mode, and reset address ff */
300 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
301 if (flags & ISADMA_RAW) {
302 if (flags & ISADMA_READ)
303 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
305 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
308 if (flags & ISADMA_READ)
309 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
311 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
314 /* send start address */
315 waport = DMA1_CHN(chan);
317 outb(waport, phys>>8);
318 outb(dmapageport[chan], phys>>16);
321 outb(waport + 1, --nbytes);
322 outb(waport + 1, nbytes>>8);
325 outb(DMA1_SMSK, chan);
328 * Program one of DMA channels 4..7. These are
329 * word mode channels.
331 /* set dma channel mode, and reset address ff */
333 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
334 if (flags & ISADMA_RAW) {
335 if (flags & ISADMA_READ)
336 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
338 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
341 if (flags & ISADMA_READ)
342 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
344 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
347 /* send start address */
348 waport = DMA2_CHN(chan - 4);
349 outb(waport, phys>>1);
350 outb(waport, phys>>9);
351 outb(dmapageport[chan], phys>>16);
355 outb(waport + 2, --nbytes);
356 outb(waport + 2, nbytes>>8);
359 outb(DMA2_SMSK, chan & 3);
361 mtx_unlock(&isa_dma_lock);
365 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
368 if (chan & ~VALID_DMA_MASK)
369 panic("isa_dmadone: channel out of range");
371 if ((dma_inuse & (1 << chan)) == 0)
372 printf("isa_dmadone: channel %d not acquired\n", chan);
375 mtx_lock(&isa_dma_lock);
376 if (((dma_busy & (1 << chan)) == 0) &&
377 (dma_auto_mode & (1 << chan)) == 0 )
378 printf("isa_dmadone: channel %d not busy\n", chan);
380 if ((dma_auto_mode & (1 << chan)) == 0)
381 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
383 if (dma_bounced & (1 << chan)) {
384 /* copy bounce buffer on read */
385 if (flags & ISADMA_READ)
386 bcopy(dma_bouncebuf[chan], addr, nbytes);
388 dma_bounced &= ~(1 << chan);
390 dma_busy &= ~(1 << chan);
391 mtx_unlock(&isa_dma_lock);
395 * Check for problems with the address range of a DMA transfer
396 * (non-contiguous physical pages, outside of bus address space,
397 * crossing DMA page boundaries).
398 * Return true if special handling needed.
402 isa_dmarangecheck(caddr_t va, u_int length, int chan)
404 vm_paddr_t phys, priorpage = 0;
406 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
408 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
409 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
410 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
412 panic("isa_dmacheck: no physical page present");
413 if (phys >= ISARAM_END)
416 if (priorpage + PAGE_SIZE != phys)
418 /* check if crossing a DMA page boundary */
419 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
428 * Query the progress of a transfer on a DMA channel.
430 * To avoid having to interrupt a transfer in progress, we sample
431 * each of the high and low databytes twice, and apply the following
432 * logic to determine the correct count.
434 * Reads are performed with interrupts disabled, thus it is to be
435 * expected that the time between reads is very small. At most
436 * one rollover in the low count byte can be expected within the
437 * four reads that are performed.
439 * There are three gaps in which a rollover can occur :
449 * If a rollover occurs in gap1 or gap2, the low2 value will be
450 * greater than the low1 value. In this case, low2 and high2 are a
451 * corresponding pair.
453 * In any other case, low1 and high1 can be considered to be correct.
455 * The function returns the number of bytes remaining in the transfer,
456 * or -1 if the channel requested is not active.
460 isa_dmastatus_locked(int chan)
464 u_long low1, high1, low2, high2;
466 mtx_assert(&isa_dma_lock, MA_OWNED);
468 /* channel active? */
469 if ((dma_inuse & (1 << chan)) == 0) {
470 printf("isa_dmastatus: channel %d not active\n", chan);
475 if (((dma_busy & (1 << chan)) == 0) &&
476 (dma_auto_mode & (1 << chan)) == 0 ) {
477 printf("chan %d not busy\n", chan);
480 if (chan < 4) { /* low DMA controller */
482 waport = DMA1_CHN(chan) + 1;
483 } else { /* high DMA controller */
485 waport = DMA2_CHN(chan - 4) + 2;
488 disable_intr(); /* no interrupts Mr Jones! */
489 outb(ffport, 0); /* clear register LSB flipflop */
492 outb(ffport, 0); /* clear again */
495 enable_intr(); /* enable interrupts again */
498 * Now decide if a wrap has tried to skew our results.
499 * Note that after TC, the count will read 0xffff, while we want
500 * to return zero, so we add and then mask to compensate.
503 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
505 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
508 if (chan >= 4) /* high channels move words */
514 isa_dmastatus(int chan)
518 mtx_lock(&isa_dma_lock);
519 status = isa_dmastatus_locked(chan);
520 mtx_unlock(&isa_dma_lock);
526 * Reached terminal count yet ?
533 return(inb(DMA1_STATUS) & (1 << chan));
535 return(inb(DMA2_STATUS) & (1 << (chan & 3)));
539 * Stop a DMA transfer currently in progress.
542 isa_dmastop(int chan)
546 mtx_lock(&isa_dma_lock);
547 if ((dma_inuse & (1 << chan)) == 0)
548 printf("isa_dmastop: channel %d not acquired\n", chan);
550 if (((dma_busy & (1 << chan)) == 0) &&
551 ((dma_auto_mode & (1 << chan)) == 0)) {
552 printf("chan %d not busy\n", chan);
553 mtx_unlock(&isa_dma_lock);
557 if ((chan & 4) == 0) {
558 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
560 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
563 status = isa_dmastatus_locked(chan);
565 mtx_unlock(&isa_dma_lock);
571 * Attach to the ISA PnP descriptor for the AT DMA controller
573 static struct isa_pnp_id atdma_ids[] = {
574 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
579 atdma_probe(device_t dev)
583 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
589 atdma_attach(device_t dev)
594 static device_method_t atdma_methods[] = {
595 /* Device interface */
596 DEVMETHOD(device_probe, atdma_probe),
597 DEVMETHOD(device_attach, atdma_attach),
598 DEVMETHOD(device_detach, bus_generic_detach),
599 DEVMETHOD(device_shutdown, bus_generic_shutdown),
600 DEVMETHOD(device_suspend, bus_generic_suspend),
601 DEVMETHOD(device_resume, bus_generic_resume),
605 static driver_t atdma_driver = {
611 static devclass_t atdma_devclass;
613 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
614 DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
615 ISA_PNP_INFO(atdma_ids);