iomd_dma.c revision 1.8 1 /* $NetBSD: iomd_dma.c,v 1.8 2003/07/15 00:24:45 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1995 Scott Stevens
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 Scott Stevens.
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 * RiscBSD kernel project
33 *
34 * dma.c
35 *
36 * Created : 15/03/97
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: iomd_dma.c,v 1.8 2003/07/15 00:24:45 lukem Exp $");
41
42 #define DMA_DEBUG
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <machine/intr.h>
50 #include <machine/pmap.h>
51 #include <arm/iomd/iomdreg.h>
52 #include <arm/iomd/iomdvar.h>
53 #include <arm/iomd/iomd_dma.h>
54
55
56 /*
57 * Only for non ARM7500 machines but the kernel could be booted on a different machine
58 */
59
60 static struct dma_ctrl ctrl[6];
61
62 void dma_dumpdc __P((struct dma_ctrl *));
63
64 void
65 dma_go(dp)
66 struct dma_ctrl *dp;
67 {
68 #ifdef DMA_DEBUG
69 printf("dma_go()\n");
70 #endif
71 if(dp->dc_flags & DMA_FL_READY) {
72 dp->dc_flags = DMA_FL_ACTIVE;
73 enable_irq(IRQ_DMACH0 + dp->dc_channel);
74 }
75 else
76 panic("dma not ready");
77 }
78
79 int
80 dma_reset(dp)
81 struct dma_ctrl *dp;
82 {
83 #ifdef DMA_DEBUG
84 printf("dma_reset()\n");
85 dma_dumpdc(dp);
86 #endif
87 *dp->dc_cr = DMA_CR_CLEAR;
88 dp->dc_flags = 0;
89 disable_irq(IRQ_DMACH0 + dp->dc_channel);
90 return(0);
91 }
92
93 /*
94 * Setup dma transfer, prior to the dma_go call
95 */
96 int
97 dma_setup(dp, start, len, readp)
98 struct dma_ctrl *dp;
99 int readp;
100 u_char *start;
101 int len;
102 {
103 #ifdef DMA_DEBUG
104 printf("dma_setup(start = %p, len = 0x%08x, readp = %d\n", start, len, readp);
105 #endif
106 if(((u_int)start & (dp->dc_dmasize - 1)) ||
107 (len & (dp->dc_dmasize - 1))) {
108 printf("dma_setup: unaligned DMA, %p (0x%08x)\n",
109 start, len);
110 }
111 *dp->dc_cr = DMA_CR_CLEAR | DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
112 *dp->dc_cr = DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
113
114 dp->dc_nextaddr = start;
115 dp->dc_len = len;
116
117 dp->dc_flags = DMA_FL_READY;
118 return(0);
119 }
120
121 /*
122 * return true if DMA is active
123 */
124 int
125 dma_isactive(dp)
126 struct dma_ctrl *dp;
127 {
128 return(dp->dc_flags & DMA_FL_ACTIVE);
129 }
130
131 /*
132 * return true if interrupt pending
133 */
134 int
135 dma_isintr(dp)
136 struct dma_ctrl *dp;
137 {
138 #ifdef DMA_DEBUG
139 /* printf("dma_isintr() returning %d\n", *dp->dc_st & DMA_ST_INT);*/
140 #endif
141 return(*dp->dc_st & DMA_ST_INT);
142 }
143
144 int
145 dma_intr(cookie)
146 void *cookie;
147 {
148 struct dma_ctrl *dp = cookie;
149 u_char status = (*dp->dc_st) & DMA_ST_MASK;
150 paddr_t cur;
151 int len;
152 int bufap = 0;
153
154 #ifdef DMA_DEBUG
155 printf("dma_intr() status = 0x%02x\n", status);
156 #endif
157
158 if(!(dp->dc_flags & DMA_FL_ACTIVE)) {
159 /* interrupt whilst not active */
160 /* ie. last buffer programmed */
161 dma_reset(dp);
162 return(0);
163 }
164
165 switch(status) {
166 case DMA_ST_OVER | DMA_ST_INT:
167 case DMA_ST_OVER | DMA_ST_INT | DMA_ST_CHAN:
168 /* idle, either first buffer or finished */
169 if(status & DMA_ST_CHAN) {
170 /* fill buffer B */
171 bufap = 0;
172 goto fill;
173 }
174 else {
175 /* fill buffer A */
176 bufap = 1;
177 goto fill;
178 }
179 break;
180 case DMA_ST_INT:
181 case DMA_ST_INT | DMA_ST_CHAN:
182 /* buffer ready */
183 if(status & DMA_ST_CHAN) {
184 /* fill buffer A */
185 bufap = 1;
186 goto fill;
187 }
188 else {
189 /* fill buffer B */
190 bufap = 0;
191 goto fill;
192 }
193 break;
194 default:
195 /* Shouldn't be here */
196 #ifdef DMA_DEBUG
197 printf("DMA ch %d bad status [%x]\n", dp->dc_channel, status);
198 dma_dumpdc(dp);
199 #endif
200 break;
201 }
202
203 /* return(0);*/
204 /* XXX */
205 #define PHYS(x, y) pmap_extract(pmap_kernel(), (vaddr_t)x, (paddr_t *)(y))
206 fill:
207 #ifdef DMA_DEBUG
208 printf("fill:\n");
209 #endif
210 if (dp->dc_len == 0) goto done;
211 PHYS(dp->dc_nextaddr, &cur);
212 len = PAGE_SIZE - (cur & PGOFSET);
213 if (len > dp->dc_len) {
214 /* Last buffer */
215 len = dp->dc_len;
216 }
217
218 #ifdef DMA_DEBUG
219 dma_dumpdc(dp);
220 /* ptsc_dump_mem(dp->dc_nextaddr, len);*/
221 #endif
222 /*
223 * Flush the cache for this address
224 */
225 cpu_dcache_wbinv_range((vaddr_t)dp->dc_nextaddr, len);
226
227 dp->dc_nextaddr += len;
228 dp->dc_len -= len;
229
230 if(bufap) {
231 *dp->dc_cura = (u_int)cur;
232 *dp->dc_enda = ((u_int)cur + len - dp->dc_dmasize) |
233 (dp->dc_len == 0 ? DMA_END_STOP : 0);
234 if (dp->dc_len == 0) {
235 /* Last buffer, fill other buffer with garbage */
236 *dp->dc_endb = (u_int)cur;
237 }
238 }
239 else {
240 *dp->dc_curb = (u_int)cur;
241 *dp->dc_endb = ((u_int)cur + len - dp->dc_dmasize) |
242 (dp->dc_len == 0 ? DMA_END_STOP : 0);
243 if (dp->dc_len == 0) {
244 /* Last buffer, fill other buffer with garbage */
245 *dp->dc_enda = (u_int)cur;
246 }
247 }
248 #ifdef DMA_DEBUG
249 dma_dumpdc(dp);
250 /* ptsc_dump_mem(dp->dc_nextaddr - len, len);*/
251 printf("about to return\n");
252 #endif
253 return(1);
254 done:
255 #ifdef DMA_DEBUG
256 printf("done:\n");
257 #endif
258 dp->dc_flags = 0;
259 *dp->dc_cr = 0;
260 disable_irq(IRQ_DMACH0 + dp->dc_channel);
261 #ifdef DMA_DEBUG
262 printf("about to return\n");
263 #endif
264 return(1);
265 }
266
267 void
268 dma_dumpdc(dc)
269 struct dma_ctrl *dc;
270 {
271 printf("\ndc:\t%p\n"
272 "dc_channel:\t%p=0x%08x\tdc_flags:\t%p=0x%08x\n"
273 "dc_cura:\t%p=0x%08x\tdc_enda:\t%p=0x%08x\n"
274 "dc_curb:\t%p=0x%08x\tdc_endb:\t%p=0x%08x\n"
275 "dc_cr:\t%p=0x%02x\t\tdc_st:\t%p=0x%02x\n"
276 "dc_nextaddr:\t%p=0x%08x\tdc_len:\t%p=0x%08x\n",
277 dc,
278 &dc->dc_channel, (int)dc->dc_channel,
279 &dc->dc_flags, (int)dc->dc_flags,
280 dc->dc_cura, (int)*dc->dc_cura,
281 dc->dc_enda, (int)*dc->dc_enda,
282 dc->dc_curb, (int)*dc->dc_curb,
283 dc->dc_endb, (int)*dc->dc_endb,
284 dc->dc_cr, (int)*dc->dc_cr,
285 dc->dc_st, (int)(*dc->dc_st) & DMA_ST_MASK,
286 &dc->dc_nextaddr, (int)dc->dc_nextaddr,
287 &dc->dc_len, dc->dc_len);
288 }
289
290 struct dma_ctrl *
291 dma_init(ch, extp, dmasize, ipl)
292 int ch;
293 int extp;
294 int dmasize;
295 int ipl;
296 {
297 struct dma_ctrl *dp = &ctrl[ch];
298 int offset = ch * 0x20;
299 volatile u_char *dmaext = (volatile u_char *)(IOMD_ADDRESS(IOMD_DMAEXT));
300
301 printf("Initialising DMA channel %d\n", ch);
302
303 dp->dc_channel = ch;
304 dp->dc_flags = 0;
305 dp->dc_dmasize = dmasize;
306 dp->dc_cura = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURA) + offset);
307 dp->dc_enda = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDA) + offset);
308 dp->dc_curb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURB) + offset);
309 dp->dc_endb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDB) + offset);
310 dp->dc_cr = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0CR) + offset);
311 dp->dc_st = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0ST) + offset);
312
313 if (extp)
314 *dmaext |= (1 << ch);
315
316 printf("about to claim interrupt\n");
317
318 dp->dc_ih.ih_func = dma_intr;
319 dp->dc_ih.ih_arg = dp;
320 dp->dc_ih.ih_level = ipl;
321 dp->dc_ih.ih_name = "dma";
322 dp->dc_ih.ih_maskaddr = (u_int) IOMD_ADDRESS(IOMD_DMARQ);
323 dp->dc_ih.ih_maskbits = (1 << ch);
324
325 if (irq_claim(IRQ_DMACH0 + ch, &dp->dc_ih))
326 panic("Cannot install DMA IRQ handler");
327
328 return(dp);
329 }
330
331