nextdma.c revision 1.1.1.1 1 1.1 dbj /* $NetBSD: nextdma.c,v 1.1.1.1 1998/06/09 07:53:05 dbj Exp $ */
2 1.1 dbj /*
3 1.1 dbj * Copyright (c) 1998 Darrin B. Jewell
4 1.1 dbj * All rights reserved.
5 1.1 dbj *
6 1.1 dbj * Redistribution and use in source and binary forms, with or without
7 1.1 dbj * modification, are permitted provided that the following conditions
8 1.1 dbj * are met:
9 1.1 dbj * 1. Redistributions of source code must retain the above copyright
10 1.1 dbj * notice, this list of conditions and the following disclaimer.
11 1.1 dbj * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 dbj * notice, this list of conditions and the following disclaimer in the
13 1.1 dbj * documentation and/or other materials provided with the distribution.
14 1.1 dbj * 3. All advertising materials mentioning features or use of this software
15 1.1 dbj * must display the following acknowledgement:
16 1.1 dbj * This product includes software developed by Darrin B. Jewell
17 1.1 dbj * 4. The name of the author may not be used to endorse or promote products
18 1.1 dbj * derived from this software without specific prior written permission
19 1.1 dbj *
20 1.1 dbj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 dbj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 dbj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 dbj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 dbj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 dbj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 dbj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 dbj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 dbj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 dbj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 dbj */
31 1.1 dbj
32 1.1 dbj #include <sys/param.h>
33 1.1 dbj #include <sys/systm.h>
34 1.1 dbj #include <sys/mbuf.h>
35 1.1 dbj #include <sys/syslog.h>
36 1.1 dbj #include <sys/socket.h>
37 1.1 dbj #include <sys/device.h>
38 1.1 dbj #include <sys/malloc.h>
39 1.1 dbj #include <sys/ioctl.h>
40 1.1 dbj #include <sys/errno.h>
41 1.1 dbj
42 1.1 dbj #include <machine/autoconf.h>
43 1.1 dbj #include <machine/cpu.h>
44 1.1 dbj #include <machine/intr.h>
45 1.1 dbj
46 1.1 dbj #include <next68k/next68k/isr.h>
47 1.1 dbj
48 1.1 dbj #define _GENERIC_BUS_DMA_PRIVATE
49 1.1 dbj #include <machine/bus.h>
50 1.1 dbj
51 1.1 dbj #include "nextdmareg.h"
52 1.1 dbj #include "nextdmavar.h"
53 1.1 dbj
54 1.1 dbj #if 0
55 1.1 dbj #define ND_DEBUG
56 1.1 dbj #endif
57 1.1 dbj
58 1.1 dbj #if defined(ND_DEBUG)
59 1.1 dbj #define DPRINTF(x) printf x;
60 1.1 dbj #else
61 1.1 dbj #define DPRINTF(x)
62 1.1 dbj #endif
63 1.1 dbj
64 1.1 dbj /* @@@ for debugging */
65 1.1 dbj struct nextdma_config *debugernd;
66 1.1 dbj struct nextdma_config *debugexnd;
67 1.1 dbj
68 1.1 dbj int nextdma_intr __P((void *));
69 1.1 dbj void next_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
70 1.1 dbj bus_size_t, int));
71 1.1 dbj int next_dma_continue __P((struct nextdma_config *));
72 1.1 dbj void next_dma_rotate __P((struct nextdma_config *));
73 1.1 dbj
74 1.1 dbj void next_dma_setup_cont_regs __P((struct nextdma_config *));
75 1.1 dbj void next_dma_setup_curr_regs __P((struct nextdma_config *));
76 1.1 dbj
77 1.1 dbj void next_dma_print __P((struct nextdma_config *));
78 1.1 dbj
79 1.1 dbj void
80 1.1 dbj nextdma_config(nd)
81 1.1 dbj struct nextdma_config *nd;
82 1.1 dbj {
83 1.1 dbj /* Initialize the dma_tag. As a hack, we currently
84 1.1 dbj * put the dma tag in the structure itself. It shouldn't be there.
85 1.1 dbj */
86 1.1 dbj
87 1.1 dbj {
88 1.1 dbj bus_dma_tag_t t;
89 1.1 dbj t = &nd->_nd_dmat;
90 1.1 dbj t->_cookie = nd;
91 1.1 dbj t->_get_tag = NULL; /* lose */
92 1.1 dbj t->_dmamap_create = _bus_dmamap_create;
93 1.1 dbj t->_dmamap_destroy = _bus_dmamap_destroy;
94 1.1 dbj t->_dmamap_load = _bus_dmamap_load_direct;
95 1.1 dbj t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
96 1.1 dbj t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
97 1.1 dbj t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
98 1.1 dbj t->_dmamap_unload = _bus_dmamap_unload;
99 1.1 dbj t->_dmamap_sync = next_dmamap_sync;
100 1.1 dbj
101 1.1 dbj t->_dmamem_alloc = _bus_dmamem_alloc;
102 1.1 dbj t->_dmamem_free = _bus_dmamem_free;
103 1.1 dbj t->_dmamem_map = _bus_dmamem_map;
104 1.1 dbj t->_dmamem_unmap = _bus_dmamem_unmap;
105 1.1 dbj t->_dmamem_mmap = _bus_dmamem_mmap;
106 1.1 dbj
107 1.1 dbj nd->nd_dmat = t;
108 1.1 dbj }
109 1.1 dbj
110 1.1 dbj /* @@@ for debugging */
111 1.1 dbj if (nd->nd_intr == NEXT_I_ENETR_DMA) {
112 1.1 dbj debugernd = nd;
113 1.1 dbj }
114 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
115 1.1 dbj debugexnd = nd;
116 1.1 dbj }
117 1.1 dbj
118 1.1 dbj nextdma_init(nd);
119 1.1 dbj
120 1.1 dbj isrlink_autovec(nextdma_intr, nd, NEXT_I_IPL(nd->nd_intr), 10);
121 1.1 dbj INTR_ENABLE(nd->nd_intr);
122 1.1 dbj }
123 1.1 dbj
124 1.1 dbj void
125 1.1 dbj nextdma_init(nd)
126 1.1 dbj struct nextdma_config *nd;
127 1.1 dbj {
128 1.1 dbj DPRINTF(("DMA init ipl (%ld) intr(0x%b)\n",
129 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
130 1.1 dbj
131 1.1 dbj /* @@@ should probably check and free these maps */
132 1.1 dbj nd->_nd_map = NULL;
133 1.1 dbj nd->_nd_idx = 0;
134 1.1 dbj nd->_nd_map_cont = NULL;
135 1.1 dbj nd->_nd_idx_cont = 0;
136 1.1 dbj
137 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
138 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
139 1.1 dbj DMACSR_INITBUF | DMACSR_CLRCOMPLETE | DMACSR_RESET);
140 1.1 dbj
141 1.1 dbj next_dma_setup_curr_regs(nd);
142 1.1 dbj next_dma_setup_cont_regs(nd);
143 1.1 dbj
144 1.1 dbj #if 0 && defined(DIAGNOSTIC)
145 1.1 dbj /* Today, my computer (mourning) appears to fail this test.
146 1.1 dbj * yesterday, another NeXT (milo) didn't have this problem
147 1.1 dbj * Darrin B. Jewell <jewell (at) mit.edu> Mon May 25 07:53:05 1998
148 1.1 dbj */
149 1.1 dbj {
150 1.1 dbj u_long state;
151 1.1 dbj state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
152 1.1 dbj state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
153 1.1 dbj state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
154 1.1 dbj DMACSR_SUPDATE | DMACSR_ENABLE);
155 1.1 dbj
156 1.1 dbj if (state) {
157 1.1 dbj next_dma_print(nd);
158 1.1 dbj panic("DMA did not reset\n");
159 1.1 dbj }
160 1.1 dbj }
161 1.1 dbj #endif
162 1.1 dbj }
163 1.1 dbj
164 1.1 dbj void
165 1.1 dbj nextdma_reset(nd)
166 1.1 dbj struct nextdma_config *nd;
167 1.1 dbj {
168 1.1 dbj int s;
169 1.1 dbj s = spldma(); /* @@@ should this be splimp()? */
170 1.1 dbj nextdma_init(nd);
171 1.1 dbj splx(s);
172 1.1 dbj }
173 1.1 dbj
174 1.1 dbj /****************************************************************/
175 1.1 dbj
176 1.1 dbj /* If the next had multiple busses, this should probably
177 1.1 dbj * go elsewhere, but it is here anyway */
178 1.1 dbj void
179 1.1 dbj next_dmamap_sync(t, map, offset, len, ops)
180 1.1 dbj bus_dma_tag_t t;
181 1.1 dbj bus_dmamap_t map;
182 1.1 dbj bus_addr_t offset;
183 1.1 dbj bus_size_t len;
184 1.1 dbj int ops;
185 1.1 dbj {
186 1.1 dbj /* flush/purge the cache.
187 1.1 dbj * assumes pointers are aligned
188 1.1 dbj * should probably be fixed to use offset and len
189 1.1 dbj */
190 1.1 dbj if (ops & BUS_DMASYNC_PREWRITE) {
191 1.1 dbj int i;
192 1.1 dbj for(i=0;i<map->dm_nsegs;i++) {
193 1.1 dbj bus_addr_t p = map->dm_segs[i].ds_addr;
194 1.1 dbj bus_addr_t e = p+map->dm_segs[i].ds_len;
195 1.1 dbj while(p<e) {
196 1.1 dbj DCFL(p); /* flush */
197 1.1 dbj p += 16; /* cache line length */
198 1.1 dbj }
199 1.1 dbj }
200 1.1 dbj }
201 1.1 dbj
202 1.1 dbj if (ops & BUS_DMASYNC_POSTREAD) {
203 1.1 dbj int i;
204 1.1 dbj for(i=0;i<map->dm_nsegs;i++) {
205 1.1 dbj bus_addr_t p = map->dm_segs[i].ds_addr;
206 1.1 dbj bus_addr_t e = p+map->dm_segs[i].ds_len;
207 1.1 dbj while(p<e) {
208 1.1 dbj DCPL(p); /* purge */
209 1.1 dbj p += 16; /* cache line length */
210 1.1 dbj }
211 1.1 dbj }
212 1.1 dbj }
213 1.1 dbj }
214 1.1 dbj
215 1.1 dbj /****************************************************************/
216 1.1 dbj
217 1.1 dbj
218 1.1 dbj /* Call the completed and continue callbacks to try to fill
219 1.1 dbj * in the dma continue buffers.
220 1.1 dbj */
221 1.1 dbj void
222 1.1 dbj next_dma_rotate(nd)
223 1.1 dbj struct nextdma_config *nd;
224 1.1 dbj {
225 1.1 dbj
226 1.1 dbj DPRINTF(("DMA next_dma_rotate()\n"));
227 1.1 dbj
228 1.1 dbj /* If we've reached the end of the current map, then inform
229 1.1 dbj * that we've completed that map.
230 1.1 dbj */
231 1.1 dbj if (nd->_nd_map && ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs)) {
232 1.1 dbj if (nd->nd_completed_cb)
233 1.1 dbj (*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
234 1.1 dbj }
235 1.1 dbj
236 1.1 dbj /* Rotate the continue map into the current map */
237 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
238 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
239 1.1 dbj
240 1.1 dbj if ((!nd->_nd_map_cont) ||
241 1.1 dbj ((nd->_nd_map_cont) &&
242 1.1 dbj (++nd->_nd_idx_cont >= nd->_nd_map_cont->dm_nsegs))) {
243 1.1 dbj if (nd->nd_continue_cb) {
244 1.1 dbj nd->_nd_map_cont = (*nd->nd_continue_cb)(nd->nd_cb_arg);
245 1.1 dbj } else {
246 1.1 dbj nd->_nd_map_cont = 0;
247 1.1 dbj }
248 1.1 dbj nd->_nd_idx_cont = 0;
249 1.1 dbj }
250 1.1 dbj }
251 1.1 dbj
252 1.1 dbj void
253 1.1 dbj next_dma_setup_cont_regs(nd)
254 1.1 dbj struct nextdma_config *nd;
255 1.1 dbj {
256 1.1 dbj DPRINTF(("DMA next_dma_setup_regs()\n"));
257 1.1 dbj
258 1.1 dbj if (nd->_nd_map_cont) {
259 1.1 dbj
260 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
261 1.1 dbj /* Ethernet transmit needs secret magic */
262 1.1 dbj
263 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
264 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
265 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
266 1.1 dbj ((nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
267 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len)
268 1.1 dbj + 0x0) | 0x80000000);
269 1.1 dbj } else {
270 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
271 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
272 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
273 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
274 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
275 1.1 dbj }
276 1.1 dbj
277 1.1 dbj } else {
278 1.1 dbj
279 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,0);
280 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP, 0);
281 1.1 dbj }
282 1.1 dbj
283 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START,
284 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START));
285 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP,
286 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP));
287 1.1 dbj
288 1.1 dbj }
289 1.1 dbj
290 1.1 dbj void
291 1.1 dbj next_dma_setup_curr_regs(nd)
292 1.1 dbj struct nextdma_config *nd;
293 1.1 dbj {
294 1.1 dbj DPRINTF(("DMA next_dma_setup_curr_regs()\n"));
295 1.1 dbj
296 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
297 1.1 dbj /* Ethernet transmit needs secret magic */
298 1.1 dbj
299 1.1 dbj if (nd->_nd_map) {
300 1.1 dbj
301 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,
302 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
303 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
304 1.1 dbj ((nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
305 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len)
306 1.1 dbj + 0x0) | 0x80000000);
307 1.1 dbj } else {
308 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,0);
309 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, 0);
310 1.1 dbj
311 1.1 dbj }
312 1.1 dbj
313 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT,
314 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF));
315 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT,
316 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT));
317 1.1 dbj
318 1.1 dbj } else {
319 1.1 dbj
320 1.1 dbj if (nd->_nd_map) {
321 1.1 dbj
322 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT,
323 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
324 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
325 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
326 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
327 1.1 dbj } else {
328 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT,0);
329 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, 0);
330 1.1 dbj
331 1.1 dbj }
332 1.1 dbj
333 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT,
334 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT));
335 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT,
336 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT));
337 1.1 dbj
338 1.1 dbj }
339 1.1 dbj
340 1.1 dbj }
341 1.1 dbj
342 1.1 dbj
343 1.1 dbj /* This routine is used for debugging */
344 1.1 dbj
345 1.1 dbj void
346 1.1 dbj next_dma_print(nd)
347 1.1 dbj struct nextdma_config *nd;
348 1.1 dbj {
349 1.1 dbj u_long dd_csr;
350 1.1 dbj u_long dd_next;
351 1.1 dbj u_long dd_next_initbuf;
352 1.1 dbj u_long dd_limit;
353 1.1 dbj u_long dd_start;
354 1.1 dbj u_long dd_stop;
355 1.1 dbj u_long dd_saved_next;
356 1.1 dbj u_long dd_saved_limit;
357 1.1 dbj u_long dd_saved_start;
358 1.1 dbj u_long dd_saved_stop;
359 1.1 dbj
360 1.1 dbj /* Read all of the registers before we print anything out,
361 1.1 dbj * in case something changes
362 1.1 dbj */
363 1.1 dbj dd_csr = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
364 1.1 dbj dd_next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
365 1.1 dbj dd_next_initbuf = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
366 1.1 dbj dd_limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
367 1.1 dbj dd_start = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START);
368 1.1 dbj dd_stop = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP);
369 1.1 dbj dd_saved_next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
370 1.1 dbj dd_saved_limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
371 1.1 dbj dd_saved_start = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START);
372 1.1 dbj dd_saved_stop = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP);
373 1.1 dbj
374 1.1 dbj if (nd->_nd_map) {
375 1.1 dbj printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
376 1.1 dbj nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
377 1.1 dbj printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_len = %d\n",
378 1.1 dbj nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
379 1.1 dbj } else {
380 1.1 dbj printf("NDMAP: nd->_nd_map = NULL\n");
381 1.1 dbj }
382 1.1 dbj if (nd->_nd_map_cont) {
383 1.1 dbj printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
384 1.1 dbj nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
385 1.1 dbj printf("NDMAP: nd->_nd_map_cont->dm_segs[%d] = %d\n",
386 1.1 dbj nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
387 1.1 dbj } else {
388 1.1 dbj printf("NDMAP: nd->_nd_map_cont = NULL\n");
389 1.1 dbj }
390 1.1 dbj
391 1.1 dbj printf("NDMAP: dd->dd_csr = 0x%b\n", dd_csr, DMACSR_BITS);
392 1.1 dbj printf("NDMAP: dd->dd_saved_next = 0x%08x\n", dd_saved_next);
393 1.1 dbj printf("NDMAP: dd->dd_saved_limit = 0x%08x\n", dd_saved_limit);
394 1.1 dbj printf("NDMAP: dd->dd_saved_start = 0x%08x\n", dd_saved_start);
395 1.1 dbj printf("NDMAP: dd->dd_saved_stop = 0x%08x\n", dd_saved_stop);
396 1.1 dbj printf("NDMAP: dd->dd_next = 0x%08x\n", dd_next);
397 1.1 dbj printf("NDMAP: dd->dd_next_initbuf = 0x%08x\n", dd_next_initbuf);
398 1.1 dbj printf("NDMAP: dd->dd_limit = 0x%08x\n", dd_limit);
399 1.1 dbj printf("NDMAP: dd->dd_start = 0x%08x\n", dd_start);
400 1.1 dbj printf("NDMAP: dd->dd_stop = 0x%08x\n", dd_stop);
401 1.1 dbj
402 1.1 dbj printf("NDMAP: interrupt ipl (%ld) intr(0x%b)\n",
403 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
404 1.1 dbj }
405 1.1 dbj
406 1.1 dbj /****************************************************************/
407 1.1 dbj
408 1.1 dbj int
409 1.1 dbj nextdma_intr(arg)
410 1.1 dbj void *arg;
411 1.1 dbj {
412 1.1 dbj struct nextdma_config *nd = arg;
413 1.1 dbj
414 1.1 dbj /* @@@ This is bogus, we can't be certain of arg's type
415 1.1 dbj * unless the interrupt is for us
416 1.1 dbj */
417 1.1 dbj
418 1.1 dbj if (!INTR_OCCURRED(nd->nd_intr)) return 0;
419 1.1 dbj /* Handle dma interrupts */
420 1.1 dbj
421 1.1 dbj #ifdef DIAGNOSTIC
422 1.1 dbj if (nd->nd_intr == NEXT_I_ENETR_DMA) {
423 1.1 dbj if (debugernd != nd) {
424 1.1 dbj panic("DMA incorrect handling of rx nd->nd_intr");
425 1.1 dbj }
426 1.1 dbj }
427 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
428 1.1 dbj if (debugexnd != nd) {
429 1.1 dbj panic("DMA incorrect handling of tx nd->nd_intr");
430 1.1 dbj }
431 1.1 dbj }
432 1.1 dbj #endif
433 1.1 dbj
434 1.1 dbj DPRINTF(("DMA interrupt ipl (%ld) intr(0x%b)\n",
435 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
436 1.1 dbj
437 1.1 dbj {
438 1.1 dbj int state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
439 1.1 dbj
440 1.1 dbj state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
441 1.1 dbj DMACSR_SUPDATE | DMACSR_ENABLE);
442 1.1 dbj
443 1.1 dbj if (state & DMACSR_BUSEXC) {
444 1.1 dbj #if 0 /* This bit seems to get set periodically and I don't know why */
445 1.1 dbj next_dma_print(nd);
446 1.1 dbj panic("Bus exception in DMA ipl (%ld) intr(0x%b)\n",
447 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
448 1.1 dbj #endif
449 1.1 dbj }
450 1.1 dbj
451 1.1 dbj #ifdef DIAGNOSTIC
452 1.1 dbj if (!(state & DMACSR_COMPLETE)) {
453 1.1 dbj next_dma_print(nd);
454 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
455 1.1 dbj panic("DMA ipl (%ld) intr(0x%b), DMACSR_COMPLETE not set in intr\n",
456 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
457 1.1 dbj }
458 1.1 dbj #endif
459 1.1 dbj
460 1.1 dbj /* Set the length of the segment to match actual length.
461 1.1 dbj * @@@ is it okay to resize dma segments here?
462 1.1 dbj * i should probably ask jason about this.
463 1.1 dbj */
464 1.1 dbj if (nd->_nd_map) {
465 1.1 dbj
466 1.1 dbj bus_addr_t next;
467 1.1 dbj bus_addr_t limit;
468 1.1 dbj
469 1.1 dbj #if 0
470 1.1 dbj if (state & DMACSR_ENABLE) {
471 1.1 dbj next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
472 1.1 dbj } else {
473 1.1 dbj next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
474 1.1 dbj }
475 1.1 dbj #else
476 1.1 dbj next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
477 1.1 dbj #endif
478 1.1 dbj limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
479 1.1 dbj
480 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
481 1.1 dbj limit &= ~0x80000000;
482 1.1 dbj }
483 1.1 dbj
484 1.1 dbj #ifdef DIAGNOSTIC
485 1.1 dbj if (next != nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr) {
486 1.1 dbj next_dma_print(nd);
487 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
488 1.1 dbj
489 1.1 dbj panic("DMA ipl (%ld) intr(0x%b), unexpected completed address\n",
490 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
491 1.1 dbj }
492 1.1 dbj #endif
493 1.1 dbj
494 1.1 dbj /* @@@ I observed a case where DMACSR_ENABLE wasn't set and
495 1.1 dbj * DD_SAVED_LIMIT didn't contain the expected limit value. This
496 1.1 dbj * should be tested, fixed, and removed. */
497 1.1 dbj
498 1.1 dbj if (((limit-next) > nd->_nd_map->dm_segs[nd->_nd_idx].ds_len)
499 1.1 dbj || (limit-next < 0)) {
500 1.1 dbj #if 0
501 1.1 dbj next_dma_print(nd);
502 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
503 1.1 dbj panic("DMA packlen: next = 0x%08x limit = 0x%08x\n",next,limit);
504 1.1 dbj #else
505 1.1 dbj DPRINTF(("DMA packlen: next = 0x%08x limit = 0x%08x",next,limit));
506 1.1 dbj #endif
507 1.1 dbj
508 1.1 dbj } else {
509 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len = limit - next;
510 1.1 dbj }
511 1.1 dbj }
512 1.1 dbj
513 1.1 dbj
514 1.1 dbj if ((state & DMACSR_ENABLE) == 0) {
515 1.1 dbj
516 1.1 dbj /* Non chaining interrupts shutdown immediately */
517 1.1 dbj if (!nd->nd_chaining_flag) {
518 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
519 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
520 1.1 dbj nd->_nd_map_cont = 0;
521 1.1 dbj nd->_nd_idx_cont = 0;
522 1.1 dbj }
523 1.1 dbj
524 1.1 dbj /* Call the completed callback for the last packet */
525 1.1 dbj if (nd->_nd_map && ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs)) {
526 1.1 dbj if (nd->nd_completed_cb)
527 1.1 dbj (*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
528 1.1 dbj }
529 1.1 dbj nd->_nd_map = 0;
530 1.1 dbj nd->_nd_idx = 0;
531 1.1 dbj
532 1.1 dbj if (nd->_nd_map_cont) {
533 1.1 dbj DPRINTF(("DMA ipl (%ld) intr(0x%b), restarting\n",
534 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
535 1.1 dbj
536 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
537 1.1 dbj DMACSR_SETSUPDATE | DMACSR_SETENABLE);
538 1.1 dbj
539 1.1 dbj } else {
540 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
541 1.1 dbj DMACSR_CLRCOMPLETE | DMACSR_RESET);
542 1.1 dbj DPRINTF(("DMA: enable not set w/o continue map, shutting down dma\n"));
543 1.1 dbj if (nd->nd_shutdown_cb) (*nd->nd_shutdown_cb)(nd->nd_cb_arg);
544 1.1 dbj }
545 1.1 dbj
546 1.1 dbj } else {
547 1.1 dbj next_dma_rotate(nd);
548 1.1 dbj next_dma_setup_cont_regs(nd);
549 1.1 dbj
550 1.1 dbj if (nd->_nd_map_cont) {
551 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
552 1.1 dbj DMACSR_SETSUPDATE | DMACSR_CLRCOMPLETE);
553 1.1 dbj } else {
554 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
555 1.1 dbj DMACSR_CLRCOMPLETE);
556 1.1 dbj }
557 1.1 dbj
558 1.1 dbj }
559 1.1 dbj
560 1.1 dbj }
561 1.1 dbj
562 1.1 dbj DPRINTF(("DMA exiting interrupt ipl (%ld) intr(0x%b)\n",
563 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
564 1.1 dbj
565 1.1 dbj return(1);
566 1.1 dbj }
567 1.1 dbj
568 1.1 dbj /*
569 1.1 dbj * Check to see if dma has finished for a channel */
570 1.1 dbj int
571 1.1 dbj nextdma_finished(nd)
572 1.1 dbj struct nextdma_config *nd;
573 1.1 dbj {
574 1.1 dbj int r;
575 1.1 dbj int s;
576 1.1 dbj s = spldma(); /* @@@ should this be splimp()? */
577 1.1 dbj r = (nd->_nd_map == NULL) && (nd->_nd_map_cont == NULL);
578 1.1 dbj splx(s);
579 1.1 dbj return(r);
580 1.1 dbj }
581 1.1 dbj
582 1.1 dbj void
583 1.1 dbj nextdma_start(nd, dmadir)
584 1.1 dbj struct nextdma_config *nd;
585 1.1 dbj u_long dmadir; /* DMACSR_READ or DMACSR_WRITE */
586 1.1 dbj {
587 1.1 dbj
588 1.1 dbj #ifdef DIAGNOSTIC
589 1.1 dbj if (!nextdma_finished(nd)) {
590 1.1 dbj panic("DMA trying to start before previous finished on intr(0x%b)\n",
591 1.1 dbj NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
592 1.1 dbj }
593 1.1 dbj #endif
594 1.1 dbj
595 1.1 dbj
596 1.1 dbj DPRINTF(("DMA start (%ld) intr(0x%b)\n",
597 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
598 1.1 dbj
599 1.1 dbj #ifdef DIAGNOSTIC
600 1.1 dbj if (nd->_nd_map) {
601 1.1 dbj next_dma_print(nd);
602 1.1 dbj panic("DMA: nextdma_start() with non null map\n");
603 1.1 dbj }
604 1.1 dbj if (nd->_nd_map_cont) {
605 1.1 dbj next_dma_print(nd);
606 1.1 dbj panic("DMA: nextdma_start() with non null continue map\n");
607 1.1 dbj }
608 1.1 dbj #endif
609 1.1 dbj
610 1.1 dbj next_dma_rotate(nd);
611 1.1 dbj
612 1.1 dbj #ifdef DIAGNOSTIC
613 1.1 dbj if (!nd->_nd_map_cont) {
614 1.1 dbj panic("No map available in nextdma_start()");
615 1.1 dbj }
616 1.1 dbj if (!DMA_BEGINALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_addr)) {
617 1.1 dbj panic("unaligned begin dma at start\n");
618 1.1 dbj }
619 1.1 dbj if (!DMA_ENDALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_addr +
620 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_len)) {
621 1.1 dbj panic("unaligned end dma at start\n");
622 1.1 dbj }
623 1.1 dbj #endif
624 1.1 dbj
625 1.1 dbj DPRINTF(("DMA initiating DMA %s of %d segments on intr(0x%b)\n",
626 1.1 dbj (dmadir == DMACSR_READ ? "read" : "write"), nd->_nd_map_cont->dm_nsegs,
627 1.1 dbj NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
628 1.1 dbj
629 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
630 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
631 1.1 dbj DMACSR_INITBUF | DMACSR_RESET | dmadir);
632 1.1 dbj
633 1.1 dbj next_dma_setup_cont_regs(nd);
634 1.1 dbj
635 1.1 dbj /* When starting DMA, we must put the continue map
636 1.1 dbj * into the current register. We reset the nd->_nd_map
637 1.1 dbj * pointer here to avoid duplicated completed callbacks
638 1.1 dbj * for the first buffer.
639 1.1 dbj */
640 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
641 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
642 1.1 dbj next_dma_setup_curr_regs(nd);
643 1.1 dbj nd->_nd_map = 0;
644 1.1 dbj nd->_nd_idx = 0;
645 1.1 dbj
646 1.1 dbj if (nd->nd_chaining_flag) {
647 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
648 1.1 dbj DMACSR_SETSUPDATE | DMACSR_SETENABLE);
649 1.1 dbj } else {
650 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
651 1.1 dbj DMACSR_SETENABLE);
652 1.1 dbj }
653 1.1 dbj
654 1.1 dbj }
655