dma.c revision 1.18.54.1 1 /* $NetBSD: dma.c,v 1.18.54.1 2010/11/20 01:09:27 riz 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.54.1 2010/11/20 01:09:27 riz 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 __P((void *, int));
92
93 static void st_dma_init __P((void));
94
95 static void
96 st_dma_init()
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(int_func, call_func, softc, lock_stat, rcaller)
112 dma_farg int_func;
113 dma_farg call_func;
114 void *softc;
115 int *lock_stat;
116 int rcaller;
117 {
118 int sps;
119 DMA_ENTRY *req;
120
121 if(must_init) {
122 st_dma_init();
123 must_init = 0;
124 }
125 *lock_stat = DMA_LOCK_REQ;
126
127 sps = splhigh();
128
129 /*
130 * Create a request...
131 */
132 if(dma_free.tqh_first == NULL)
133 panic("st_dmagrab: Too many outstanding requests");
134 req = dma_free.tqh_first;
135 TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
136 req->call_func = call_func;
137 req->int_func = int_func;
138 req->softc = softc;
139 req->lock_stat = lock_stat;
140 TAILQ_INSERT_TAIL(&dma_active, req, entries);
141
142 if(dma_active.tqh_first != req) {
143 if (call_func == NULL) {
144 do {
145 tsleep(&dma_active, PRIBIO, "dmalck", 0);
146 } while (*req->lock_stat != DMA_LOCK_GRANT);
147 splx(sps);
148 return(1);
149 }
150 splx(sps);
151 return(0);
152 }
153 splx(sps);
154
155 /*
156 * We're at the head of the queue, ergo: we got the lock.
157 */
158 *lock_stat = DMA_LOCK_GRANT;
159
160 if(rcaller || (call_func == NULL)) {
161 /*
162 * Just return to caller immediately without going
163 * through 'call_func' first.
164 */
165 return(1);
166 }
167
168 (*call_func)(softc); /* Call followup function */
169 return(0);
170 }
171
172 void
173 st_dmafree(softc, lock_stat)
174 void *softc;
175 int *lock_stat;
176 {
177 int sps;
178 DMA_ENTRY *req;
179
180 sps = splhigh();
181
182 /*
183 * Some validity checks first.
184 */
185 if((req = dma_active.tqh_first) == NULL)
186 panic("st_dmafree: empty active queue");
187 if(req->softc != softc)
188 printf("Caller of st_dmafree is not lock-owner!\n");
189
190 /*
191 * Clear lock status, move request from active to free queue.
192 */
193 *lock_stat = 0;
194 TAILQ_REMOVE(&dma_active, req, entries);
195 TAILQ_INSERT_HEAD(&dma_free, req, entries);
196
197 if((req = dma_active.tqh_first) != NULL) {
198 *req->lock_stat = DMA_LOCK_GRANT;
199
200 if (req->call_func == NULL)
201 wakeup((void *)&dma_active);
202 else {
203 /*
204 * Call next request through softint handler. This avoids
205 * spl-conflicts.
206 */
207 add_sicallback((si_farg)req->call_func, req->softc, 0);
208 }
209 }
210 splx(sps);
211 return;
212 }
213
214 int
215 st_dmawanted()
216 {
217 return(dma_active.tqh_first->entries.tqe_next != NULL);
218 }
219
220 int
221 cdmaint(unused, sr)
222 void *unused;
223 int sr; /* sr at time of interrupt */
224 {
225 dma_farg int_func;
226 void *softc;
227
228 if(dma_active.tqh_first != NULL) {
229 /*
230 * Due to the logic of the ST-DMA chip, it is not possible to
231 * check for stray interrupts here...
232 */
233 int_func = dma_active.tqh_first->int_func;
234 softc = dma_active.tqh_first->softc;
235 add_sicallback((si_farg)int_func, softc, 0);
236 return 1;
237 }
238 return 0;
239 }
240
241 /*
242 * Setup address for DMA-transfer.
243 * Note: The order _is_ important!
244 */
245 void
246 st_dmaaddr_set(address)
247 void * address;
248 {
249 register u_long ad = (u_long)address;
250
251 DMA->dma_addr[AD_LOW ] = (ad ) & 0xff;
252 DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
253 DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
254 }
255
256 /*
257 * Get address from DMA unit.
258 */
259 u_long
260 st_dmaaddr_get()
261 {
262 register u_long ad = 0;
263
264 ad = (DMA->dma_addr[AD_LOW ] & 0xff);
265 ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
266 ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
267 return(ad);
268 }
269
270 /*
271 * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
272 * The DMA_WRBIT trick flushes the FIFO before doing DMA.
273 */
274 void
275 st_dmacomm(mode, nblk)
276 int mode, nblk;
277 {
278 DMA->dma_mode = mode;
279 DMA->dma_mode = mode ^ DMA_WRBIT;
280 DMA->dma_mode = mode;
281 DMA->dma_data = nblk;
282 delay(2); /* Needed for Falcon */
283 DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
284 }
285