Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.18.58.1
      1 /*	$NetBSD: dma.c,v 1.18.58.1 2009/05/13 17:16:22 jym 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/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.18.58.1 2009/05/13 17:16:22 jym Exp $");
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/kernel.h>
     60 #include <sys/proc.h>
     61 #include <sys/queue.h>
     62 
     63 #include <machine/cpu.h>
     64 #include <machine/iomap.h>
     65 #include <machine/dma.h>
     66 
     67 #include <atari/atari/intr.h>
     68 
     69 #define	NDMA_DEV	10	/* Max 2 floppy's, 8 hard-disks		*/
     70 typedef struct dma_entry {
     71 	TAILQ_ENTRY(dma_entry)	entries;	/* List pointers	   */
     72 	void		(*call_func)(void *);	/* Call when lock granted  */
     73 	void		(*int_func)(void *);	/* Call on DMA interrupt   */
     74 	void		*softc;			/* Arg. to int_func	   */
     75 	int		*lock_stat;		/* status of DMA lock	   */
     76 } DMA_ENTRY;
     77 
     78 /*
     79  * Preallocated entries. An allocator seem an overkill here.
     80  */
     81 static	DMA_ENTRY dmatable[NDMA_DEV];	/* preallocated entries		*/
     82 
     83 /*
     84  * Heads of free and active lists:
     85  */
     86 static  TAILQ_HEAD(freehead, dma_entry)	dma_free;
     87 static  TAILQ_HEAD(acthead, dma_entry)	dma_active;
     88 
     89 static	int	must_init = 1;		/* Must initialize		*/
     90 
     91 int	cdmaint(void *, int);
     92 
     93 static	void	st_dma_init(void);
     94 
     95 static void
     96 st_dma_init(void)
     97 {
     98 	int	i;
     99 
    100 	TAILQ_INIT(&dma_free);
    101 	TAILQ_INIT(&dma_active);
    102 
    103 	for(i = 0; i < NDMA_DEV; i++)
    104 		TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
    105 
    106 	if (intr_establish(7, USER_VEC, 0, cdmaint, NULL) == NULL)
    107 		panic("st_dma_init: Can't establish interrupt");
    108 }
    109 
    110 int
    111 st_dmagrab(dma_farg int_func, dma_farg call_func, void *softc, int *lock_stat, int rcaller)
    112 {
    113 	int		sps;
    114 	DMA_ENTRY	*req;
    115 
    116 	if(must_init) {
    117 		st_dma_init();
    118 		must_init = 0;
    119 	}
    120 	*lock_stat = DMA_LOCK_REQ;
    121 
    122 	sps = splhigh();
    123 
    124 	/*
    125 	 * Create a request...
    126 	 */
    127 	if(dma_free.tqh_first == NULL)
    128 		panic("st_dmagrab: Too many outstanding requests");
    129 	req = dma_free.tqh_first;
    130 	TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
    131 	req->call_func = call_func;
    132 	req->int_func  = int_func;
    133 	req->softc     = softc;
    134 	req->lock_stat = lock_stat;
    135 	TAILQ_INSERT_TAIL(&dma_active, req, entries);
    136 
    137 	if(dma_active.tqh_first != req) {
    138 		if (call_func == NULL) {
    139 			do {
    140 				tsleep(&dma_active, PRIBIO, "dmalck", 0);
    141 			} while (*req->lock_stat != DMA_LOCK_GRANT);
    142 			splx(sps);
    143 			return(1);
    144 		}
    145 		splx(sps);
    146 		return(0);
    147 	}
    148 	splx(sps);
    149 
    150 	/*
    151 	 * We're at the head of the queue, ergo: we got the lock.
    152 	 */
    153 	*lock_stat = DMA_LOCK_GRANT;
    154 
    155 	if(rcaller || (call_func == NULL)) {
    156 		/*
    157 		 * Just return to caller immediately without going
    158 		 * through 'call_func' first.
    159 		 */
    160 		return(1);
    161 	}
    162 
    163 	(*call_func)(softc);	/* Call followup function		*/
    164 	return(0);
    165 }
    166 
    167 void
    168 st_dmafree(void *softc, int *lock_stat)
    169 {
    170 	int		sps;
    171 	DMA_ENTRY	*req;
    172 
    173 	sps = splhigh();
    174 
    175 	/*
    176 	 * Some validity checks first.
    177 	 */
    178 	if((req = dma_active.tqh_first) == NULL)
    179 		panic("st_dmafree: empty active queue");
    180 	if(req->softc != softc)
    181 		printf("Caller of st_dmafree is not lock-owner!\n");
    182 
    183 	/*
    184 	 * Clear lock status, move request from active to free queue.
    185 	 */
    186 	*lock_stat = 0;
    187 	TAILQ_REMOVE(&dma_active, req, entries);
    188 	TAILQ_INSERT_HEAD(&dma_free, req, entries);
    189 
    190 	if((req = dma_active.tqh_first) != NULL) {
    191 		*req->lock_stat = DMA_LOCK_GRANT;
    192 
    193 		if (req->call_func == NULL)
    194 			wakeup((void *)&dma_active);
    195 		else {
    196 		    /*
    197 		     * Call next request through softint handler. This avoids
    198 		     * spl-conflicts.
    199 		     */
    200 		    add_sicallback((si_farg)req->call_func, req->softc, 0);
    201 		}
    202 	}
    203 	splx(sps);
    204 	return;
    205 }
    206 
    207 int
    208 st_dmawanted(void)
    209 {
    210 	return(dma_active.tqh_first->entries.tqe_next != NULL);
    211 }
    212 
    213 int
    214 cdmaint(void *unused, int sr)
    215 	/* sr:	 sr at time of interrupt */
    216 {
    217 	dma_farg	int_func;
    218 	void		*softc;
    219 
    220 	if(dma_active.tqh_first != NULL) {
    221 		/*
    222 		 * Due to the logic of the ST-DMA chip, it is not possible to
    223 		 * check for stray interrupts here...
    224 		 */
    225 		int_func = dma_active.tqh_first->int_func;
    226 		softc    = dma_active.tqh_first->softc;
    227 
    228 		if(!BASEPRI(sr))
    229 			add_sicallback((si_farg)int_func, softc, 0);
    230 		else {
    231 			spl1();
    232 			(*int_func)(softc);
    233 			spl0();
    234 		}
    235 		return 1;
    236 	}
    237 	return 0;
    238 }
    239 
    240 /*
    241  * Setup address for DMA-transfer.
    242  * Note: The order _is_ important!
    243  */
    244 void
    245 st_dmaaddr_set(void * address)
    246 {
    247 	register u_long ad = (u_long)address;
    248 
    249 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
    250 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
    251 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
    252 }
    253 
    254 /*
    255  * Get address from DMA unit.
    256  */
    257 u_long
    258 st_dmaaddr_get(void)
    259 {
    260 	register u_long ad = 0;
    261 
    262 	ad  = (DMA->dma_addr[AD_LOW ] & 0xff);
    263 	ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
    264 	ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
    265 	return(ad);
    266 }
    267 
    268 /*
    269  * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
    270  * The DMA_WRBIT trick flushes the FIFO before doing DMA.
    271  */
    272 void
    273 st_dmacomm(int mode, int nblk)
    274 {
    275 	DMA->dma_mode = mode;
    276 	DMA->dma_mode = mode ^ DMA_WRBIT;
    277 	DMA->dma_mode = mode;
    278 	DMA->dma_data = nblk;
    279 	delay(2);	/* Needed for Falcon */
    280 	DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
    281 }
    282