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;
76 #define VALID_DMA_MASK (3)
78 /* high byte of address is stored in this port for i-th dma channel */
79 static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
82 * Setup a DMA channel's bounce buffer.
85 isa_dma_init(int chan, u_int bouncebufsize, int flag)
90 if (chan & ~VALID_DMA_MASK)
91 panic("isa_dma_init: channel out of range");
92 if (dma_bouncebuf[chan] != NULL)
93 panic("isa_dma_init: impossible request");
96 dma_bouncebufsize[chan] = bouncebufsize;
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) {
102 dma_bouncebuf[chan] = buf;
107 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
108 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
111 dma_bouncebuf[chan] = buf;
116 * Register a DMA channel's usage. Usually called from a device driver
117 * in open() or during its initialization.
120 isa_dma_acquire(chan)
124 if (chan & ~VALID_DMA_MASK)
125 panic("isa_dma_acquire: channel out of range");
128 if (dma_inuse & (1 << chan)) {
129 printf("isa_dma_acquire: channel %d already in use\n", chan);
132 dma_inuse |= (1 << chan);
133 dma_auto_mode &= ~(1 << chan);
139 * Unregister a DMA channel's usage. Usually called from a device driver
140 * during close() or during its shutdown.
143 isa_dma_release(chan)
147 if (chan & ~VALID_DMA_MASK)
148 panic("isa_dma_release: channel out of range");
150 if ((dma_inuse & (1 << chan)) == 0)
151 printf("isa_dma_release: channel %d not in use\n", chan);
154 if (dma_busy & (1 << chan)) {
155 dma_busy &= ~(1 << chan);
157 * XXX We should also do "dma_bounced &= (1 << chan);"
158 * because we are acting on behalf of isa_dmadone() which
159 * was not called to end the last DMA operation. This does
160 * not matter now, but it may in the future.
164 dma_inuse &= ~(1 << chan);
165 dma_auto_mode &= ~(1 << chan);
169 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
170 * problems by using a bounce buffer.
173 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
182 if (chan & ~VALID_DMA_MASK)
183 panic("isa_dmastart: channel out of range");
185 if ((chan < 4 && nbytes > (1<<16))
186 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
187 panic("isa_dmastart: impossible request");
189 if ((dma_inuse & (1 << chan)) == 0)
190 printf("isa_dmastart: channel %d not acquired\n", chan);
195 * XXX This should be checked, but drivers like ad1848 only call
196 * isa_dmastart() once because they use Auto DMA mode. If we
197 * leave this in, drivers that do this will print this continuously.
199 if (dma_busy & (1 << chan))
200 printf("isa_dmastart: channel %d busy\n", chan);
203 dma_busy |= (1 << chan);
205 if (isa_dmarangecheck(addr, nbytes, chan)) {
206 if (dma_bouncebuf[chan] == NULL
207 || dma_bouncebufsize[chan] < nbytes)
208 panic("isa_dmastart: bad bounce buffer");
209 dma_bounced |= (1 << chan);
210 newaddr = dma_bouncebuf[chan];
212 /* copy bounce buffer on write */
213 if (!(flags & ISADMA_READ))
214 bcopy(addr, newaddr, nbytes);
218 /* translate to physical */
219 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
221 if (flags & ISADMA_RAW) {
222 dma_auto_mode |= (1 << chan);
224 dma_auto_mode &= ~(1 << chan);
227 if (need_pre_dma_flush)
228 wbinvd(); /* wbinvd (WB cache flush) */
230 /* set dma channel mode, and reset address ff */
232 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
233 if (flags & ISADMA_RAW) {
234 if (flags & ISADMA_READ)
235 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
237 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
239 if (flags & ISADMA_READ)
240 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
242 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
246 /* send start address */
247 waport = DMA1_CHN(chan);
249 outb(waport, phys>>8);
250 outb(dmapageport[chan], phys>>16);
253 outb(waport + 2, --nbytes);
254 outb(waport + 2, nbytes>>8);
257 outb(DMA1_SMSK, chan);
261 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
264 if (flags & ISADMA_READ) {
265 /* cache flush only after reading 92/12/9 by A.Kojima */
266 if (need_post_dma_flush)
271 if (chan & ~VALID_DMA_MASK)
272 panic("isa_dmadone: channel out of range");
274 if ((dma_inuse & (1 << chan)) == 0)
275 printf("isa_dmadone: channel %d not acquired\n", chan);
278 if (((dma_busy & (1 << chan)) == 0) &&
279 (dma_auto_mode & (1 << chan)) == 0 )
280 printf("isa_dmadone: channel %d not busy\n", chan);
282 if ((dma_auto_mode & (1 << chan)) == 0)
283 outb(DMA1_SMSK, (chan & 3) | 4);
285 if (dma_bounced & (1 << chan)) {
286 /* copy bounce buffer on read */
287 if (flags & ISADMA_READ)
288 bcopy(dma_bouncebuf[chan], addr, nbytes);
290 dma_bounced &= ~(1 << chan);
292 dma_busy &= ~(1 << chan);
296 * Check for problems with the address range of a DMA transfer
297 * (non-contiguous physical pages, outside of bus address space,
298 * crossing DMA page boundaries).
299 * Return true if special handling needed.
303 isa_dmarangecheck(caddr_t va, u_int length, int chan)
305 vm_paddr_t phys, priorpage = 0;
307 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
311 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
312 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
313 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
314 #ifdef EPSON_BOUNCEDMA
315 #define ISARAM_END 0xf00000
317 #define ISARAM_END RAM_END
320 panic("isa_dmacheck: no physical page present");
321 if (phys >= ISARAM_END)
324 if (priorpage + PAGE_SIZE != phys)
326 /* check if crossing a DMA page boundary */
327 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
336 * Query the progress of a transfer on a DMA channel.
338 * To avoid having to interrupt a transfer in progress, we sample
339 * each of the high and low databytes twice, and apply the following
340 * logic to determine the correct count.
342 * Reads are performed with interrupts disabled, thus it is to be
343 * expected that the time between reads is very small. At most
344 * one rollover in the low count byte can be expected within the
345 * four reads that are performed.
347 * There are three gaps in which a rollover can occur :
357 * If a rollover occurs in gap1 or gap2, the low2 value will be
358 * greater than the low1 value. In this case, low2 and high2 are a
359 * corresponding pair.
361 * In any other case, low1 and high1 can be considered to be correct.
363 * The function returns the number of bytes remaining in the transfer,
364 * or -1 if the channel requested is not active.
368 isa_dmastatus(int chan)
372 u_long low1, high1, low2, high2;
374 /* channel active? */
375 if ((dma_inuse & (1 << chan)) == 0) {
376 printf("isa_dmastatus: channel %d not active\n", chan);
381 if (((dma_busy & (1 << chan)) == 0) &&
382 (dma_auto_mode & (1 << chan)) == 0 ) {
383 printf("chan %d not busy\n", chan);
387 waport = DMA1_CHN(chan) + 2;
389 disable_intr(); /* no interrupts Mr Jones! */
390 outb(ffport, 0); /* clear register LSB flipflop */
393 outb(ffport, 0); /* clear again */
396 enable_intr(); /* enable interrupts again */
399 * Now decide if a wrap has tried to skew our results.
400 * Note that after TC, the count will read 0xffff, while we want
401 * to return zero, so we add and then mask to compensate.
404 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
406 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
409 if (chan >= 4) /* high channels move words */
415 * Reached terminal count yet ?
421 return(inb(DMA1_STATUS) & (1 << chan));
425 * Stop a DMA transfer currently in progress.
428 isa_dmastop(int chan)
430 if ((dma_inuse & (1 << chan)) == 0)
431 printf("isa_dmastop: channel %d not acquired\n", chan);
433 if (((dma_busy & (1 << chan)) == 0) &&
434 ((dma_auto_mode & (1 << chan)) == 0)) {
435 printf("chan %d not busy\n", chan);
440 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
442 return(isa_dmastatus(chan));
446 * Attach to the ISA PnP descriptor for the AT DMA controller
448 static struct isa_pnp_id atdma_ids[] = {
449 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
454 atdma_probe(device_t dev)
458 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
464 atdma_attach(device_t dev)
469 static device_method_t atdma_methods[] = {
470 /* Device interface */
471 DEVMETHOD(device_probe, atdma_probe),
472 DEVMETHOD(device_attach, atdma_attach),
473 DEVMETHOD(device_detach, bus_generic_detach),
474 DEVMETHOD(device_shutdown, bus_generic_shutdown),
475 DEVMETHOD(device_suspend, bus_generic_suspend),
476 DEVMETHOD(device_resume, bus_generic_resume),
480 static driver_t atdma_driver = {
486 static devclass_t atdma_devclass;
488 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);