Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.2
      1  1.2  leo /*	$NetBSD: dma.c,v 1.2 1995/04/22 22:18:17 leo Exp $	*/
      2  1.1  leo 
      3  1.1  leo /*
      4  1.1  leo  * Copyright (c) 1995 Leo Weppelman.
      5  1.1  leo  * All rights reserved.
      6  1.1  leo  *
      7  1.1  leo  * Redistribution and use in source and binary forms, with or without
      8  1.1  leo  * modification, are permitted provided that the following conditions
      9  1.1  leo  * are met:
     10  1.1  leo  * 1. Redistributions of source code must retain the above copyright
     11  1.1  leo  *    notice, this list of conditions and the following disclaimer.
     12  1.1  leo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  leo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  leo  *    documentation and/or other materials provided with the distribution.
     15  1.1  leo  * 3. All advertising materials mentioning features or use of this software
     16  1.1  leo  *    must display the following acknowledgement:
     17  1.1  leo  *      This product includes software developed by Leo Weppelman.
     18  1.1  leo  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  leo  *    derived from this software without specific prior written permission
     20  1.1  leo  *
     21  1.1  leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  leo  */
     32  1.1  leo 
     33  1.1  leo /*
     34  1.1  leo  * This file contains special code dealing with the DMA interface
     35  1.1  leo  * on the Atari ST.
     36  1.1  leo  *
     37  1.1  leo  * The DMA circuitry requires some special treatment for the peripheral
     38  1.1  leo  * devices which make use of the ST's DMA feature (the hard disk and the
     39  1.1  leo  * floppy drive).
     40  1.1  leo  * All devices using DMA need mutually exclusive access and can follow some
     41  1.1  leo  * standard pattern which will be provided in this file.
     42  1.1  leo  *
     43  1.1  leo  * The file contains the following entry points:
     44  1.1  leo  *
     45  1.2  leo  *	st_dmagrab:	ensure exclusive access to the DMA circuitry
     46  1.2  leo  *	st_dmafree:	free exclusive access to the DMA circuitry
     47  1.2  leo  *	st_dmawanted:	somebody is queued waiting for DMA-access
     48  1.1  leo  *	dmaint:		DMA interrupt routine, switches to the current driver
     49  1.2  leo  *	st_dmaaddr:	specify 24 bit RAM address
     50  1.2  leo  *	st_dmacomm:	program DMA, flush FIFO first
     51  1.1  leo  */
     52  1.1  leo 
     53  1.1  leo #include <sys/param.h>
     54  1.1  leo #include <sys/systm.h>
     55  1.1  leo #include <sys/kernel.h>
     56  1.1  leo #include <machine/cpu.h>
     57  1.1  leo #include <machine/iomap.h>
     58  1.1  leo #include <machine/dma.h>
     59  1.1  leo 
     60  1.1  leo #define	NDMA_DEV	10	/* Max 2 floppy's, 8 hard-disks		*/
     61  1.1  leo typedef struct {
     62  1.2  leo 	void	(*call_func)();
     63  1.2  leo 	void	(*int_func)();
     64  1.1  leo 	void	*softc;
     65  1.1  leo } DMA_DISP;
     66  1.1  leo 
     67  1.1  leo static	DMA_DISP  dispatch[NDMA_DEV];	/* dispatch table		     */
     68  1.1  leo static	int	  dma_free = 0;		/* next free entry in dispatch table */
     69  1.1  leo static	int	  dma_curr = 0;		/* current entry in dispatch table   */
     70  1.1  leo static	int	  dmalock = 0;		/* if != 0, dma is not free	     */
     71  1.2  leo static	void	  (*xxxint)();		/* current interrupt function	     */
     72  1.1  leo static	void	  *dma_softc;		/* Device currently owning DMA-intr  */
     73  1.1  leo static	int	  sched_soft = 0;	/* Software interrupt scheduled	     */
     74  1.1  leo 
     75  1.1  leo static	void	cdmasoft __P((void));
     76  1.1  leo 
     77  1.2  leo int st_dmagrab(int_func, call_func, softc, rcaller)
     78  1.2  leo void 	(*int_func)();
     79  1.2  leo void 	(*call_func)();
     80  1.1  leo void	*softc;
     81  1.2  leo int	rcaller;
     82  1.1  leo {
     83  1.1  leo 	int		sps;
     84  1.1  leo 	DMA_DISP	*disp;
     85  1.1  leo 
     86  1.1  leo 	sps = splbio();
     87  1.1  leo 
     88  1.1  leo 	if(dmalock) {
     89  1.1  leo 		disp = &dispatch[dma_free++];
     90  1.1  leo 		if(dma_free >= NDMA_DEV)
     91  1.1  leo 			dma_free = 0;
     92  1.1  leo 		if(disp->call_func != NULL)
     93  1.1  leo 			panic("dma dispatch table overflow");
     94  1.1  leo 		disp->call_func = call_func;
     95  1.1  leo 		disp->int_func  = int_func;
     96  1.1  leo 		disp->softc     = softc;
     97  1.1  leo 		splx(sps);
     98  1.2  leo 		return(0);
     99  1.1  leo 	}
    100  1.1  leo 	dmalock++;
    101  1.1  leo 	xxxint    = int_func;	/* Grab DMA interrupts			*/
    102  1.1  leo 	dma_softc = softc;	/* Identify device which got DMA	*/
    103  1.2  leo 	if(rcaller) {
    104  1.2  leo 		/*
    105  1.2  leo 		 * Just return to caller immediately without going
    106  1.2  leo 		 * through 'call_func' first.
    107  1.2  leo 		 */
    108  1.2  leo 		return(1);
    109  1.2  leo 	}
    110  1.2  leo 
    111  1.1  leo 	(*call_func)(softc);	/* Call followup function		*/
    112  1.1  leo 	splx(sps);
    113  1.2  leo 	return(0);
    114  1.1  leo }
    115  1.1  leo 
    116  1.2  leo void
    117  1.2  leo st_dmafree()
    118  1.1  leo {
    119  1.1  leo 	int		sps;
    120  1.1  leo 	DMA_DISP	*disp;
    121  1.1  leo 
    122  1.1  leo 	sps = splbio();
    123  1.1  leo 	disp = &dispatch[dma_curr];
    124  1.1  leo 	if(disp->call_func != NULL) {
    125  1.1  leo 		xxxint    = disp->int_func;
    126  1.1  leo 		dma_softc = disp->softc;
    127  1.1  leo 		(*disp->call_func)(dma_softc);
    128  1.1  leo 		disp->call_func = NULL;
    129  1.1  leo 		if(++dma_curr >= NDMA_DEV)
    130  1.1  leo 			dma_curr = 0;
    131  1.1  leo 		splx(sps);
    132  1.1  leo 		return;
    133  1.1  leo 	}
    134  1.1  leo 	dmalock = 0;
    135  1.1  leo 	xxxint = NULL;		/* no more DMA interrupts */
    136  1.1  leo 	splx(sps);
    137  1.1  leo }
    138  1.1  leo 
    139  1.2  leo int
    140  1.2  leo st_dmawanted()
    141  1.2  leo {
    142  1.2  leo 	return(dispatch[dma_curr].call_func != NULL);
    143  1.2  leo }
    144  1.2  leo 
    145  1.1  leo cdmaint(sr)
    146  1.1  leo long	sr;	/* sr at time of interrupt */
    147  1.1  leo {
    148  1.1  leo 	if(xxxint != NULL) {
    149  1.1  leo 		if(!BASEPRI(sr)) {
    150  1.1  leo 			if(!sched_soft++)
    151  1.1  leo 				add_sicallback(cdmasoft, 0, 0);
    152  1.1  leo 		}
    153  1.1  leo 		else {
    154  1.1  leo 			spl1();
    155  1.1  leo 			cdmasoft();
    156  1.1  leo 		}
    157  1.1  leo 	}
    158  1.1  leo 	else printf("DMA interrupt discarded\r\n");
    159  1.1  leo }
    160  1.1  leo 
    161  1.1  leo static void cdmasoft()
    162  1.1  leo {
    163  1.1  leo 	sched_soft = 0;
    164  1.1  leo 	(*xxxint)(dma_softc);
    165  1.1  leo }
    166  1.1  leo 
    167  1.2  leo /*
    168  1.2  leo  * Setup address for DMA-transfer.
    169  1.2  leo  * Note: The order _is_ important!
    170  1.2  leo  */
    171  1.2  leo void
    172  1.2  leo st_dmaaddr(address)
    173  1.1  leo caddr_t	address;
    174  1.1  leo {
    175  1.1  leo 	register u_long ad = (u_long)address;
    176  1.1  leo 
    177  1.1  leo 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
    178  1.1  leo 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
    179  1.1  leo 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
    180  1.1  leo }
    181  1.1  leo 
    182  1.2  leo /*
    183  1.2  leo  * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
    184  1.2  leo  * The DMA_WRBIT trick flushes the FIFO before doing DMA.
    185  1.2  leo  */
    186  1.2  leo void
    187  1.2  leo st_dmacomm(mode, data)
    188  1.2  leo int	mode, data;
    189  1.1  leo {
    190  1.1  leo 	DMA->dma_mode = mode;
    191  1.2  leo 	DMA->dma_mode = mode ^ DMA_WRBIT;
    192  1.1  leo 	DMA->dma_mode = mode;
    193  1.1  leo 	DMA->dma_data = data;
    194  1.1  leo }
    195