Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.3
      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.3  mycroft  *	$Id: isadma.c,v 1.3 1993/10/17 05:34:23 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.1  mycroft  * at_dma_cascade(): program 8237 DMA controller channel to accept
     90  1.1  mycroft  * external dma control by a board.
     91  1.1  mycroft  */
     92  1.1  mycroft void
     93  1.1  mycroft at_dma_cascade(chan)
     94  1.1  mycroft 	unsigned chan;
     95  1.1  mycroft {
     96  1.1  mycroft #ifdef DIAGNOSTIC
     97  1.1  mycroft 	if (chan > 7)
     98  1.1  mycroft 		panic("at_dma_cascade: impossible request");
     99  1.1  mycroft #endif
    100  1.1  mycroft 
    101  1.1  mycroft 	/* set dma channel mode, and set dma channel mode */
    102  1.1  mycroft 	if ((chan & 4) == 0) {
    103  1.1  mycroft 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    104  1.1  mycroft 		outb(DMA1_SMSK, chan);
    105  1.1  mycroft 	} else {
    106  1.1  mycroft 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    107  1.1  mycroft 		outb(DMA2_SMSK, chan & 3);
    108  1.1  mycroft 	}
    109  1.1  mycroft }
    110  1.1  mycroft 
    111  1.1  mycroft /*
    112  1.1  mycroft  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    113  1.1  mycroft  * problems by using a bounce buffer.
    114  1.1  mycroft  */
    115  1.1  mycroft void
    116  1.1  mycroft at_dma(read, addr, nbytes, chan)
    117  1.1  mycroft 	int read;
    118  1.1  mycroft 	caddr_t addr;
    119  1.1  mycroft 	vm_size_t nbytes;
    120  1.1  mycroft 	unsigned chan;
    121  1.1  mycroft {
    122  1.1  mycroft 	vm_offset_t phys;
    123  1.1  mycroft 	int waport;
    124  1.1  mycroft 	caddr_t newaddr;
    125  1.1  mycroft 
    126  1.1  mycroft 	if (chan > 7 ||
    127  1.1  mycroft 	    (chan < 4 && nbytes > (1<<16)) ||
    128  1.2  mycroft 	    (chan >= 4 && (nbytes > (1<<17) || nbytes & 1 || (u_int)addr & 1)))
    129  1.1  mycroft 		panic("at_dma: impossible request");
    130  1.1  mycroft 
    131  1.1  mycroft 	if (at_dma_rangecheck((vm_offset_t)addr, nbytes, chan)) {
    132  1.1  mycroft 		/* XXX totally braindead; NBPG is not enough */
    133  1.1  mycroft 		if (bouncebuf[chan] == 0)
    134  1.1  mycroft 			bouncebuf[chan] =
    135  1.1  mycroft 				(caddr_t) isaphysmem + NBPG*chan;
    136  1.1  mycroft 		bouncesize[chan] = nbytes;
    137  1.1  mycroft 		newaddr = bouncebuf[chan];
    138  1.1  mycroft 		/* copy bounce buffer on write */
    139  1.1  mycroft 		if (!read) {
    140  1.1  mycroft 			bcopy(addr, newaddr, nbytes);
    141  1.1  mycroft 			bounced[chan] = 0;
    142  1.1  mycroft 		} else
    143  1.1  mycroft 			bounced[chan] = addr;
    144  1.1  mycroft 		addr = newaddr;
    145  1.1  mycroft 	}
    146  1.1  mycroft 
    147  1.1  mycroft 	/* translate to physical */
    148  1.1  mycroft 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    149  1.1  mycroft 
    150  1.1  mycroft 	if ((chan & 4) == 0) {
    151  1.1  mycroft 		/*
    152  1.1  mycroft 		 * Program one of DMA channels 0..3.  These are
    153  1.1  mycroft 		 * byte mode channels.
    154  1.1  mycroft 		 */
    155  1.1  mycroft 		/* set dma channel mode, and reset address ff */
    156  1.1  mycroft 		if (read)
    157  1.1  mycroft 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    158  1.1  mycroft 		else
    159  1.1  mycroft 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    160  1.1  mycroft 		outb(DMA1_FFC, 0);
    161  1.1  mycroft 
    162  1.1  mycroft 		/* send start address */
    163  1.1  mycroft 		waport =  DMA1_CHN(chan);
    164  1.1  mycroft 		outb(waport, phys);
    165  1.1  mycroft 		outb(waport, phys>>8);
    166  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    167  1.1  mycroft 
    168  1.1  mycroft 		/* send count */
    169  1.1  mycroft 		outb(waport + 1, --nbytes);
    170  1.1  mycroft 		outb(waport + 1, nbytes>>8);
    171  1.1  mycroft 
    172  1.1  mycroft 		/* unmask channel */
    173  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_CLEAR | chan);
    174  1.1  mycroft 	} else {
    175  1.1  mycroft 		/*
    176  1.1  mycroft 		 * Program one of DMA channels 4..7.  These are
    177  1.1  mycroft 		 * word mode channels.
    178  1.1  mycroft 		 */
    179  1.1  mycroft 		/* set dma channel mode, and reset address ff */
    180  1.1  mycroft 		if (read)
    181  1.1  mycroft 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    182  1.1  mycroft 		else
    183  1.1  mycroft 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    184  1.1  mycroft 		outb(DMA2_FFC, 0);
    185  1.1  mycroft 
    186  1.1  mycroft 		/* send start address */
    187  1.1  mycroft 		waport = DMA2_CHN(chan & 3);
    188  1.1  mycroft 		outb(waport, phys>>1);
    189  1.1  mycroft 		outb(waport, phys>>9);
    190  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    191  1.1  mycroft 
    192  1.1  mycroft 		/* send count */
    193  1.1  mycroft 		nbytes >>= 1;
    194  1.1  mycroft 		outb(waport + 2, --nbytes);
    195  1.1  mycroft 		outb(waport + 2, nbytes>>8);
    196  1.1  mycroft 
    197  1.1  mycroft 		/* unmask channel */
    198  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_CLEAR | (chan & 3));
    199  1.1  mycroft 	}
    200  1.1  mycroft }
    201  1.1  mycroft 
    202  1.1  mycroft /*
    203  1.1  mycroft  * Abort a DMA request, clearing the bounce buffer, if any.
    204  1.1  mycroft  */
    205  1.1  mycroft void
    206  1.1  mycroft at_dma_abort(chan)
    207  1.1  mycroft 	unsigned chan;
    208  1.1  mycroft {
    209  1.1  mycroft 
    210  1.1  mycroft #ifdef DIAGNOSTIC
    211  1.1  mycroft 	if (chan > 7)
    212  1.1  mycroft 		panic("at_dma_abort: impossible request");
    213  1.1  mycroft #endif
    214  1.1  mycroft 
    215  1.1  mycroft 	bounced[chan] = 0;
    216  1.1  mycroft 
    217  1.1  mycroft 	/* mask channel */
    218  1.1  mycroft 	if ((chan & 4) == 0)
    219  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    220  1.1  mycroft 	else
    221  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    222  1.1  mycroft }
    223  1.1  mycroft 
    224  1.1  mycroft /*
    225  1.1  mycroft  * End a DMA request, copying data from the bounce buffer, if any,
    226  1.1  mycroft  * when reading.
    227  1.1  mycroft  */
    228  1.1  mycroft void
    229  1.1  mycroft at_dma_terminate(chan)
    230  1.1  mycroft 	unsigned chan;
    231  1.1  mycroft {
    232  1.1  mycroft 	u_char tc;
    233  1.1  mycroft 
    234  1.1  mycroft #ifdef DIAGNOSTIC
    235  1.1  mycroft 	if (chan > 7)
    236  1.1  mycroft 		panic("at_dma_terminate: impossible request");
    237  1.1  mycroft #endif
    238  1.1  mycroft 
    239  1.1  mycroft 	/* check that the terminal count was reached */
    240  1.1  mycroft 	if ((chan & 4) == 0)
    241  1.1  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    242  1.1  mycroft 	else
    243  1.1  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    244  1.1  mycroft 	if (tc == 0)
    245  1.1  mycroft 		/* XXX probably should panic or something */
    246  1.1  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    247  1.1  mycroft 
    248  1.1  mycroft 	/* copy bounce buffer on read */
    249  1.1  mycroft 	if (bounced[chan]) {
    250  1.1  mycroft 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    251  1.1  mycroft 		bounced[chan] = 0;
    252  1.1  mycroft 	}
    253  1.1  mycroft 
    254  1.1  mycroft 	/* mask channel */
    255  1.1  mycroft 	if ((chan & 4) == 0)
    256  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    257  1.1  mycroft 	else
    258  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    259  1.1  mycroft }
    260  1.1  mycroft 
    261  1.1  mycroft /*
    262  1.1  mycroft  * Check for problems with the address range of a DMA transfer
    263  1.1  mycroft  * (non-contiguous physical pages, outside of bus address space,
    264  1.1  mycroft  * crossing DMA page boundaries).
    265  1.1  mycroft  * Return true if special handling needed.
    266  1.1  mycroft  */
    267  1.1  mycroft int
    268  1.1  mycroft at_dma_rangecheck(va, length, chan)
    269  1.1  mycroft 	vm_offset_t va;
    270  1.1  mycroft 	unsigned length;
    271  1.1  mycroft 	unsigned chan;
    272  1.1  mycroft {
    273  1.1  mycroft 	vm_offset_t phys, priorpage = 0, endva;
    274  1.1  mycroft 	u_int dma_pgmsk = (chan&4) ?  ~(128*1024-1) : ~(64*1024-1);
    275  1.1  mycroft 
    276  1.1  mycroft 	endva = round_page(va + length);
    277  1.1  mycroft 	for (; va < endva ; va += NBPG) {
    278  1.1  mycroft 		phys = trunc_page(pmap_extract(pmap_kernel(), va));
    279  1.1  mycroft 		if (phys == 0)
    280  1.1  mycroft 			panic("isa_dmacheck: no physical page present");
    281  1.1  mycroft 		if (phys >= (1<<24))
    282  1.1  mycroft 			return 1;
    283  1.1  mycroft 		if (priorpage) {
    284  1.1  mycroft 			if (priorpage + NBPG != phys)
    285  1.1  mycroft 				return 1;
    286  1.1  mycroft 			/* check if crossing a DMA page boundary */
    287  1.1  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    288  1.1  mycroft 				return 1;
    289  1.1  mycroft 		}
    290  1.1  mycroft 		priorpage = phys;
    291  1.1  mycroft 	}
    292  1.1  mycroft 	return 0;
    293  1.1  mycroft }
    294