dma.c revision 1.20 1 /* $NetBSD: dma.c,v 1.20 2009/03/14 15:36:03 dsl 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.20 2009/03/14 15:36:03 dsl 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()
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()
209 {
210 return(dma_active.tqh_first->entries.tqe_next != NULL);
211 }
212
213 int
214 cdmaint(unused, sr)
215 void *unused;
216 int sr; /* sr at time of interrupt */
217 {
218 dma_farg int_func;
219 void *softc;
220
221 if(dma_active.tqh_first != NULL) {
222 /*
223 * Due to the logic of the ST-DMA chip, it is not possible to
224 * check for stray interrupts here...
225 */
226 int_func = dma_active.tqh_first->int_func;
227 softc = dma_active.tqh_first->softc;
228
229 if(!BASEPRI(sr))
230 add_sicallback((si_farg)int_func, softc, 0);
231 else {
232 spl1();
233 (*int_func)(softc);
234 spl0();
235 }
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(void * address)
247 {
248 register u_long ad = (u_long)address;
249
250 DMA->dma_addr[AD_LOW ] = (ad ) & 0xff;
251 DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
252 DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
253 }
254
255 /*
256 * Get address from DMA unit.
257 */
258 u_long
259 st_dmaaddr_get()
260 {
261 register u_long ad = 0;
262
263 ad = (DMA->dma_addr[AD_LOW ] & 0xff);
264 ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
265 ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
266 return(ad);
267 }
268
269 /*
270 * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
271 * The DMA_WRBIT trick flushes the FIFO before doing DMA.
272 */
273 void
274 st_dmacomm(mode, nblk)
275 int mode, nblk;
276 {
277 DMA->dma_mode = mode;
278 DMA->dma_mode = mode ^ DMA_WRBIT;
279 DMA->dma_mode = mode;
280 DMA->dma_data = nblk;
281 delay(2); /* Needed for Falcon */
282 DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
283 }
284