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
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>
59 #include <machine/md_var.h>
61 #include <vm/vm_param.h>
63 #include <isa/isavar.h>
64 #include <pc98/cbus/cbus.h>
65 #include <pc98/cbus/cbus_dmareg.h>
67 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
69 static caddr_t dma_bouncebuf[4];
70 static u_int dma_bouncebufsize[4];
71 static u_int8_t dma_bounced = 0;
72 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
73 static u_int8_t dma_inuse = 0; /* User for acquire/release */
74 static u_int8_t dma_auto_mode = 0;
75 static struct mtx isa_dma_lock;
76 MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
78 #define VALID_DMA_MASK (3)
80 /* high byte of address is stored in this port for i-th dma channel */
81 static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
84 * Setup a DMA channel's bounce buffer.
87 isa_dma_init(int chan, u_int bouncebufsize, int flag)
92 if (chan & ~VALID_DMA_MASK)
93 panic("isa_dma_init: channel out of range");
94 if (dma_bouncebuf[chan] != NULL)
95 panic("isa_dma_init: impossible request");
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) {
109 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
110 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
116 mtx_lock(&isa_dma_lock);
118 dma_bouncebufsize[chan] = bouncebufsize;
119 dma_bouncebuf[chan] = buf;
121 mtx_unlock(&isa_dma_lock);
127 * Register a DMA channel's usage. Usually called from a device driver
128 * in open() or during its initialization.
131 isa_dma_acquire(chan)
135 if (chan & ~VALID_DMA_MASK)
136 panic("isa_dma_acquire: channel out of range");
139 mtx_lock(&isa_dma_lock);
140 if (dma_inuse & (1 << chan)) {
141 printf("isa_dma_acquire: channel %d already in use\n", chan);
142 mtx_unlock(&isa_dma_lock);
145 dma_inuse |= (1 << chan);
146 dma_auto_mode &= ~(1 << chan);
147 mtx_unlock(&isa_dma_lock);
153 * Unregister a DMA channel's usage. Usually called from a device driver
154 * during close() or during its shutdown.
157 isa_dma_release(chan)
161 if (chan & ~VALID_DMA_MASK)
162 panic("isa_dma_release: channel out of range");
164 mtx_lock(&isa_dma_lock);
165 if ((dma_inuse & (1 << chan)) == 0)
166 printf("isa_dma_release: channel %d not in use\n", chan);
168 mtx_lock(&isa_dma_lock);
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);
184 mtx_unlock(&isa_dma_lock);
188 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
189 * problems by using a bounce buffer.
192 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
197 int dma_range_checked;
199 /* translate to physical */
200 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
201 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
204 if (chan & ~VALID_DMA_MASK)
205 panic("isa_dmastart: channel out of range");
207 if ((chan < 4 && nbytes > (1<<16))
208 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
209 panic("isa_dmastart: impossible request");
211 mtx_lock(&isa_dma_lock);
212 if ((dma_inuse & (1 << chan)) == 0)
213 printf("isa_dmastart: channel %d not acquired\n", chan);
215 mtx_lock(&isa_dma_lock);
220 * XXX This should be checked, but drivers like ad1848 only call
221 * isa_dmastart() once because they use Auto DMA mode. If we
222 * leave this in, drivers that do this will print this continuously.
224 if (dma_busy & (1 << chan))
225 printf("isa_dmastart: channel %d busy\n", chan);
228 dma_busy |= (1 << chan);
230 if (dma_range_checked) {
231 if (dma_bouncebuf[chan] == NULL
232 || dma_bouncebufsize[chan] < nbytes)
233 panic("isa_dmastart: bad bounce buffer");
234 dma_bounced |= (1 << chan);
235 newaddr = dma_bouncebuf[chan];
237 /* copy bounce buffer on write */
238 if (!(flags & ISADMA_READ))
239 bcopy(addr, newaddr, nbytes);
243 if (flags & ISADMA_RAW) {
244 dma_auto_mode |= (1 << chan);
246 dma_auto_mode &= ~(1 << chan);
249 if (need_pre_dma_flush)
250 wbinvd(); /* wbinvd (WB cache flush) */
252 /* set dma channel mode, and reset address ff */
254 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
255 if (flags & ISADMA_RAW) {
256 if (flags & ISADMA_READ)
257 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
259 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
261 if (flags & ISADMA_READ)
262 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
264 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
268 /* send start address */
269 waport = DMA1_CHN(chan);
271 outb(waport, phys>>8);
272 outb(dmapageport[chan], phys>>16);
275 outb(waport + 2, --nbytes);
276 outb(waport + 2, nbytes>>8);
279 outb(DMA1_SMSK, chan);
281 mtx_unlock(&isa_dma_lock);
285 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
288 if (flags & ISADMA_READ) {
289 /* cache flush only after reading 92/12/9 by A.Kojima */
290 if (need_post_dma_flush)
295 if (chan & ~VALID_DMA_MASK)
296 panic("isa_dmadone: channel out of range");
298 if ((dma_inuse & (1 << chan)) == 0)
299 printf("isa_dmadone: channel %d not acquired\n", chan);
302 mtx_lock(&isa_dma_lock);
303 if (((dma_busy & (1 << chan)) == 0) &&
304 (dma_auto_mode & (1 << chan)) == 0 )
305 printf("isa_dmadone: channel %d not busy\n", chan);
307 if ((dma_auto_mode & (1 << chan)) == 0)
308 outb(DMA1_SMSK, (chan & 3) | 4);
310 if (dma_bounced & (1 << chan)) {
311 /* copy bounce buffer on read */
312 if (flags & ISADMA_READ)
313 bcopy(dma_bouncebuf[chan], addr, nbytes);
315 dma_bounced &= ~(1 << chan);
317 dma_busy &= ~(1 << chan);
318 mtx_unlock(&isa_dma_lock);
322 * Check for problems with the address range of a DMA transfer
323 * (non-contiguous physical pages, outside of bus address space,
324 * crossing DMA page boundaries).
325 * Return true if special handling needed.
329 isa_dmarangecheck(caddr_t va, u_int length, int chan)
331 vm_paddr_t phys, priorpage = 0;
333 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
335 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
336 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
337 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
338 #ifdef EPSON_BOUNCEDMA
339 #define ISARAM_END 0x0f00000
341 #define ISARAM_END 0x1000000
344 panic("isa_dmacheck: no physical page present");
345 if (phys >= ISARAM_END)
348 if (priorpage + PAGE_SIZE != phys)
350 /* check if crossing a DMA page boundary */
351 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
360 * Query the progress of a transfer on a DMA channel.
362 * To avoid having to interrupt a transfer in progress, we sample
363 * each of the high and low databytes twice, and apply the following
364 * logic to determine the correct count.
366 * Reads are performed with interrupts disabled, thus it is to be
367 * expected that the time between reads is very small. At most
368 * one rollover in the low count byte can be expected within the
369 * four reads that are performed.
371 * There are three gaps in which a rollover can occur :
381 * If a rollover occurs in gap1 or gap2, the low2 value will be
382 * greater than the low1 value. In this case, low2 and high2 are a
383 * corresponding pair.
385 * In any other case, low1 and high1 can be considered to be correct.
387 * The function returns the number of bytes remaining in the transfer,
388 * or -1 if the channel requested is not active.
392 isa_dmastatus_locked(int chan)
396 u_long low1, high1, low2, high2;
398 mtx_assert(&isa_dma_lock, MA_OWNED);
400 /* channel active? */
401 if ((dma_inuse & (1 << chan)) == 0) {
402 printf("isa_dmastatus: channel %d not active\n", chan);
407 if (((dma_busy & (1 << chan)) == 0) &&
408 (dma_auto_mode & (1 << chan)) == 0 ) {
409 printf("chan %d not busy\n", chan);
413 waport = DMA1_CHN(chan) + 2;
415 disable_intr(); /* no interrupts Mr Jones! */
416 outb(ffport, 0); /* clear register LSB flipflop */
419 outb(ffport, 0); /* clear again */
422 enable_intr(); /* enable interrupts again */
425 * Now decide if a wrap has tried to skew our results.
426 * Note that after TC, the count will read 0xffff, while we want
427 * to return zero, so we add and then mask to compensate.
430 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
432 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
435 if (chan >= 4) /* high channels move words */
441 isa_dmastatus(int chan)
445 mtx_lock(&isa_dma_lock);
446 status = isa_dmastatus_locked(chan);
447 mtx_unlock(&isa_dma_lock);
453 * Reached terminal count yet ?
459 return(inb(DMA1_STATUS) & (1 << chan));
463 * Stop a DMA transfer currently in progress.
466 isa_dmastop(int chan)
470 mtx_lock(&isa_dma_lock);
471 if ((dma_inuse & (1 << chan)) == 0)
472 printf("isa_dmastop: channel %d not acquired\n", chan);
474 if (((dma_busy & (1 << chan)) == 0) &&
475 ((dma_auto_mode & (1 << chan)) == 0)) {
476 printf("chan %d not busy\n", chan);
477 mtx_unlock(&isa_dma_lock);
482 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
484 status = isa_dmastatus_locked(chan);
486 mtx_unlock(&isa_dma_lock);
492 * Attach to the ISA PnP descriptor for the AT DMA controller
494 static struct isa_pnp_id atdma_ids[] = {
495 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
500 atdma_probe(device_t dev)
504 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
510 atdma_attach(device_t dev)
515 static device_method_t atdma_methods[] = {
516 /* Device interface */
517 DEVMETHOD(device_probe, atdma_probe),
518 DEVMETHOD(device_attach, atdma_attach),
519 DEVMETHOD(device_detach, bus_generic_detach),
520 DEVMETHOD(device_shutdown, bus_generic_shutdown),
521 DEVMETHOD(device_suspend, bus_generic_suspend),
522 DEVMETHOD(device_resume, bus_generic_resume),
526 static driver_t atdma_driver = {
532 static devclass_t atdma_devclass;
534 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);