Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.7
      1 /*	$NetBSD: dma.c,v 1.7 1996/04/19 20:35:46 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 static	int	  sched_soft = 0;	/* callback scheduled		*/
     76 
     77 /*
     78  * Heads of free and active lists:
     79  */
     80 static  TAILQ_HEAD(freehead, dma_entry)	dma_free;
     81 static  TAILQ_HEAD(acthead, dma_entry)	dma_active;
     82 
     83 static	int	must_init = 1;		/* Must initialize		*/
     84 
     85 void	cdmaint __P((int));
     86 
     87 long	sr;	/* sr at time of interrupt */
     88 static	void	cdmasoft __P((void *, void *));
     89 static	void	init_queues __P((void));
     90 
     91 static void
     92 init_queues()
     93 {
     94 	int	i;
     95 
     96 	TAILQ_INIT(&dma_free);
     97 	TAILQ_INIT(&dma_active);
     98 
     99 	for(i = 0; i < NDMA_DEV; i++)
    100 		TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
    101 }
    102 
    103 int
    104 st_dmagrab(int_func, call_func, softc, lock_stat, rcaller)
    105 dma_farg	int_func;
    106 dma_farg 	call_func;
    107 void		*softc;
    108 int		*lock_stat;
    109 int		rcaller;
    110 {
    111 	int		sps;
    112 	DMA_ENTRY	*req;
    113 
    114 	if(must_init) {
    115 		init_queues();
    116 		must_init = 0;
    117 	}
    118 	*lock_stat = DMA_LOCK_REQ;
    119 
    120 	sps = splhigh();
    121 
    122 	/*
    123 	 * Create a request...
    124 	 */
    125 	if(dma_free.tqh_first == NULL)
    126 		panic("st_dmagrab: Too many outstanding requests\n");
    127 	req = dma_free.tqh_first;
    128 	TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
    129 	req->call_func = call_func;
    130 	req->int_func  = int_func;
    131 	req->softc     = softc;
    132 	req->lock_stat = lock_stat;
    133 	TAILQ_INSERT_TAIL(&dma_active, req, entries);
    134 
    135 	if(dma_active.tqh_first != req) {
    136 		splx(sps);
    137 		return(0);
    138 	}
    139 	splx(sps);
    140 
    141 	/*
    142 	 * We're at the head of the queue, ergo: we got the lock.
    143 	 */
    144 	*lock_stat = DMA_LOCK_GRANT;
    145 
    146 	if(rcaller) {
    147 		/*
    148 		 * Just return to caller immediately without going
    149 		 * through 'call_func' first.
    150 		 */
    151 		return(1);
    152 	}
    153 
    154 	(*call_func)(softc);	/* Call followup function		*/
    155 	return(0);
    156 }
    157 
    158 void
    159 st_dmafree(softc, lock_stat)
    160 void	*softc;
    161 int	*lock_stat;
    162 {
    163 	int		sps;
    164 	DMA_ENTRY	*req;
    165 
    166 	sps = splhigh();
    167 
    168 	/*
    169 	 * Some validity checks first.
    170 	 */
    171 	if((req = dma_active.tqh_first) == NULL)
    172 		panic("st_dmafree: empty active queue\n");
    173 	if(req->softc != softc)
    174 		printf("Caller of st_dmafree is not lock-owner!\n");
    175 
    176 	/*
    177 	 * Clear lock status, move request from active to free queue.
    178 	 */
    179 	*lock_stat = 0;
    180 	TAILQ_REMOVE(&dma_active, req, entries);
    181 	TAILQ_INSERT_HEAD(&dma_free, req, entries);
    182 
    183 	if((req = dma_active.tqh_first) != NULL) {
    184 		/*
    185 		 * Call next request through softint handler. This avoids
    186 		 * spl-conflicts.
    187 		 */
    188 		*req->lock_stat = DMA_LOCK_GRANT;
    189 		add_sicallback((si_farg)req->call_func, req->softc, 0);
    190 	}
    191 	splx(sps);
    192 	return;
    193 }
    194 
    195 int
    196 st_dmawanted()
    197 {
    198 	return(dma_active.tqh_first->entries.tqe_next != NULL);
    199 }
    200 
    201 void
    202 cdmaint(sr)
    203 int	sr;	/* sr at time of interrupt */
    204 {
    205 	if(dma_active.tqh_first != NULL) {
    206 		if(!BASEPRI(sr)) {
    207 			if(!sched_soft++)
    208 				add_sicallback(cdmasoft, 0, 0);
    209 		}
    210 		else {
    211 			spl1();
    212 			cdmasoft(NULL, NULL);
    213 		}
    214 	}
    215 	else printf("DMA interrupt discarded\n");
    216 }
    217 
    218 static void
    219 cdmasoft(arg1, arg2)
    220 void	*arg1, *arg2;
    221 {
    222 	int		s;
    223 	dma_farg	int_func;
    224 	void		*softc;
    225 
    226 	/*
    227 	 * Prevent a race condition here. DMA might be freed while
    228 	 * the callback was pending!
    229 	 */
    230 	s = splhigh();
    231 	sched_soft = 0;
    232 	if(dma_active.tqh_first != NULL) {
    233 		int_func = dma_active.tqh_first->int_func;
    234 		softc    = dma_active.tqh_first->softc;
    235 	}
    236 	else int_func = softc = NULL;
    237 	splx(s);
    238 
    239 	if(int_func != NULL)
    240 		(*int_func)(softc);
    241 }
    242 
    243 /*
    244  * Setup address for DMA-transfer.
    245  * Note: The order _is_ important!
    246  */
    247 void
    248 st_dmaaddr_set(address)
    249 caddr_t	address;
    250 {
    251 	register u_long ad = (u_long)address;
    252 
    253 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
    254 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
    255 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
    256 }
    257 
    258 /*
    259  * Get address from DMA unit.
    260  */
    261 u_long
    262 st_dmaaddr_get()
    263 {
    264 	register u_long ad = 0;
    265 
    266 	ad  = (DMA->dma_addr[AD_LOW ] & 0xff);
    267 	ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
    268 	ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
    269 	return(ad);
    270 }
    271 
    272 /*
    273  * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
    274  * The DMA_WRBIT trick flushes the FIFO before doing DMA.
    275  */
    276 void
    277 st_dmacomm(mode, nblk)
    278 int	mode, nblk;
    279 {
    280 	DMA->dma_mode = mode;
    281 	DMA->dma_mode = mode ^ DMA_WRBIT;
    282 	DMA->dma_mode = mode;
    283 	DMA->dma_data = nblk;
    284 	delay(2);	/* Needed for Falcon */
    285 	DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
    286 }
    287