iomd_dma.c revision 1.8 1 1.8 lukem /* $NetBSD: iomd_dma.c,v 1.8 2003/07/15 00:24:45 lukem Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 1995 Scott Stevens
5 1.1 reinoud * All rights reserved.
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud * 3. All advertising materials mentioning features or use of this software
16 1.1 reinoud * must display the following acknowledgement:
17 1.1 reinoud * This product includes software developed by Scott Stevens.
18 1.1 reinoud * 4. The name of the author may not be used to endorse or promote products
19 1.1 reinoud * derived from this software without specific prior written permission.
20 1.1 reinoud *
21 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 reinoud *
32 1.1 reinoud * RiscBSD kernel project
33 1.1 reinoud *
34 1.1 reinoud * dma.c
35 1.1 reinoud *
36 1.1 reinoud * Created : 15/03/97
37 1.1 reinoud */
38 1.8 lukem
39 1.8 lukem #include <sys/cdefs.h>
40 1.8 lukem __KERNEL_RCSID(0, "$NetBSD: iomd_dma.c,v 1.8 2003/07/15 00:24:45 lukem Exp $");
41 1.1 reinoud
42 1.1 reinoud #define DMA_DEBUG
43 1.1 reinoud #include <sys/param.h>
44 1.1 reinoud #include <sys/systm.h>
45 1.1 reinoud #include <sys/kernel.h>
46 1.1 reinoud
47 1.1 reinoud #include <uvm/uvm_extern.h>
48 1.1 reinoud
49 1.2 thorpej #include <machine/intr.h>
50 1.1 reinoud #include <machine/pmap.h>
51 1.1 reinoud #include <arm/iomd/iomdreg.h>
52 1.1 reinoud #include <arm/iomd/iomdvar.h>
53 1.1 reinoud #include <arm/iomd/iomd_dma.h>
54 1.1 reinoud
55 1.1 reinoud
56 1.1 reinoud /*
57 1.1 reinoud * Only for non ARM7500 machines but the kernel could be booted on a different machine
58 1.1 reinoud */
59 1.1 reinoud
60 1.1 reinoud static struct dma_ctrl ctrl[6];
61 1.1 reinoud
62 1.1 reinoud void dma_dumpdc __P((struct dma_ctrl *));
63 1.1 reinoud
64 1.1 reinoud void
65 1.1 reinoud dma_go(dp)
66 1.1 reinoud struct dma_ctrl *dp;
67 1.1 reinoud {
68 1.1 reinoud #ifdef DMA_DEBUG
69 1.1 reinoud printf("dma_go()\n");
70 1.1 reinoud #endif
71 1.1 reinoud if(dp->dc_flags & DMA_FL_READY) {
72 1.1 reinoud dp->dc_flags = DMA_FL_ACTIVE;
73 1.1 reinoud enable_irq(IRQ_DMACH0 + dp->dc_channel);
74 1.1 reinoud }
75 1.1 reinoud else
76 1.1 reinoud panic("dma not ready");
77 1.1 reinoud }
78 1.1 reinoud
79 1.1 reinoud int
80 1.1 reinoud dma_reset(dp)
81 1.1 reinoud struct dma_ctrl *dp;
82 1.1 reinoud {
83 1.1 reinoud #ifdef DMA_DEBUG
84 1.1 reinoud printf("dma_reset()\n");
85 1.1 reinoud dma_dumpdc(dp);
86 1.1 reinoud #endif
87 1.1 reinoud *dp->dc_cr = DMA_CR_CLEAR;
88 1.1 reinoud dp->dc_flags = 0;
89 1.1 reinoud disable_irq(IRQ_DMACH0 + dp->dc_channel);
90 1.1 reinoud return(0);
91 1.1 reinoud }
92 1.1 reinoud
93 1.1 reinoud /*
94 1.1 reinoud * Setup dma transfer, prior to the dma_go call
95 1.1 reinoud */
96 1.1 reinoud int
97 1.1 reinoud dma_setup(dp, start, len, readp)
98 1.1 reinoud struct dma_ctrl *dp;
99 1.1 reinoud int readp;
100 1.1 reinoud u_char *start;
101 1.1 reinoud int len;
102 1.1 reinoud {
103 1.1 reinoud #ifdef DMA_DEBUG
104 1.1 reinoud printf("dma_setup(start = %p, len = 0x%08x, readp = %d\n", start, len, readp);
105 1.1 reinoud #endif
106 1.1 reinoud if(((u_int)start & (dp->dc_dmasize - 1)) ||
107 1.1 reinoud (len & (dp->dc_dmasize - 1))) {
108 1.1 reinoud printf("dma_setup: unaligned DMA, %p (0x%08x)\n",
109 1.1 reinoud start, len);
110 1.1 reinoud }
111 1.1 reinoud *dp->dc_cr = DMA_CR_CLEAR | DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
112 1.1 reinoud *dp->dc_cr = DMA_CR_ENABLE | (readp?DMA_CR_DIR:0) | dp->dc_dmasize;
113 1.1 reinoud
114 1.1 reinoud dp->dc_nextaddr = start;
115 1.1 reinoud dp->dc_len = len;
116 1.1 reinoud
117 1.1 reinoud dp->dc_flags = DMA_FL_READY;
118 1.1 reinoud return(0);
119 1.1 reinoud }
120 1.1 reinoud
121 1.1 reinoud /*
122 1.1 reinoud * return true if DMA is active
123 1.1 reinoud */
124 1.1 reinoud int
125 1.1 reinoud dma_isactive(dp)
126 1.1 reinoud struct dma_ctrl *dp;
127 1.1 reinoud {
128 1.1 reinoud return(dp->dc_flags & DMA_FL_ACTIVE);
129 1.1 reinoud }
130 1.1 reinoud
131 1.1 reinoud /*
132 1.1 reinoud * return true if interrupt pending
133 1.1 reinoud */
134 1.1 reinoud int
135 1.1 reinoud dma_isintr(dp)
136 1.1 reinoud struct dma_ctrl *dp;
137 1.1 reinoud {
138 1.1 reinoud #ifdef DMA_DEBUG
139 1.1 reinoud /* printf("dma_isintr() returning %d\n", *dp->dc_st & DMA_ST_INT);*/
140 1.1 reinoud #endif
141 1.1 reinoud return(*dp->dc_st & DMA_ST_INT);
142 1.1 reinoud }
143 1.1 reinoud
144 1.1 reinoud int
145 1.4 bjh21 dma_intr(cookie)
146 1.4 bjh21 void *cookie;
147 1.1 reinoud {
148 1.4 bjh21 struct dma_ctrl *dp = cookie;
149 1.1 reinoud u_char status = (*dp->dc_st) & DMA_ST_MASK;
150 1.5 thorpej paddr_t cur;
151 1.1 reinoud int len;
152 1.1 reinoud int bufap = 0;
153 1.1 reinoud
154 1.1 reinoud #ifdef DMA_DEBUG
155 1.1 reinoud printf("dma_intr() status = 0x%02x\n", status);
156 1.1 reinoud #endif
157 1.1 reinoud
158 1.1 reinoud if(!(dp->dc_flags & DMA_FL_ACTIVE)) {
159 1.1 reinoud /* interrupt whilst not active */
160 1.1 reinoud /* ie. last buffer programmed */
161 1.1 reinoud dma_reset(dp);
162 1.1 reinoud return(0);
163 1.1 reinoud }
164 1.1 reinoud
165 1.1 reinoud switch(status) {
166 1.1 reinoud case DMA_ST_OVER | DMA_ST_INT:
167 1.1 reinoud case DMA_ST_OVER | DMA_ST_INT | DMA_ST_CHAN:
168 1.1 reinoud /* idle, either first buffer or finished */
169 1.1 reinoud if(status & DMA_ST_CHAN) {
170 1.1 reinoud /* fill buffer B */
171 1.1 reinoud bufap = 0;
172 1.1 reinoud goto fill;
173 1.1 reinoud }
174 1.1 reinoud else {
175 1.1 reinoud /* fill buffer A */
176 1.1 reinoud bufap = 1;
177 1.1 reinoud goto fill;
178 1.1 reinoud }
179 1.1 reinoud break;
180 1.1 reinoud case DMA_ST_INT:
181 1.1 reinoud case DMA_ST_INT | DMA_ST_CHAN:
182 1.1 reinoud /* buffer ready */
183 1.1 reinoud if(status & DMA_ST_CHAN) {
184 1.1 reinoud /* fill buffer A */
185 1.1 reinoud bufap = 1;
186 1.1 reinoud goto fill;
187 1.1 reinoud }
188 1.1 reinoud else {
189 1.1 reinoud /* fill buffer B */
190 1.1 reinoud bufap = 0;
191 1.1 reinoud goto fill;
192 1.1 reinoud }
193 1.1 reinoud break;
194 1.1 reinoud default:
195 1.1 reinoud /* Shouldn't be here */
196 1.1 reinoud #ifdef DMA_DEBUG
197 1.1 reinoud printf("DMA ch %d bad status [%x]\n", dp->dc_channel, status);
198 1.1 reinoud dma_dumpdc(dp);
199 1.1 reinoud #endif
200 1.1 reinoud break;
201 1.1 reinoud }
202 1.1 reinoud
203 1.1 reinoud /* return(0);*/
204 1.1 reinoud /* XXX */
205 1.1 reinoud #define PHYS(x, y) pmap_extract(pmap_kernel(), (vaddr_t)x, (paddr_t *)(y))
206 1.1 reinoud fill:
207 1.1 reinoud #ifdef DMA_DEBUG
208 1.1 reinoud printf("fill:\n");
209 1.1 reinoud #endif
210 1.1 reinoud if (dp->dc_len == 0) goto done;
211 1.1 reinoud PHYS(dp->dc_nextaddr, &cur);
212 1.7 thorpej len = PAGE_SIZE - (cur & PGOFSET);
213 1.1 reinoud if (len > dp->dc_len) {
214 1.1 reinoud /* Last buffer */
215 1.1 reinoud len = dp->dc_len;
216 1.1 reinoud }
217 1.1 reinoud
218 1.1 reinoud #ifdef DMA_DEBUG
219 1.1 reinoud dma_dumpdc(dp);
220 1.1 reinoud /* ptsc_dump_mem(dp->dc_nextaddr, len);*/
221 1.1 reinoud #endif
222 1.1 reinoud /*
223 1.1 reinoud * Flush the cache for this address
224 1.1 reinoud */
225 1.5 thorpej cpu_dcache_wbinv_range((vaddr_t)dp->dc_nextaddr, len);
226 1.1 reinoud
227 1.1 reinoud dp->dc_nextaddr += len;
228 1.1 reinoud dp->dc_len -= len;
229 1.1 reinoud
230 1.1 reinoud if(bufap) {
231 1.1 reinoud *dp->dc_cura = (u_int)cur;
232 1.1 reinoud *dp->dc_enda = ((u_int)cur + len - dp->dc_dmasize) |
233 1.1 reinoud (dp->dc_len == 0 ? DMA_END_STOP : 0);
234 1.1 reinoud if (dp->dc_len == 0) {
235 1.1 reinoud /* Last buffer, fill other buffer with garbage */
236 1.1 reinoud *dp->dc_endb = (u_int)cur;
237 1.1 reinoud }
238 1.1 reinoud }
239 1.1 reinoud else {
240 1.1 reinoud *dp->dc_curb = (u_int)cur;
241 1.1 reinoud *dp->dc_endb = ((u_int)cur + len - dp->dc_dmasize) |
242 1.1 reinoud (dp->dc_len == 0 ? DMA_END_STOP : 0);
243 1.1 reinoud if (dp->dc_len == 0) {
244 1.1 reinoud /* Last buffer, fill other buffer with garbage */
245 1.1 reinoud *dp->dc_enda = (u_int)cur;
246 1.1 reinoud }
247 1.1 reinoud }
248 1.1 reinoud #ifdef DMA_DEBUG
249 1.1 reinoud dma_dumpdc(dp);
250 1.1 reinoud /* ptsc_dump_mem(dp->dc_nextaddr - len, len);*/
251 1.1 reinoud printf("about to return\n");
252 1.1 reinoud #endif
253 1.1 reinoud return(1);
254 1.1 reinoud done:
255 1.1 reinoud #ifdef DMA_DEBUG
256 1.1 reinoud printf("done:\n");
257 1.1 reinoud #endif
258 1.1 reinoud dp->dc_flags = 0;
259 1.1 reinoud *dp->dc_cr = 0;
260 1.1 reinoud disable_irq(IRQ_DMACH0 + dp->dc_channel);
261 1.1 reinoud #ifdef DMA_DEBUG
262 1.1 reinoud printf("about to return\n");
263 1.1 reinoud #endif
264 1.1 reinoud return(1);
265 1.1 reinoud }
266 1.1 reinoud
267 1.1 reinoud void
268 1.1 reinoud dma_dumpdc(dc)
269 1.1 reinoud struct dma_ctrl *dc;
270 1.1 reinoud {
271 1.1 reinoud printf("\ndc:\t%p\n"
272 1.1 reinoud "dc_channel:\t%p=0x%08x\tdc_flags:\t%p=0x%08x\n"
273 1.1 reinoud "dc_cura:\t%p=0x%08x\tdc_enda:\t%p=0x%08x\n"
274 1.1 reinoud "dc_curb:\t%p=0x%08x\tdc_endb:\t%p=0x%08x\n"
275 1.1 reinoud "dc_cr:\t%p=0x%02x\t\tdc_st:\t%p=0x%02x\n"
276 1.1 reinoud "dc_nextaddr:\t%p=0x%08x\tdc_len:\t%p=0x%08x\n",
277 1.1 reinoud dc,
278 1.1 reinoud &dc->dc_channel, (int)dc->dc_channel,
279 1.1 reinoud &dc->dc_flags, (int)dc->dc_flags,
280 1.1 reinoud dc->dc_cura, (int)*dc->dc_cura,
281 1.1 reinoud dc->dc_enda, (int)*dc->dc_enda,
282 1.1 reinoud dc->dc_curb, (int)*dc->dc_curb,
283 1.1 reinoud dc->dc_endb, (int)*dc->dc_endb,
284 1.1 reinoud dc->dc_cr, (int)*dc->dc_cr,
285 1.1 reinoud dc->dc_st, (int)(*dc->dc_st) & DMA_ST_MASK,
286 1.1 reinoud &dc->dc_nextaddr, (int)dc->dc_nextaddr,
287 1.1 reinoud &dc->dc_len, dc->dc_len);
288 1.1 reinoud }
289 1.1 reinoud
290 1.1 reinoud struct dma_ctrl *
291 1.1 reinoud dma_init(ch, extp, dmasize, ipl)
292 1.1 reinoud int ch;
293 1.1 reinoud int extp;
294 1.1 reinoud int dmasize;
295 1.1 reinoud int ipl;
296 1.1 reinoud {
297 1.1 reinoud struct dma_ctrl *dp = &ctrl[ch];
298 1.1 reinoud int offset = ch * 0x20;
299 1.1 reinoud volatile u_char *dmaext = (volatile u_char *)(IOMD_ADDRESS(IOMD_DMAEXT));
300 1.1 reinoud
301 1.1 reinoud printf("Initialising DMA channel %d\n", ch);
302 1.1 reinoud
303 1.1 reinoud dp->dc_channel = ch;
304 1.1 reinoud dp->dc_flags = 0;
305 1.1 reinoud dp->dc_dmasize = dmasize;
306 1.1 reinoud dp->dc_cura = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURA) + offset);
307 1.1 reinoud dp->dc_enda = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDA) + offset);
308 1.1 reinoud dp->dc_curb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0CURB) + offset);
309 1.1 reinoud dp->dc_endb = (volatile u_int *)(IOMD_ADDRESS(IOMD_IO0ENDB) + offset);
310 1.1 reinoud dp->dc_cr = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0CR) + offset);
311 1.1 reinoud dp->dc_st = (volatile u_char *)(IOMD_ADDRESS(IOMD_IO0ST) + offset);
312 1.1 reinoud
313 1.1 reinoud if (extp)
314 1.1 reinoud *dmaext |= (1 << ch);
315 1.1 reinoud
316 1.1 reinoud printf("about to claim interrupt\n");
317 1.1 reinoud
318 1.1 reinoud dp->dc_ih.ih_func = dma_intr;
319 1.1 reinoud dp->dc_ih.ih_arg = dp;
320 1.1 reinoud dp->dc_ih.ih_level = ipl;
321 1.1 reinoud dp->dc_ih.ih_name = "dma";
322 1.1 reinoud dp->dc_ih.ih_maskaddr = (u_int) IOMD_ADDRESS(IOMD_DMARQ);
323 1.1 reinoud dp->dc_ih.ih_maskbits = (1 << ch);
324 1.1 reinoud
325 1.1 reinoud if (irq_claim(IRQ_DMACH0 + ch, &dp->dc_ih))
326 1.6 provos panic("Cannot install DMA IRQ handler");
327 1.1 reinoud
328 1.1 reinoud return(dp);
329 1.1 reinoud }
330 1.1 reinoud
331