Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.8
      1 /*	$NetBSD: dma.c,v 1.8 1996/06/18 11:10:11 leo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * This file contains special code dealing with the DMA interface
     35  * on the Atari ST.
     36  *
     37  * The DMA circuitry requires some special treatment for the peripheral
     38  * devices which make use of the ST's DMA feature (the hard disk and the
     39  * floppy drive).
     40  * All devices using DMA need mutually exclusive access and can follow some
     41  * standard pattern which will be provided in this file.
     42  *
     43  * The file contains the following entry points:
     44  *
     45  *	st_dmagrab:	ensure exclusive access to the DMA circuitry
     46  *	st_dmafree:	free exclusive access to the DMA circuitry
     47  *	st_dmawanted:	somebody is queued waiting for DMA-access
     48  *	dmaint:		DMA interrupt routine, switches to the current driver
     49  *	st_dmaaddr_set:	specify 24 bit RAM address
     50  *	st_dmaaddr_get:	get address of last DMA-op
     51  *	st_dmacomm:	program DMA, flush FIFO first
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/queue.h>
     58 #include <machine/cpu.h>
     59 #include <machine/iomap.h>
     60 #include <machine/dma.h>
     61 
     62 #define	NDMA_DEV	10	/* Max 2 floppy's, 8 hard-disks		*/
     63 typedef struct dma_entry {
     64 	TAILQ_ENTRY(dma_entry)	entries;	/* List pointers	   */
     65 	void		(*call_func)(void *);	/* Call when lock granted  */
     66 	void		(*int_func)(void *);	/* Call on DMA interrupt   */
     67 	void		*softc;			/* Arg. to int_func	   */
     68 	int		*lock_stat;		/* status of DMA lock	   */
     69 } DMA_ENTRY;
     70 
     71 /*
     72  * Preallocated entries. An allocator seem an overkill here.
     73  */
     74 static	DMA_ENTRY dmatable[NDMA_DEV];	/* preallocated entries		*/
     75 
     76 /*
     77  * Heads of free and active lists:
     78  */
     79 static  TAILQ_HEAD(freehead, dma_entry)	dma_free;
     80 static  TAILQ_HEAD(acthead, dma_entry)	dma_active;
     81 
     82 static	int	must_init = 1;		/* Must initialize		*/
     83 
     84 void	cdmaint __P((int));
     85 
     86 long	sr;	/* sr at time of interrupt */
     87 static	void	init_queues __P((void));
     88 
     89 static void
     90 init_queues()
     91 {
     92 	int	i;
     93 
     94 	TAILQ_INIT(&dma_free);
     95 	TAILQ_INIT(&dma_active);
     96 
     97 	for(i = 0; i < NDMA_DEV; i++)
     98 		TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
     99 }
    100 
    101 int
    102 st_dmagrab(int_func, call_func, softc, lock_stat, rcaller)
    103 dma_farg	int_func;
    104 dma_farg 	call_func;
    105 void		*softc;
    106 int		*lock_stat;
    107 int		rcaller;
    108 {
    109 	int		sps;
    110 	DMA_ENTRY	*req;
    111 
    112 	if(must_init) {
    113 		init_queues();
    114 		must_init = 0;
    115 	}
    116 	*lock_stat = DMA_LOCK_REQ;
    117 
    118 	sps = splhigh();
    119 
    120 	/*
    121 	 * Create a request...
    122 	 */
    123 	if(dma_free.tqh_first == NULL)
    124 		panic("st_dmagrab: Too many outstanding requests\n");
    125 	req = dma_free.tqh_first;
    126 	TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
    127 	req->call_func = call_func;
    128 	req->int_func  = int_func;
    129 	req->softc     = softc;
    130 	req->lock_stat = lock_stat;
    131 	TAILQ_INSERT_TAIL(&dma_active, req, entries);
    132 
    133 	if(dma_active.tqh_first != req) {
    134 		splx(sps);
    135 		return(0);
    136 	}
    137 	splx(sps);
    138 
    139 	/*
    140 	 * We're at the head of the queue, ergo: we got the lock.
    141 	 */
    142 	*lock_stat = DMA_LOCK_GRANT;
    143 
    144 	if(rcaller) {
    145 		/*
    146 		 * Just return to caller immediately without going
    147 		 * through 'call_func' first.
    148 		 */
    149 		return(1);
    150 	}
    151 
    152 	(*call_func)(softc);	/* Call followup function		*/
    153 	return(0);
    154 }
    155 
    156 void
    157 st_dmafree(softc, lock_stat)
    158 void	*softc;
    159 int	*lock_stat;
    160 {
    161 	int		sps;
    162 	DMA_ENTRY	*req;
    163 
    164 	sps = splhigh();
    165 
    166 	/*
    167 	 * Some validity checks first.
    168 	 */
    169 	if((req = dma_active.tqh_first) == NULL)
    170 		panic("st_dmafree: empty active queue\n");
    171 	if(req->softc != softc)
    172 		printf("Caller of st_dmafree is not lock-owner!\n");
    173 
    174 	/*
    175 	 * Clear lock status, move request from active to free queue.
    176 	 */
    177 	*lock_stat = 0;
    178 	TAILQ_REMOVE(&dma_active, req, entries);
    179 	TAILQ_INSERT_HEAD(&dma_free, req, entries);
    180 
    181 	if((req = dma_active.tqh_first) != NULL) {
    182 		/*
    183 		 * Call next request through softint handler. This avoids
    184 		 * spl-conflicts.
    185 		 */
    186 		*req->lock_stat = DMA_LOCK_GRANT;
    187 		add_sicallback((si_farg)req->call_func, req->softc, 0);
    188 	}
    189 	splx(sps);
    190 	return;
    191 }
    192 
    193 int
    194 st_dmawanted()
    195 {
    196 	return(dma_active.tqh_first->entries.tqe_next != NULL);
    197 }
    198 
    199 void
    200 cdmaint(sr)
    201 int	sr;	/* sr at time of interrupt */
    202 {
    203 	dma_farg	int_func;
    204 	void		*softc;
    205 
    206 	if(dma_active.tqh_first != NULL) {
    207 		int_func = dma_active.tqh_first->int_func;
    208 		softc    = dma_active.tqh_first->softc;
    209 
    210 		if(!BASEPRI(sr))
    211 			add_sicallback((si_farg)int_func, softc, 0);
    212 		else {
    213 			spl1();
    214 			(*int_func)(softc);
    215 		}
    216 	}
    217 	else printf("DMA interrupt discarded\n");
    218 }
    219 
    220 /*
    221  * Setup address for DMA-transfer.
    222  * Note: The order _is_ important!
    223  */
    224 void
    225 st_dmaaddr_set(address)
    226 caddr_t	address;
    227 {
    228 	register u_long ad = (u_long)address;
    229 
    230 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
    231 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
    232 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
    233 }
    234 
    235 /*
    236  * Get address from DMA unit.
    237  */
    238 u_long
    239 st_dmaaddr_get()
    240 {
    241 	register u_long ad = 0;
    242 
    243 	ad  = (DMA->dma_addr[AD_LOW ] & 0xff);
    244 	ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
    245 	ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
    246 	return(ad);
    247 }
    248 
    249 /*
    250  * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
    251  * The DMA_WRBIT trick flushes the FIFO before doing DMA.
    252  */
    253 void
    254 st_dmacomm(mode, nblk)
    255 int	mode, nblk;
    256 {
    257 	DMA->dma_mode = mode;
    258 	DMA->dma_mode = mode ^ DMA_WRBIT;
    259 	DMA->dma_mode = mode;
    260 	DMA->dma_data = nblk;
    261 	delay(2);	/* Needed for Falcon */
    262 	DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
    263 }
    264