Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.4
      1  1.1  mycroft /*-
      2  1.1  mycroft  * Copyright (c) 1993 Charles Hannum.
      3  1.1  mycroft  * Copyright (c) 1991 The Regents of the University of California.
      4  1.1  mycroft  * All rights reserved.
      5  1.1  mycroft  *
      6  1.1  mycroft  * This code is derived from software contributed to Berkeley by
      7  1.1  mycroft  * William Jolitz.
      8  1.1  mycroft  *
      9  1.1  mycroft  * Redistribution and use in source and binary forms, with or without
     10  1.1  mycroft  * modification, are permitted provided that the following conditions
     11  1.1  mycroft  * are met:
     12  1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
     13  1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     14  1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     17  1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     18  1.1  mycroft  *    must display the following acknowledgement:
     19  1.1  mycroft  *	This product includes software developed by the University of
     20  1.1  mycroft  *	California, Berkeley and its contributors.
     21  1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  mycroft  *    may be used to endorse or promote products derived from this software
     23  1.1  mycroft  *    without specific prior written permission.
     24  1.1  mycroft  *
     25  1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  mycroft  * SUCH DAMAGE.
     36  1.1  mycroft  *
     37  1.1  mycroft  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     38  1.4  mycroft  *	$Id: isadma.c,v 1.4 1993/10/22 20:24:14 mycroft Exp $
     39  1.1  mycroft  */
     40  1.1  mycroft 
     41  1.1  mycroft /*
     42  1.1  mycroft  * code to deal with ISA DMA
     43  1.1  mycroft  */
     44  1.1  mycroft 
     45  1.3  mycroft #include <sys/param.h>
     46  1.3  mycroft #include <sys/systm.h>
     47  1.3  mycroft #include <sys/conf.h>
     48  1.3  mycroft #include <sys/file.h>
     49  1.3  mycroft #include <sys/syslog.h>
     50  1.3  mycroft #include <sys/device.h>
     51  1.3  mycroft #include <vm/vm.h>
     52  1.3  mycroft 
     53  1.3  mycroft #include <machine/cpu.h>
     54  1.3  mycroft #include <machine/pio.h>
     55  1.3  mycroft 
     56  1.3  mycroft #include <i386/isa/isa.h>
     57  1.3  mycroft #include <i386/isa/isavar.h>
     58  1.3  mycroft #include <i386/isa/ic/i8237.h>
     59  1.1  mycroft 
     60  1.1  mycroft /*
     61  1.1  mycroft **  Register definitions for DMA controller 1 (channels 0..3):
     62  1.1  mycroft */
     63  1.1  mycroft #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     64  1.1  mycroft #define	DMA1_SR		(IO_DMA1 + 1*8)		/* status register */
     65  1.1  mycroft #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     66  1.1  mycroft #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     67  1.1  mycroft #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     68  1.1  mycroft 
     69  1.1  mycroft /*
     70  1.1  mycroft **  Register definitions for DMA controller 2 (channels 4..7):
     71  1.1  mycroft */
     72  1.1  mycroft #define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
     73  1.1  mycroft #define	DMA2_SR		(IO_DMA2 + 2*8)		/* status register */
     74  1.1  mycroft #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     75  1.1  mycroft #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     76  1.1  mycroft #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     77  1.1  mycroft 
     78  1.1  mycroft /* region of physical memory known to be contiguous */
     79  1.1  mycroft caddr_t isaphysmem;
     80  1.1  mycroft static caddr_t bouncebuf[8];		/* XXX */
     81  1.1  mycroft static caddr_t bounced[8];		/* XXX */
     82  1.1  mycroft static vm_size_t bouncesize[8];		/* XXX */
     83  1.1  mycroft 
     84  1.1  mycroft /* high byte of address is stored in this port for i-th dma channel */
     85  1.1  mycroft static u_short dmapageport[8] =
     86  1.1  mycroft 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
     87  1.1  mycroft 
     88  1.1  mycroft /*
     89  1.4  mycroft  * at_setup_dmachan(): allocate bounce buffer and check for conflicts.
     90  1.4  mycroft  *
     91  1.4  mycroft  * XXX
     92  1.4  mycroft  * This sucks.  We should be able to bounce more than NBPG bytes, but I
     93  1.4  mycroft  * don't feel like writing the code to do contiguous allocation right now.
     94  1.4  mycroft  */
     95  1.4  mycroft void
     96  1.4  mycroft at_setup_dmachan(chan, max)
     97  1.4  mycroft 	int chan;
     98  1.4  mycroft 	u_long max;
     99  1.4  mycroft {
    100  1.4  mycroft 
    101  1.4  mycroft #ifdef DIAGNOSTIC
    102  1.4  mycroft 	if (chan > 7 || chan < 0)
    103  1.4  mycroft 		panic("at_setup_dmachan: impossible request");
    104  1.4  mycroft 	if (max > NBPG)
    105  1.4  mycroft 		panic("at_setup_dmachan: what a lose");
    106  1.4  mycroft #endif
    107  1.4  mycroft 
    108  1.4  mycroft 	/* XXX check for drq conflict? */
    109  1.4  mycroft 
    110  1.4  mycroft 	bouncebuf[chan] = (caddr_t) isaphysmem + NBPG*chan;
    111  1.4  mycroft }
    112  1.4  mycroft 
    113  1.4  mycroft /*
    114  1.1  mycroft  * at_dma_cascade(): program 8237 DMA controller channel to accept
    115  1.1  mycroft  * external dma control by a board.
    116  1.1  mycroft  */
    117  1.1  mycroft void
    118  1.1  mycroft at_dma_cascade(chan)
    119  1.4  mycroft 	int chan;
    120  1.1  mycroft {
    121  1.4  mycroft 
    122  1.1  mycroft #ifdef DIAGNOSTIC
    123  1.4  mycroft 	if (chan > 7 || chan < 0)
    124  1.1  mycroft 		panic("at_dma_cascade: impossible request");
    125  1.1  mycroft #endif
    126  1.1  mycroft 
    127  1.4  mycroft 	/* XXX check for drq conflict? */
    128  1.4  mycroft 
    129  1.1  mycroft 	/* set dma channel mode, and set dma channel mode */
    130  1.1  mycroft 	if ((chan & 4) == 0) {
    131  1.1  mycroft 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    132  1.1  mycroft 		outb(DMA1_SMSK, chan);
    133  1.1  mycroft 	} else {
    134  1.1  mycroft 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    135  1.1  mycroft 		outb(DMA2_SMSK, chan & 3);
    136  1.1  mycroft 	}
    137  1.1  mycroft }
    138  1.1  mycroft 
    139  1.1  mycroft /*
    140  1.1  mycroft  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    141  1.1  mycroft  * problems by using a bounce buffer.
    142  1.1  mycroft  */
    143  1.1  mycroft void
    144  1.1  mycroft at_dma(read, addr, nbytes, chan)
    145  1.1  mycroft 	int read;
    146  1.1  mycroft 	caddr_t addr;
    147  1.1  mycroft 	vm_size_t nbytes;
    148  1.4  mycroft 	int chan;
    149  1.1  mycroft {
    150  1.1  mycroft 	vm_offset_t phys;
    151  1.1  mycroft 	int waport;
    152  1.1  mycroft 	caddr_t newaddr;
    153  1.1  mycroft 
    154  1.4  mycroft #ifdef DIAGNOSTIC
    155  1.4  mycroft 	if (chan > 7 || chan < 0 ||
    156  1.1  mycroft 	    (chan < 4 && nbytes > (1<<16)) ||
    157  1.2  mycroft 	    (chan >= 4 && (nbytes > (1<<17) || nbytes & 1 || (u_int)addr & 1)))
    158  1.1  mycroft 		panic("at_dma: impossible request");
    159  1.4  mycroft #endif
    160  1.1  mycroft 
    161  1.1  mycroft 	if (at_dma_rangecheck((vm_offset_t)addr, nbytes, chan)) {
    162  1.4  mycroft 		if (bouncebuf[chan] == 0)
    163  1.4  mycroft 			/* some twit forgot to call at_setup_dmachan() */
    164  1.4  mycroft 			panic("at_dma: no bounce buffer");
    165  1.1  mycroft 		/* XXX totally braindead; NBPG is not enough */
    166  1.4  mycroft 		if (nbytes > NBPG)
    167  1.4  mycroft 			panic("at_dma: transfer too large");
    168  1.1  mycroft 		bouncesize[chan] = nbytes;
    169  1.1  mycroft 		newaddr = bouncebuf[chan];
    170  1.1  mycroft 		/* copy bounce buffer on write */
    171  1.1  mycroft 		if (!read) {
    172  1.1  mycroft 			bcopy(addr, newaddr, nbytes);
    173  1.1  mycroft 			bounced[chan] = 0;
    174  1.1  mycroft 		} else
    175  1.1  mycroft 			bounced[chan] = addr;
    176  1.1  mycroft 		addr = newaddr;
    177  1.1  mycroft 	}
    178  1.1  mycroft 
    179  1.1  mycroft 	/* translate to physical */
    180  1.1  mycroft 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    181  1.1  mycroft 
    182  1.1  mycroft 	if ((chan & 4) == 0) {
    183  1.1  mycroft 		/*
    184  1.1  mycroft 		 * Program one of DMA channels 0..3.  These are
    185  1.1  mycroft 		 * byte mode channels.
    186  1.1  mycroft 		 */
    187  1.1  mycroft 		/* set dma channel mode, and reset address ff */
    188  1.1  mycroft 		if (read)
    189  1.1  mycroft 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    190  1.1  mycroft 		else
    191  1.1  mycroft 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    192  1.1  mycroft 		outb(DMA1_FFC, 0);
    193  1.1  mycroft 
    194  1.1  mycroft 		/* send start address */
    195  1.1  mycroft 		waport =  DMA1_CHN(chan);
    196  1.1  mycroft 		outb(waport, phys);
    197  1.1  mycroft 		outb(waport, phys>>8);
    198  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    199  1.1  mycroft 
    200  1.1  mycroft 		/* send count */
    201  1.1  mycroft 		outb(waport + 1, --nbytes);
    202  1.1  mycroft 		outb(waport + 1, nbytes>>8);
    203  1.1  mycroft 
    204  1.1  mycroft 		/* unmask channel */
    205  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_CLEAR | chan);
    206  1.1  mycroft 	} else {
    207  1.1  mycroft 		/*
    208  1.1  mycroft 		 * Program one of DMA channels 4..7.  These are
    209  1.1  mycroft 		 * word mode channels.
    210  1.1  mycroft 		 */
    211  1.1  mycroft 		/* set dma channel mode, and reset address ff */
    212  1.1  mycroft 		if (read)
    213  1.1  mycroft 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    214  1.1  mycroft 		else
    215  1.1  mycroft 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    216  1.1  mycroft 		outb(DMA2_FFC, 0);
    217  1.1  mycroft 
    218  1.1  mycroft 		/* send start address */
    219  1.1  mycroft 		waport = DMA2_CHN(chan & 3);
    220  1.1  mycroft 		outb(waport, phys>>1);
    221  1.1  mycroft 		outb(waport, phys>>9);
    222  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    223  1.1  mycroft 
    224  1.1  mycroft 		/* send count */
    225  1.1  mycroft 		nbytes >>= 1;
    226  1.1  mycroft 		outb(waport + 2, --nbytes);
    227  1.1  mycroft 		outb(waport + 2, nbytes>>8);
    228  1.1  mycroft 
    229  1.1  mycroft 		/* unmask channel */
    230  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_CLEAR | (chan & 3));
    231  1.1  mycroft 	}
    232  1.1  mycroft }
    233  1.1  mycroft 
    234  1.1  mycroft /*
    235  1.1  mycroft  * Abort a DMA request, clearing the bounce buffer, if any.
    236  1.1  mycroft  */
    237  1.1  mycroft void
    238  1.1  mycroft at_dma_abort(chan)
    239  1.4  mycroft 	int chan;
    240  1.1  mycroft {
    241  1.1  mycroft 
    242  1.1  mycroft #ifdef DIAGNOSTIC
    243  1.4  mycroft 	if (chan > 7 || chan < 0)
    244  1.1  mycroft 		panic("at_dma_abort: impossible request");
    245  1.1  mycroft #endif
    246  1.1  mycroft 
    247  1.1  mycroft 	bounced[chan] = 0;
    248  1.1  mycroft 
    249  1.1  mycroft 	/* mask channel */
    250  1.1  mycroft 	if ((chan & 4) == 0)
    251  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    252  1.1  mycroft 	else
    253  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    254  1.1  mycroft }
    255  1.1  mycroft 
    256  1.1  mycroft /*
    257  1.1  mycroft  * End a DMA request, copying data from the bounce buffer, if any,
    258  1.1  mycroft  * when reading.
    259  1.1  mycroft  */
    260  1.1  mycroft void
    261  1.1  mycroft at_dma_terminate(chan)
    262  1.1  mycroft 	unsigned chan;
    263  1.1  mycroft {
    264  1.1  mycroft 	u_char tc;
    265  1.1  mycroft 
    266  1.1  mycroft #ifdef DIAGNOSTIC
    267  1.4  mycroft 	if (chan > 7 || chan < 0)
    268  1.1  mycroft 		panic("at_dma_terminate: impossible request");
    269  1.1  mycroft #endif
    270  1.1  mycroft 
    271  1.1  mycroft 	/* check that the terminal count was reached */
    272  1.1  mycroft 	if ((chan & 4) == 0)
    273  1.1  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    274  1.1  mycroft 	else
    275  1.1  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    276  1.1  mycroft 	if (tc == 0)
    277  1.1  mycroft 		/* XXX probably should panic or something */
    278  1.1  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    279  1.1  mycroft 
    280  1.1  mycroft 	/* copy bounce buffer on read */
    281  1.1  mycroft 	if (bounced[chan]) {
    282  1.1  mycroft 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    283  1.1  mycroft 		bounced[chan] = 0;
    284  1.1  mycroft 	}
    285  1.1  mycroft 
    286  1.1  mycroft 	/* mask channel */
    287  1.1  mycroft 	if ((chan & 4) == 0)
    288  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    289  1.1  mycroft 	else
    290  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    291  1.1  mycroft }
    292  1.1  mycroft 
    293  1.1  mycroft /*
    294  1.1  mycroft  * Check for problems with the address range of a DMA transfer
    295  1.1  mycroft  * (non-contiguous physical pages, outside of bus address space,
    296  1.1  mycroft  * crossing DMA page boundaries).
    297  1.1  mycroft  * Return true if special handling needed.
    298  1.1  mycroft  */
    299  1.1  mycroft int
    300  1.1  mycroft at_dma_rangecheck(va, length, chan)
    301  1.1  mycroft 	vm_offset_t va;
    302  1.4  mycroft 	u_long length;
    303  1.4  mycroft 	int chan;
    304  1.1  mycroft {
    305  1.1  mycroft 	vm_offset_t phys, priorpage = 0, endva;
    306  1.1  mycroft 	u_int dma_pgmsk = (chan&4) ?  ~(128*1024-1) : ~(64*1024-1);
    307  1.1  mycroft 
    308  1.1  mycroft 	endva = round_page(va + length);
    309  1.1  mycroft 	for (; va < endva ; va += NBPG) {
    310  1.1  mycroft 		phys = trunc_page(pmap_extract(pmap_kernel(), va));
    311  1.1  mycroft 		if (phys == 0)
    312  1.4  mycroft 			panic("at_dma_rangecheck: no physical page present");
    313  1.1  mycroft 		if (phys >= (1<<24))
    314  1.1  mycroft 			return 1;
    315  1.1  mycroft 		if (priorpage) {
    316  1.1  mycroft 			if (priorpage + NBPG != phys)
    317  1.1  mycroft 				return 1;
    318  1.1  mycroft 			/* check if crossing a DMA page boundary */
    319  1.1  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    320  1.1  mycroft 				return 1;
    321  1.1  mycroft 		}
    322  1.1  mycroft 		priorpage = phys;
    323  1.1  mycroft 	}
    324  1.1  mycroft 	return 0;
    325  1.1  mycroft }
    326