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