Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.1.1.1
      1  1.1  leo /*	$NetBSD: dma.c,v 1.1.1.1 1995/03/26 07:12:13 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.1  leo  *	dmagrab:	ensure exclusive access to the DMA circuitry
     46  1.1  leo  *	dmafree:	free exclusive access to the DMA circuitry
     47  1.1  leo  *	dmaint:		DMA interrupt routine, switches to the current driver
     48  1.1  leo  *	dmaaddr:	specify 24 bit RAM address
     49  1.1  leo  *	dmardat:	set dma_mode and read word from dma_data
     50  1.1  leo  *	dmawdat:	set dma_mode and write word to dma_data
     51  1.1  leo  *	dmacomm:	like dmawdat, but first toggle WRBIT
     52  1.1  leo  *
     53  1.1  leo  * FIXME: 	The delay-loops should be done otherwise!
     54  1.1  leo  */
     55  1.1  leo 
     56  1.1  leo #include <sys/param.h>
     57  1.1  leo #include <sys/systm.h>
     58  1.1  leo #include <sys/kernel.h>
     59  1.1  leo #include <machine/cpu.h>
     60  1.1  leo #include <machine/iomap.h>
     61  1.1  leo #include <machine/dma.h>
     62  1.1  leo 
     63  1.1  leo #define	NDMA_DEV	10	/* Max 2 floppy's, 8 hard-disks		*/
     64  1.1  leo typedef struct {
     65  1.1  leo 	int	(*call_func)();
     66  1.1  leo 	int	(*int_func)();
     67  1.1  leo 	void	*softc;
     68  1.1  leo } DMA_DISP;
     69  1.1  leo 
     70  1.1  leo static	DMA_DISP  dispatch[NDMA_DEV];	/* dispatch table		     */
     71  1.1  leo static	int	  dma_free = 0;		/* next free entry in dispatch table */
     72  1.1  leo static	int	  dma_curr = 0;		/* current entry in dispatch table   */
     73  1.1  leo static	int	  dmalock = 0;		/* if != 0, dma is not free	     */
     74  1.1  leo static	int	  (*xxxint)();		/* current interrupt function	     */
     75  1.1  leo static	void	  *dma_softc;		/* Device currently owning DMA-intr  */
     76  1.1  leo static	int	  sched_soft = 0;	/* Software interrupt scheduled	     */
     77  1.1  leo 
     78  1.1  leo static	void	cdmasoft __P((void));
     79  1.1  leo 
     80  1.1  leo dmagrab(int_func, call_func, softc)
     81  1.1  leo int 	(*int_func)();
     82  1.1  leo int 	(*call_func)();
     83  1.1  leo void	*softc;
     84  1.1  leo {
     85  1.1  leo 	int		sps;
     86  1.1  leo 	DMA_DISP	*disp;
     87  1.1  leo 
     88  1.1  leo 	sps = splbio();
     89  1.1  leo 
     90  1.1  leo 	if(dmalock) {
     91  1.1  leo 		disp = &dispatch[dma_free++];
     92  1.1  leo 		if(dma_free >= NDMA_DEV)
     93  1.1  leo 			dma_free = 0;
     94  1.1  leo 		if(disp->call_func != NULL)
     95  1.1  leo 			panic("dma dispatch table overflow");
     96  1.1  leo 		disp->call_func = call_func;
     97  1.1  leo 		disp->int_func  = int_func;
     98  1.1  leo 		disp->softc     = softc;
     99  1.1  leo 		splx(sps);
    100  1.1  leo 		return;
    101  1.1  leo 	}
    102  1.1  leo 	dmalock++;
    103  1.1  leo 	xxxint    = int_func;	/* Grab DMA interrupts			*/
    104  1.1  leo 	dma_softc = softc;	/* Identify device which got DMA	*/
    105  1.1  leo 	(*call_func)(softc);	/* Call followup function		*/
    106  1.1  leo 	splx(sps);
    107  1.1  leo 
    108  1.1  leo }
    109  1.1  leo 
    110  1.1  leo dmafree()
    111  1.1  leo {
    112  1.1  leo 	int		sps;
    113  1.1  leo 	DMA_DISP	*disp;
    114  1.1  leo 
    115  1.1  leo 	sps = splbio();
    116  1.1  leo 	disp = &dispatch[dma_curr];
    117  1.1  leo 	if(disp->call_func != NULL) {
    118  1.1  leo 		xxxint    = disp->int_func;
    119  1.1  leo 		dma_softc = disp->softc;
    120  1.1  leo 		(*disp->call_func)(dma_softc);
    121  1.1  leo 		disp->call_func = NULL;
    122  1.1  leo 		if(++dma_curr >= NDMA_DEV)
    123  1.1  leo 			dma_curr = 0;
    124  1.1  leo 		splx(sps);
    125  1.1  leo 		return;
    126  1.1  leo 	}
    127  1.1  leo 	dmalock = 0;
    128  1.1  leo 	xxxint = NULL;		/* no more DMA interrupts */
    129  1.1  leo 	splx(sps);
    130  1.1  leo }
    131  1.1  leo 
    132  1.1  leo cdmaint(sr)
    133  1.1  leo long	sr;	/* sr at time of interrupt */
    134  1.1  leo {
    135  1.1  leo 	if(xxxint != NULL) {
    136  1.1  leo 		if(!BASEPRI(sr)) {
    137  1.1  leo 			if(!sched_soft++)
    138  1.1  leo 				add_sicallback(cdmasoft, 0, 0);
    139  1.1  leo 		}
    140  1.1  leo 		else {
    141  1.1  leo 			spl1();
    142  1.1  leo 			cdmasoft();
    143  1.1  leo 		}
    144  1.1  leo 	}
    145  1.1  leo 	else printf("DMA interrupt discarded\r\n");
    146  1.1  leo }
    147  1.1  leo 
    148  1.1  leo static void cdmasoft()
    149  1.1  leo {
    150  1.1  leo 	sched_soft = 0;
    151  1.1  leo 	(*xxxint)(dma_softc);
    152  1.1  leo }
    153  1.1  leo 
    154  1.1  leo dmaaddr(address)
    155  1.1  leo caddr_t	address;
    156  1.1  leo {
    157  1.1  leo 	register u_long ad = (u_long)address;
    158  1.1  leo 
    159  1.1  leo 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
    160  1.1  leo 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
    161  1.1  leo 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
    162  1.1  leo }
    163  1.1  leo 
    164  1.1  leo int
    165  1.1  leo dmardat(mode, delay)
    166  1.1  leo int	mode, delay;
    167  1.1  leo {
    168  1.1  leo 	while(--delay >= 0);
    169  1.1  leo 	DMA->dma_mode = mode;
    170  1.1  leo 	while(--delay >= 0);
    171  1.1  leo 	return(DMA->dma_data);
    172  1.1  leo }
    173  1.1  leo 
    174  1.1  leo dmawdat(mode, data, delay)
    175  1.1  leo int	mode, data, delay;
    176  1.1  leo {
    177  1.1  leo 	DMA->dma_mode = mode;
    178  1.1  leo 	while(--delay >= 0);
    179  1.1  leo 	DMA->dma_data = data;
    180  1.1  leo 	while(--delay >= 0);
    181  1.1  leo }
    182  1.1  leo 
    183  1.1  leo dmacomm(mode, data, delay)
    184  1.1  leo int	mode, data, delay;
    185  1.1  leo {
    186  1.1  leo 	DMA->dma_mode = mode;
    187  1.1  leo 	DMA->dma_mode = mode ^ WRBIT;
    188  1.1  leo 	DMA->dma_mode = mode;
    189  1.1  leo 	while(--delay >= 0);
    190  1.1  leo 	DMA->dma_data = data;
    191  1.1  leo 	while(--delay >= 0);
    192  1.1  leo }
    193  1.1  leo 
    194  1.1  leo int
    195  1.1  leo dmastat(mode, delay)
    196  1.1  leo int	mode, delay;
    197  1.1  leo {
    198  1.1  leo 	while(--delay >= 0);
    199  1.1  leo 	DMA->dma_mode = mode;
    200  1.1  leo 	while(--delay >= 0);
    201  1.1  leo 	return(DMA->dma_stat);
    202  1.1  leo }
    203