dma.c revision 1.6 1 1.6 leo /* $NetBSD: dma.c,v 1.6 1996/02/22 10:11:20 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo /*
34 1.1 leo * This file contains special code dealing with the DMA interface
35 1.1 leo * on the Atari ST.
36 1.1 leo *
37 1.1 leo * The DMA circuitry requires some special treatment for the peripheral
38 1.1 leo * devices which make use of the ST's DMA feature (the hard disk and the
39 1.1 leo * floppy drive).
40 1.1 leo * All devices using DMA need mutually exclusive access and can follow some
41 1.1 leo * standard pattern which will be provided in this file.
42 1.1 leo *
43 1.1 leo * The file contains the following entry points:
44 1.1 leo *
45 1.2 leo * st_dmagrab: ensure exclusive access to the DMA circuitry
46 1.2 leo * st_dmafree: free exclusive access to the DMA circuitry
47 1.2 leo * st_dmawanted: somebody is queued waiting for DMA-access
48 1.1 leo * dmaint: DMA interrupt routine, switches to the current driver
49 1.4 leo * st_dmaaddr_set: specify 24 bit RAM address
50 1.4 leo * st_dmaaddr_get: get address of last DMA-op
51 1.2 leo * st_dmacomm: program DMA, flush FIFO first
52 1.1 leo */
53 1.1 leo
54 1.1 leo #include <sys/param.h>
55 1.1 leo #include <sys/systm.h>
56 1.1 leo #include <sys/kernel.h>
57 1.3 leo #include <sys/queue.h>
58 1.1 leo #include <machine/cpu.h>
59 1.1 leo #include <machine/iomap.h>
60 1.1 leo #include <machine/dma.h>
61 1.1 leo
62 1.1 leo #define NDMA_DEV 10 /* Max 2 floppy's, 8 hard-disks */
63 1.3 leo typedef struct dma_entry {
64 1.3 leo TAILQ_ENTRY(dma_entry) entries; /* List pointers */
65 1.6 leo void (*call_func)(void *); /* Call when lock granted */
66 1.6 leo void (*int_func)(void *); /* Call on DMA interrupt */
67 1.6 leo void *softc; /* Arg. to int_func */
68 1.6 leo int *lock_stat; /* status of DMA lock */
69 1.3 leo } DMA_ENTRY;
70 1.3 leo
71 1.3 leo /*
72 1.3 leo * Preallocated entries. An allocator seem an overkill here.
73 1.3 leo */
74 1.3 leo static DMA_ENTRY dmatable[NDMA_DEV]; /* preallocated entries */
75 1.3 leo static int sched_soft = 0; /* callback scheduled */
76 1.3 leo
77 1.3 leo /*
78 1.3 leo * Heads of free and active lists:
79 1.3 leo */
80 1.3 leo static TAILQ_HEAD(freehead, dma_entry) dma_free;
81 1.3 leo static TAILQ_HEAD(acthead, dma_entry) dma_active;
82 1.3 leo
83 1.3 leo static int must_init = 1; /* Must initialize */
84 1.1 leo
85 1.6 leo void cdmaint __P((int));
86 1.6 leo
87 1.6 leo long sr; /* sr at time of interrupt */
88 1.1 leo static void cdmasoft __P((void));
89 1.3 leo static void init_queues __P((void));
90 1.1 leo
91 1.6 leo static void
92 1.6 leo init_queues()
93 1.3 leo {
94 1.3 leo int i;
95 1.3 leo
96 1.3 leo TAILQ_INIT(&dma_free);
97 1.3 leo TAILQ_INIT(&dma_active);
98 1.3 leo
99 1.3 leo for(i = 0; i < NDMA_DEV; i++)
100 1.3 leo TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
101 1.3 leo }
102 1.3 leo
103 1.6 leo int
104 1.6 leo st_dmagrab(int_func, call_func, softc, lock_stat, rcaller)
105 1.6 leo dma_farg int_func;
106 1.6 leo dma_farg call_func;
107 1.6 leo void *softc;
108 1.6 leo int *lock_stat;
109 1.6 leo int rcaller;
110 1.1 leo {
111 1.1 leo int sps;
112 1.3 leo DMA_ENTRY *req;
113 1.1 leo
114 1.3 leo if(must_init) {
115 1.3 leo init_queues();
116 1.3 leo must_init = 0;
117 1.3 leo }
118 1.3 leo *lock_stat = DMA_LOCK_REQ;
119 1.1 leo
120 1.3 leo sps = splhigh();
121 1.3 leo
122 1.3 leo /*
123 1.3 leo * Create a request...
124 1.3 leo */
125 1.3 leo if(dma_free.tqh_first == NULL)
126 1.3 leo panic("st_dmagrab: Too many outstanding requests\n");
127 1.3 leo req = dma_free.tqh_first;
128 1.3 leo TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries);
129 1.3 leo req->call_func = call_func;
130 1.3 leo req->int_func = int_func;
131 1.3 leo req->softc = softc;
132 1.3 leo req->lock_stat = lock_stat;
133 1.3 leo TAILQ_INSERT_TAIL(&dma_active, req, entries);
134 1.3 leo
135 1.3 leo if(dma_active.tqh_first != req) {
136 1.1 leo splx(sps);
137 1.2 leo return(0);
138 1.1 leo }
139 1.3 leo splx(sps);
140 1.3 leo
141 1.3 leo /*
142 1.3 leo * We're at the head of the queue, ergo: we got the lock.
143 1.3 leo */
144 1.3 leo *lock_stat = DMA_LOCK_GRANT;
145 1.3 leo
146 1.2 leo if(rcaller) {
147 1.2 leo /*
148 1.2 leo * Just return to caller immediately without going
149 1.2 leo * through 'call_func' first.
150 1.2 leo */
151 1.2 leo return(1);
152 1.2 leo }
153 1.2 leo
154 1.1 leo (*call_func)(softc); /* Call followup function */
155 1.2 leo return(0);
156 1.1 leo }
157 1.1 leo
158 1.2 leo void
159 1.3 leo st_dmafree(softc, lock_stat)
160 1.3 leo void *softc;
161 1.3 leo int *lock_stat;
162 1.1 leo {
163 1.1 leo int sps;
164 1.3 leo DMA_ENTRY *req;
165 1.1 leo
166 1.3 leo sps = splhigh();
167 1.3 leo
168 1.3 leo /*
169 1.3 leo * Some validity checks first.
170 1.3 leo */
171 1.3 leo if((req = dma_active.tqh_first) == NULL)
172 1.3 leo panic("st_dmafree: empty active queue\n");
173 1.3 leo if(req->softc != softc)
174 1.3 leo printf("Caller of st_dmafree is not lock-owner!\n");
175 1.3 leo
176 1.3 leo /*
177 1.3 leo * Clear lock status, move request from active to free queue.
178 1.3 leo */
179 1.3 leo *lock_stat = 0;
180 1.3 leo TAILQ_REMOVE(&dma_active, req, entries);
181 1.3 leo TAILQ_INSERT_HEAD(&dma_free, req, entries);
182 1.3 leo
183 1.3 leo if((req = dma_active.tqh_first) != NULL) {
184 1.3 leo /*
185 1.3 leo * Call next request through softint handler. This avoids
186 1.3 leo * spl-conflicts.
187 1.3 leo */
188 1.3 leo *req->lock_stat = DMA_LOCK_GRANT;
189 1.3 leo add_sicallback(req->call_func, req->softc, 0);
190 1.1 leo }
191 1.1 leo splx(sps);
192 1.3 leo return;
193 1.1 leo }
194 1.1 leo
195 1.2 leo int
196 1.2 leo st_dmawanted()
197 1.2 leo {
198 1.3 leo return(dma_active.tqh_first->entries.tqe_next != NULL);
199 1.2 leo }
200 1.2 leo
201 1.6 leo void
202 1.1 leo cdmaint(sr)
203 1.6 leo int sr; /* sr at time of interrupt */
204 1.1 leo {
205 1.3 leo if(dma_active.tqh_first != NULL) {
206 1.1 leo if(!BASEPRI(sr)) {
207 1.1 leo if(!sched_soft++)
208 1.1 leo add_sicallback(cdmasoft, 0, 0);
209 1.1 leo }
210 1.1 leo else {
211 1.1 leo spl1();
212 1.1 leo cdmasoft();
213 1.1 leo }
214 1.1 leo }
215 1.3 leo else printf("DMA interrupt discarded\n");
216 1.1 leo }
217 1.1 leo
218 1.6 leo static void
219 1.6 leo cdmasoft()
220 1.1 leo {
221 1.6 leo int s;
222 1.6 leo dma_farg int_func;
223 1.6 leo void *softc;
224 1.4 leo
225 1.4 leo /*
226 1.4 leo * Prevent a race condition here. DMA might be freed while
227 1.4 leo * the callback was pending!
228 1.4 leo */
229 1.4 leo s = splhigh();
230 1.1 leo sched_soft = 0;
231 1.4 leo if(dma_active.tqh_first != NULL) {
232 1.4 leo int_func = dma_active.tqh_first->int_func;
233 1.4 leo softc = dma_active.tqh_first->softc;
234 1.4 leo }
235 1.6 leo else int_func = softc = NULL;
236 1.4 leo splx(s);
237 1.4 leo
238 1.4 leo if(int_func != NULL)
239 1.4 leo (*int_func)(softc);
240 1.1 leo }
241 1.1 leo
242 1.2 leo /*
243 1.2 leo * Setup address for DMA-transfer.
244 1.2 leo * Note: The order _is_ important!
245 1.2 leo */
246 1.2 leo void
247 1.4 leo st_dmaaddr_set(address)
248 1.1 leo caddr_t address;
249 1.1 leo {
250 1.1 leo register u_long ad = (u_long)address;
251 1.1 leo
252 1.1 leo DMA->dma_addr[AD_LOW ] = (ad ) & 0xff;
253 1.1 leo DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
254 1.1 leo DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
255 1.1 leo }
256 1.1 leo
257 1.2 leo /*
258 1.4 leo * Get address from DMA unit.
259 1.4 leo */
260 1.4 leo u_long
261 1.4 leo st_dmaaddr_get()
262 1.4 leo {
263 1.4 leo register u_long ad = 0;
264 1.4 leo
265 1.4 leo ad = (DMA->dma_addr[AD_LOW ] & 0xff);
266 1.4 leo ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
267 1.4 leo ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
268 1.4 leo return(ad);
269 1.4 leo }
270 1.4 leo
271 1.4 leo /*
272 1.2 leo * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes.
273 1.2 leo * The DMA_WRBIT trick flushes the FIFO before doing DMA.
274 1.2 leo */
275 1.2 leo void
276 1.4 leo st_dmacomm(mode, nblk)
277 1.4 leo int mode, nblk;
278 1.1 leo {
279 1.1 leo DMA->dma_mode = mode;
280 1.2 leo DMA->dma_mode = mode ^ DMA_WRBIT;
281 1.1 leo DMA->dma_mode = mode;
282 1.4 leo DMA->dma_data = nblk;
283 1.5 leo delay(2); /* Needed for Falcon */
284 1.4 leo DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
285 1.1 leo }
286