xref: /netbsd-src/sys/arch/atari/dev/dma.c (revision 07bae7edddbb1ce4c926b2e8db425804589074c9)
1 /*	$NetBSD: dma.c,v 1.3 1995/04/30 12:04:48 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:	specify 24 bit RAM address
50  *	st_dmacomm:	program DMA, flush FIFO first
51  */
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/queue.h>
57 #include <machine/cpu.h>
58 #include <machine/iomap.h>
59 #include <machine/dma.h>
60 
61 #define	NDMA_DEV	10	/* Max 2 floppy's, 8 hard-disks		*/
62 typedef struct dma_entry {
63 	TAILQ_ENTRY(dma_entry)	entries;	/* List pointers	   */
64 	void			(*call_func)();	/* Call when lock granted  */
65 	void			(*int_func)();	/* Call on DMA interrupt   */
66 	void			*softc;		/* Arg. to int_func	   */
67 	int			*lock_stat;	/* status of DMA lock	   */
68 } DMA_ENTRY;
69 
70 /*
71  * Preallocated entries. An allocator seem an overkill here.
72  */
73 static	DMA_ENTRY dmatable[NDMA_DEV];	/* preallocated entries		*/
74 static	int	  sched_soft = 0;	/* callback scheduled		*/
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 static	void	cdmasoft __P((void));
85 static	void	init_queues __P((void));
86 
87 static void init_queues()
88 {
89 	int	i;
90 
91 	TAILQ_INIT(&dma_free);
92 	TAILQ_INIT(&dma_active);
93 
94 	for(i = 0; i < NDMA_DEV; i++)
95 		TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
96 }
97 
98 int st_dmagrab(int_func, call_func, softc, lock_stat, rcaller)
99 void 	(*int_func)();
100 void 	(*call_func)();
101 void	*softc;
102 int	*lock_stat;
103 int	rcaller;
104 {
105 	int		sps;
106 	DMA_ENTRY	*req;
107 
108 	if(must_init) {
109 		init_queues();
110 		must_init = 0;
111 	}
112 	*lock_stat = DMA_LOCK_REQ;
113 
114 	sps = splhigh();
115 
116 	/*
117 	 * Create a request...
118 	 */
119 	if(dma_free.tqh_first == NULL)
120 		panic("st_dmagrab: Too many outstanding requests\n");
121 	req = dma_free.tqh_first;
122 	TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
123 	req->call_func = call_func;
124 	req->int_func  = int_func;
125 	req->softc     = softc;
126 	req->lock_stat = lock_stat;
127 	TAILQ_INSERT_TAIL(&dma_active, req, entries);
128 
129 	if(dma_active.tqh_first != req) {
130 		splx(sps);
131 		return(0);
132 	}
133 	splx(sps);
134 
135 	/*
136 	 * We're at the head of the queue, ergo: we got the lock.
137 	 */
138 	*lock_stat = DMA_LOCK_GRANT;
139 
140 	if(rcaller) {
141 		/*
142 		 * Just return to caller immediately without going
143 		 * through 'call_func' first.
144 		 */
145 		return(1);
146 	}
147 
148 	(*call_func)(softc);	/* Call followup function		*/
149 	return(0);
150 }
151 
152 void
153 st_dmafree(softc, lock_stat)
154 void	*softc;
155 int	*lock_stat;
156 {
157 	int		sps;
158 	DMA_ENTRY	*req;
159 
160 	sps = splhigh();
161 
162 	/*
163 	 * Some validity checks first.
164 	 */
165 	if((req = dma_active.tqh_first) == NULL)
166 		panic("st_dmafree: empty active queue\n");
167 	if(req->softc != softc)
168 		printf("Caller of st_dmafree is not lock-owner!\n");
169 
170 	/*
171 	 * Clear lock status, move request from active to free queue.
172 	 */
173 	*lock_stat = 0;
174 	TAILQ_REMOVE(&dma_active, req, entries);
175 	TAILQ_INSERT_HEAD(&dma_free, req, entries);
176 
177 	if((req = dma_active.tqh_first) != NULL) {
178 		/*
179 		 * Call next request through softint handler. This avoids
180 		 * spl-conflicts.
181 		 */
182 		*req->lock_stat = DMA_LOCK_GRANT;
183 		add_sicallback(req->call_func, req->softc, 0);
184 	}
185 	splx(sps);
186 	return;
187 }
188 
189 int
190 st_dmawanted()
191 {
192 	return(dma_active.tqh_first->entries.tqe_next != NULL);
193 }
194 
195 cdmaint(sr)
196 long	sr;	/* sr at time of interrupt */
197 {
198 	if(dma_active.tqh_first != NULL) {
199 		if(!BASEPRI(sr)) {
200 			if(!sched_soft++)
201 				add_sicallback(cdmasoft, 0, 0);
202 		}
203 		else {
204 			spl1();
205 			cdmasoft();
206 		}
207 	}
208 	else printf("DMA interrupt discarded\n");
209 }
210 
211 static void cdmasoft()
212 {
213 	sched_soft = 0;
214 	(*dma_active.tqh_first->int_func)(dma_active.tqh_first->softc);
215 }
216 
217 /*
218  * Setup address for DMA-transfer.
219  * Note: The order _is_ important!
220  */
221 void
222 st_dmaaddr(address)
223 caddr_t	address;
224 {
225 	register u_long ad = (u_long)address;
226 
227 	DMA->dma_addr[AD_LOW ] = (ad     ) & 0xff;
228 	DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
229 	DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
230 }
231 
232 /*
233  * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
234  * The DMA_WRBIT trick flushes the FIFO before doing DMA.
235  */
236 void
237 st_dmacomm(mode, data)
238 int	mode, data;
239 {
240 	DMA->dma_mode = mode;
241 	DMA->dma_mode = mode ^ DMA_WRBIT;
242 	DMA->dma_mode = mode;
243 	DMA->dma_data = data;
244 }
245