isa_dma.c revision 199104
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/i386/isa/isa_dma.c 199104 2009-11-09 20:29:10Z rdivacky $");
37
38/*
39 * code to manage AT bus
40 *
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
45 * isa_dmastart()
46 */
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/bus.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/lock.h>
54#include <sys/proc.h>
55#include <sys/mutex.h>
56#include <sys/module.h>
57#include <vm/vm.h>
58#include <vm/vm_param.h>
59#include <vm/pmap.h>
60#include <isa/isareg.h>
61#include <isa/isavar.h>
62#include <isa/isa_dmareg.h>
63
64static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
65
66static caddr_t	dma_bouncebuf[8];
67static u_int	dma_bouncebufsize[8];
68static u_int8_t	dma_bounced = 0;
69static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
70static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
71static u_int8_t dma_auto_mode = 0;
72static struct mtx isa_dma_lock;
73MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
74
75#define VALID_DMA_MASK (7)
76
77/* high byte of address is stored in this port for i-th dma channel */
78static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
79
80/*
81 * Setup a DMA channel's bounce buffer.
82 */
83int
84isa_dma_init(int chan, u_int bouncebufsize, int flag)
85{
86	void *buf;
87	int contig;
88
89#ifdef DIAGNOSTIC
90	if (chan & ~VALID_DMA_MASK)
91		panic("isa_dma_init: channel out of range");
92#endif
93
94
95	/* Try malloc() first.  It works better if it works. */
96	buf = malloc(bouncebufsize, M_DEVBUF, flag);
97	if (buf != NULL) {
98		if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
99			free(buf, M_DEVBUF);
100			buf = NULL;
101		}
102		contig = 0;
103	}
104
105	if (buf == NULL) {
106		buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
107			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
108		contig = 1;
109	}
110
111	if (buf == NULL)
112		return (ENOMEM);
113
114	mtx_lock(&isa_dma_lock);
115	/*
116	 * If a DMA channel is shared, both drivers have to call isa_dma_init
117	 * since they don't know that the other driver will do it.
118	 * Just return if we're already set up good.
119	 * XXX: this only works if they agree on the bouncebuf size.  This
120	 * XXX: is typically the case since they are multiple instances of
121	 * XXX: the same driver.
122	 */
123	if (dma_bouncebuf[chan] != NULL) {
124		if (contig)
125			contigfree(buf, bouncebufsize, M_DEVBUF);
126		else
127			free(buf, M_DEVBUF);
128		mtx_unlock(&isa_dma_lock);
129		return (0);
130	}
131
132	dma_bouncebufsize[chan] = bouncebufsize;
133	dma_bouncebuf[chan] = buf;
134
135	mtx_unlock(&isa_dma_lock);
136
137	return (0);
138}
139
140/*
141 * Register a DMA channel's usage.  Usually called from a device driver
142 * in open() or during its initialization.
143 */
144int
145isa_dma_acquire(chan)
146	int chan;
147{
148#ifdef DIAGNOSTIC
149	if (chan & ~VALID_DMA_MASK)
150		panic("isa_dma_acquire: channel out of range");
151#endif
152
153	mtx_lock(&isa_dma_lock);
154	if (dma_inuse & (1 << chan)) {
155		printf("isa_dma_acquire: channel %d already in use\n", chan);
156		mtx_unlock(&isa_dma_lock);
157		return (EBUSY);
158	}
159	dma_inuse |= (1 << chan);
160	dma_auto_mode &= ~(1 << chan);
161	mtx_unlock(&isa_dma_lock);
162
163	return (0);
164}
165
166/*
167 * Unregister a DMA channel's usage.  Usually called from a device driver
168 * during close() or during its shutdown.
169 */
170void
171isa_dma_release(chan)
172	int chan;
173{
174#ifdef DIAGNOSTIC
175	if (chan & ~VALID_DMA_MASK)
176		panic("isa_dma_release: channel out of range");
177
178	mtx_lock(&isa_dma_lock);
179	if ((dma_inuse & (1 << chan)) == 0)
180		printf("isa_dma_release: channel %d not in use\n", chan);
181#else
182	mtx_lock(&isa_dma_lock);
183#endif
184
185	if (dma_busy & (1 << chan)) {
186		dma_busy &= ~(1 << chan);
187		/*
188		 * XXX We should also do "dma_bounced &= (1 << chan);"
189		 * because we are acting on behalf of isa_dmadone() which
190		 * was not called to end the last DMA operation.  This does
191		 * not matter now, but it may in the future.
192		 */
193	}
194
195	dma_inuse &= ~(1 << chan);
196	dma_auto_mode &= ~(1 << chan);
197
198	mtx_unlock(&isa_dma_lock);
199}
200
201/*
202 * isa_dmacascade(): program 8237 DMA controller channel to accept
203 * external dma control by a board.
204 */
205void
206isa_dmacascade(chan)
207	int chan;
208{
209#ifdef DIAGNOSTIC
210	if (chan & ~VALID_DMA_MASK)
211		panic("isa_dmacascade: channel out of range");
212#endif
213
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);
219	} else {
220		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
221		outb(DMA2_SMSK, chan & 3);
222	}
223	mtx_unlock(&isa_dma_lock);
224}
225
226/*
227 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
228 * problems by using a bounce buffer.
229 */
230void
231isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
232{
233	vm_paddr_t phys;
234	int waport;
235	caddr_t newaddr;
236	int dma_range_checked;
237
238	/* translate to physical */
239	phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
240	dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
241
242#ifdef DIAGNOSTIC
243	if (chan & ~VALID_DMA_MASK)
244		panic("isa_dmastart: channel out of range");
245
246	if ((chan < 4 && nbytes > (1<<16))
247	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
248		panic("isa_dmastart: impossible request");
249
250	mtx_lock(&isa_dma_lock);
251	if ((dma_inuse & (1 << chan)) == 0)
252		printf("isa_dmastart: channel %d not acquired\n", chan);
253#else
254	mtx_lock(&isa_dma_lock);
255#endif
256
257#if 0
258	/*
259	 * XXX This should be checked, but drivers like ad1848 only call
260	 * isa_dmastart() once because they use Auto DMA mode.  If we
261	 * leave this in, drivers that do this will print this continuously.
262	 */
263	if (dma_busy & (1 << chan))
264		printf("isa_dmastart: channel %d busy\n", chan);
265#endif
266
267	dma_busy |= (1 << chan);
268
269	if (dma_range_checked) {
270		if (dma_bouncebuf[chan] == NULL
271		    || dma_bouncebufsize[chan] < nbytes)
272			panic("isa_dmastart: bad bounce buffer");
273		dma_bounced |= (1 << chan);
274		newaddr = dma_bouncebuf[chan];
275
276		/* copy bounce buffer on write */
277		if (!(flags & ISADMA_READ))
278			bcopy(addr, newaddr, nbytes);
279		addr = newaddr;
280	}
281
282	if (flags & ISADMA_RAW) {
283	    dma_auto_mode |= (1 << chan);
284	} else {
285	    dma_auto_mode &= ~(1 << chan);
286	}
287
288	if ((chan & 4) == 0) {
289		/*
290		 * Program one of DMA channels 0..3.  These are
291		 * byte mode channels.
292		 */
293		/* set dma channel mode, and reset address ff */
294
295		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
296		if (flags & ISADMA_RAW) {
297		  if (flags & ISADMA_READ)
298			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
299		  else
300			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
301		}
302		else
303		if (flags & ISADMA_READ)
304			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
305		else
306			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
307		outb(DMA1_FFC, 0);
308
309		/* send start address */
310		waport =  DMA1_CHN(chan);
311		outb(waport, phys);
312		outb(waport, phys>>8);
313		outb(dmapageport[chan], phys>>16);
314
315		/* send count */
316		outb(waport + 1, --nbytes);
317		outb(waport + 1, nbytes>>8);
318
319		/* unmask channel */
320		outb(DMA1_SMSK, chan);
321	} else {
322		/*
323		 * Program one of DMA channels 4..7.  These are
324		 * word mode channels.
325		 */
326		/* set dma channel mode, and reset address ff */
327
328		/* If ISADMA_RAW flag is set, then use autoinitialise mode */
329		if (flags & ISADMA_RAW) {
330		  if (flags & ISADMA_READ)
331			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
332		  else
333			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
334		}
335		else
336		if (flags & ISADMA_READ)
337			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
338		else
339			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
340		outb(DMA2_FFC, 0);
341
342		/* send start address */
343		waport = DMA2_CHN(chan - 4);
344		outb(waport, phys>>1);
345		outb(waport, phys>>9);
346		outb(dmapageport[chan], phys>>16);
347
348		/* send count */
349		nbytes >>= 1;
350		outb(waport + 2, --nbytes);
351		outb(waport + 2, nbytes>>8);
352
353		/* unmask channel */
354		outb(DMA2_SMSK, chan & 3);
355	}
356	mtx_unlock(&isa_dma_lock);
357}
358
359void
360isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
361{
362#ifdef DIAGNOSTIC
363	if (chan & ~VALID_DMA_MASK)
364		panic("isa_dmadone: channel out of range");
365
366	if ((dma_inuse & (1 << chan)) == 0)
367		printf("isa_dmadone: channel %d not acquired\n", chan);
368#endif
369
370	mtx_lock(&isa_dma_lock);
371	if (((dma_busy & (1 << chan)) == 0) &&
372	    (dma_auto_mode & (1 << chan)) == 0 )
373		printf("isa_dmadone: channel %d not busy\n", chan);
374
375	if ((dma_auto_mode & (1 << chan)) == 0)
376		outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
377
378	if (dma_bounced & (1 << chan)) {
379		/* copy bounce buffer on read */
380		if (flags & ISADMA_READ)
381			bcopy(dma_bouncebuf[chan], addr, nbytes);
382
383		dma_bounced &= ~(1 << chan);
384	}
385	dma_busy &= ~(1 << chan);
386	mtx_unlock(&isa_dma_lock);
387}
388
389/*
390 * Check for problems with the address range of a DMA transfer
391 * (non-contiguous physical pages, outside of bus address space,
392 * crossing DMA page boundaries).
393 * Return true if special handling needed.
394 */
395
396static int
397isa_dmarangecheck(caddr_t va, u_int length, int chan)
398{
399	vm_paddr_t phys, priorpage = 0;
400	vm_offset_t endva;
401	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
402
403	endva = (vm_offset_t)round_page((vm_offset_t)va + length);
404	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
405		phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
406#define ISARAM_END	RAM_END
407		if (phys == 0)
408			panic("isa_dmacheck: no physical page present");
409		if (phys >= ISARAM_END)
410			return (1);
411		if (priorpage) {
412			if (priorpage + PAGE_SIZE != phys)
413				return (1);
414			/* check if crossing a DMA page boundary */
415			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
416				return (1);
417		}
418		priorpage = phys;
419	}
420	return (0);
421}
422
423/*
424 * Query the progress of a transfer on a DMA channel.
425 *
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.
429 *
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.
434 *
435 * There are three gaps in which a rollover can occur :
436 *
437 * - read low1
438 *              gap1
439 * - read high1
440 *              gap2
441 * - read low2
442 *              gap3
443 * - read high2
444 *
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.
448 *
449 * In any other case, low1 and high1 can be considered to be correct.
450 *
451 * The function returns the number of bytes remaining in the transfer,
452 * or -1 if the channel requested is not active.
453 *
454 */
455static int
456isa_dmastatus_locked(int chan)
457{
458	u_long	cnt = 0;
459	int	ffport, waport;
460	u_long	low1, high1, low2, high2;
461
462	mtx_assert(&isa_dma_lock, MA_OWNED);
463
464	/* channel active? */
465	if ((dma_inuse & (1 << chan)) == 0) {
466		printf("isa_dmastatus: channel %d not active\n", chan);
467		return(-1);
468	}
469	/* channel busy? */
470
471	if (((dma_busy & (1 << chan)) == 0) &&
472	    (dma_auto_mode & (1 << chan)) == 0 ) {
473	    printf("chan %d not busy\n", chan);
474	    return -2 ;
475	}
476	if (chan < 4) {			/* low DMA controller */
477		ffport = DMA1_FFC;
478		waport = DMA1_CHN(chan) + 1;
479	} else {			/* high DMA controller */
480		ffport = DMA2_FFC;
481		waport = DMA2_CHN(chan - 4) + 2;
482	}
483
484	disable_intr();			/* no interrupts Mr Jones! */
485	outb(ffport, 0);		/* clear register LSB flipflop */
486	low1 = inb(waport);
487	high1 = inb(waport);
488	outb(ffport, 0);		/* clear again */
489	low2 = inb(waport);
490	high2 = inb(waport);
491	enable_intr();			/* enable interrupts again */
492
493	/*
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.
497	 */
498	if (low1 >= low2) {
499		cnt = (low1 + (high1 << 8) + 1) & 0xffff;
500	} else {
501		cnt = (low2 + (high2 << 8) + 1) & 0xffff;
502	}
503
504	if (chan >= 4)			/* high channels move words */
505		cnt *= 2;
506	return(cnt);
507}
508
509int
510isa_dmastatus(int chan)
511{
512	int status;
513
514	mtx_lock(&isa_dma_lock);
515	status = isa_dmastatus_locked(chan);
516	mtx_unlock(&isa_dma_lock);
517
518	return (status);
519}
520
521/*
522 * Reached terminal count yet ?
523 */
524int
525isa_dmatc(int chan)
526{
527
528	if (chan < 4)
529		return(inb(DMA1_STATUS) & (1 << chan));
530	else
531		return(inb(DMA2_STATUS) & (1 << (chan & 3)));
532}
533
534/*
535 * Stop a DMA transfer currently in progress.
536 */
537int
538isa_dmastop(int chan)
539{
540	int status;
541
542	mtx_lock(&isa_dma_lock);
543	if ((dma_inuse & (1 << chan)) == 0)
544		printf("isa_dmastop: channel %d not acquired\n", chan);
545
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);
550		return -2 ;
551	}
552
553	if ((chan & 4) == 0) {
554		outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
555	} else {
556		outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
557	}
558
559	status = isa_dmastatus_locked(chan);
560
561	mtx_unlock(&isa_dma_lock);
562
563	return (status);
564}
565
566/*
567 * Attach to the ISA PnP descriptor for the AT DMA controller
568 */
569static struct isa_pnp_id atdma_ids[] = {
570	{ 0x0002d041 /* PNP0200 */, "AT DMA controller" },
571	{ 0 }
572};
573
574static int
575atdma_probe(device_t dev)
576{
577	int result;
578
579	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
580		device_quiet(dev);
581	return(result);
582}
583
584static int
585atdma_attach(device_t dev)
586{
587	return(0);
588}
589
590static 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),
598	{ 0, 0 }
599};
600
601static driver_t atdma_driver = {
602	"atdma",
603	atdma_methods,
604	1,		/* no softc */
605};
606
607static devclass_t atdma_devclass;
608
609DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
610DRIVER_MODULE(atdma, acpi, atdma_driver, atdma_devclass, 0, 0);
611