nextdma.c revision 1.3 1 1.3 dbj /* $NetBSD: nextdma.c,v 1.3 1998/07/19 21:41:17 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.3 dbj * @@@ should probably be fixed to use offset and len args.
189 1.3 dbj * should also optimize this to work on pages for larger regions?
190 1.1 dbj */
191 1.1 dbj if (ops & BUS_DMASYNC_PREWRITE) {
192 1.1 dbj int i;
193 1.1 dbj for(i=0;i<map->dm_nsegs;i++) {
194 1.1 dbj bus_addr_t p = map->dm_segs[i].ds_addr;
195 1.1 dbj bus_addr_t e = p+map->dm_segs[i].ds_len;
196 1.1 dbj while(p<e) {
197 1.1 dbj DCFL(p); /* flush */
198 1.1 dbj p += 16; /* cache line length */
199 1.1 dbj }
200 1.1 dbj }
201 1.1 dbj }
202 1.1 dbj
203 1.1 dbj if (ops & BUS_DMASYNC_POSTREAD) {
204 1.1 dbj int i;
205 1.1 dbj for(i=0;i<map->dm_nsegs;i++) {
206 1.1 dbj bus_addr_t p = map->dm_segs[i].ds_addr;
207 1.1 dbj bus_addr_t e = p+map->dm_segs[i].ds_len;
208 1.1 dbj while(p<e) {
209 1.1 dbj DCPL(p); /* purge */
210 1.1 dbj p += 16; /* cache line length */
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
219 1.1 dbj /* Call the completed and continue callbacks to try to fill
220 1.1 dbj * in the dma continue buffers.
221 1.1 dbj */
222 1.1 dbj void
223 1.1 dbj next_dma_rotate(nd)
224 1.1 dbj struct nextdma_config *nd;
225 1.1 dbj {
226 1.1 dbj
227 1.1 dbj DPRINTF(("DMA next_dma_rotate()\n"));
228 1.1 dbj
229 1.1 dbj /* If we've reached the end of the current map, then inform
230 1.1 dbj * that we've completed that map.
231 1.1 dbj */
232 1.1 dbj if (nd->_nd_map && ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs)) {
233 1.1 dbj if (nd->nd_completed_cb)
234 1.1 dbj (*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
235 1.1 dbj }
236 1.1 dbj
237 1.1 dbj /* Rotate the continue map into the current map */
238 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
239 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
240 1.1 dbj
241 1.1 dbj if ((!nd->_nd_map_cont) ||
242 1.1 dbj ((nd->_nd_map_cont) &&
243 1.1 dbj (++nd->_nd_idx_cont >= nd->_nd_map_cont->dm_nsegs))) {
244 1.1 dbj if (nd->nd_continue_cb) {
245 1.1 dbj nd->_nd_map_cont = (*nd->nd_continue_cb)(nd->nd_cb_arg);
246 1.1 dbj } else {
247 1.1 dbj nd->_nd_map_cont = 0;
248 1.1 dbj }
249 1.1 dbj nd->_nd_idx_cont = 0;
250 1.1 dbj }
251 1.1 dbj }
252 1.1 dbj
253 1.1 dbj void
254 1.1 dbj next_dma_setup_cont_regs(nd)
255 1.1 dbj struct nextdma_config *nd;
256 1.1 dbj {
257 1.1 dbj DPRINTF(("DMA next_dma_setup_regs()\n"));
258 1.1 dbj
259 1.1 dbj if (nd->_nd_map_cont) {
260 1.1 dbj
261 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
262 1.1 dbj /* Ethernet transmit needs secret magic */
263 1.1 dbj
264 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
265 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
266 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
267 1.1 dbj ((nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
268 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len)
269 1.1 dbj + 0x0) | 0x80000000);
270 1.1 dbj } else {
271 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
272 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
273 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
274 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
275 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
276 1.1 dbj }
277 1.1 dbj
278 1.1 dbj } else {
279 1.1 dbj
280 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,0);
281 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP, 0);
282 1.1 dbj }
283 1.1 dbj
284 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START,
285 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START));
286 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP,
287 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP));
288 1.1 dbj
289 1.1 dbj }
290 1.1 dbj
291 1.1 dbj void
292 1.1 dbj next_dma_setup_curr_regs(nd)
293 1.1 dbj struct nextdma_config *nd;
294 1.1 dbj {
295 1.1 dbj DPRINTF(("DMA next_dma_setup_curr_regs()\n"));
296 1.1 dbj
297 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
298 1.1 dbj /* Ethernet transmit needs secret magic */
299 1.1 dbj
300 1.1 dbj if (nd->_nd_map) {
301 1.1 dbj
302 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,
303 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
304 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
305 1.1 dbj ((nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
306 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len)
307 1.1 dbj + 0x0) | 0x80000000);
308 1.1 dbj } else {
309 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,0);
310 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, 0);
311 1.1 dbj
312 1.1 dbj }
313 1.1 dbj
314 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT,
315 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF));
316 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT,
317 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT));
318 1.1 dbj
319 1.1 dbj } else {
320 1.1 dbj
321 1.1 dbj if (nd->_nd_map) {
322 1.1 dbj
323 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT,
324 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
325 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
326 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
327 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
328 1.1 dbj } else {
329 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT,0);
330 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, 0);
331 1.1 dbj
332 1.1 dbj }
333 1.1 dbj
334 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT,
335 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT));
336 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT,
337 1.1 dbj bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT));
338 1.1 dbj
339 1.1 dbj }
340 1.1 dbj
341 1.1 dbj }
342 1.1 dbj
343 1.1 dbj
344 1.1 dbj /* This routine is used for debugging */
345 1.1 dbj
346 1.1 dbj void
347 1.1 dbj next_dma_print(nd)
348 1.1 dbj struct nextdma_config *nd;
349 1.1 dbj {
350 1.1 dbj u_long dd_csr;
351 1.1 dbj u_long dd_next;
352 1.1 dbj u_long dd_next_initbuf;
353 1.1 dbj u_long dd_limit;
354 1.1 dbj u_long dd_start;
355 1.1 dbj u_long dd_stop;
356 1.1 dbj u_long dd_saved_next;
357 1.1 dbj u_long dd_saved_limit;
358 1.1 dbj u_long dd_saved_start;
359 1.1 dbj u_long dd_saved_stop;
360 1.1 dbj
361 1.1 dbj /* Read all of the registers before we print anything out,
362 1.1 dbj * in case something changes
363 1.1 dbj */
364 1.1 dbj dd_csr = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
365 1.1 dbj dd_next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
366 1.1 dbj dd_next_initbuf = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
367 1.1 dbj dd_limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
368 1.1 dbj dd_start = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START);
369 1.1 dbj dd_stop = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP);
370 1.1 dbj dd_saved_next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
371 1.1 dbj dd_saved_limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
372 1.1 dbj dd_saved_start = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START);
373 1.1 dbj dd_saved_stop = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP);
374 1.1 dbj
375 1.1 dbj if (nd->_nd_map) {
376 1.1 dbj printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
377 1.1 dbj nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
378 1.1 dbj printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_len = %d\n",
379 1.1 dbj nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
380 1.1 dbj } else {
381 1.1 dbj printf("NDMAP: nd->_nd_map = NULL\n");
382 1.1 dbj }
383 1.1 dbj if (nd->_nd_map_cont) {
384 1.1 dbj printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
385 1.1 dbj nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
386 1.2 dbj printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_len = %d\n",
387 1.1 dbj nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
388 1.1 dbj } else {
389 1.1 dbj printf("NDMAP: nd->_nd_map_cont = NULL\n");
390 1.1 dbj }
391 1.1 dbj
392 1.1 dbj printf("NDMAP: dd->dd_csr = 0x%b\n", dd_csr, DMACSR_BITS);
393 1.1 dbj printf("NDMAP: dd->dd_saved_next = 0x%08x\n", dd_saved_next);
394 1.1 dbj printf("NDMAP: dd->dd_saved_limit = 0x%08x\n", dd_saved_limit);
395 1.1 dbj printf("NDMAP: dd->dd_saved_start = 0x%08x\n", dd_saved_start);
396 1.1 dbj printf("NDMAP: dd->dd_saved_stop = 0x%08x\n", dd_saved_stop);
397 1.1 dbj printf("NDMAP: dd->dd_next = 0x%08x\n", dd_next);
398 1.1 dbj printf("NDMAP: dd->dd_next_initbuf = 0x%08x\n", dd_next_initbuf);
399 1.1 dbj printf("NDMAP: dd->dd_limit = 0x%08x\n", dd_limit);
400 1.1 dbj printf("NDMAP: dd->dd_start = 0x%08x\n", dd_start);
401 1.1 dbj printf("NDMAP: dd->dd_stop = 0x%08x\n", dd_stop);
402 1.1 dbj
403 1.1 dbj printf("NDMAP: interrupt ipl (%ld) intr(0x%b)\n",
404 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
405 1.1 dbj }
406 1.1 dbj
407 1.1 dbj /****************************************************************/
408 1.1 dbj
409 1.1 dbj int
410 1.1 dbj nextdma_intr(arg)
411 1.1 dbj void *arg;
412 1.1 dbj {
413 1.1 dbj struct nextdma_config *nd = arg;
414 1.1 dbj
415 1.1 dbj /* @@@ This is bogus, we can't be certain of arg's type
416 1.1 dbj * unless the interrupt is for us
417 1.1 dbj */
418 1.1 dbj
419 1.1 dbj if (!INTR_OCCURRED(nd->nd_intr)) return 0;
420 1.1 dbj /* Handle dma interrupts */
421 1.1 dbj
422 1.1 dbj #ifdef DIAGNOSTIC
423 1.1 dbj if (nd->nd_intr == NEXT_I_ENETR_DMA) {
424 1.1 dbj if (debugernd != nd) {
425 1.1 dbj panic("DMA incorrect handling of rx nd->nd_intr");
426 1.1 dbj }
427 1.1 dbj }
428 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
429 1.1 dbj if (debugexnd != nd) {
430 1.1 dbj panic("DMA incorrect handling of tx nd->nd_intr");
431 1.1 dbj }
432 1.1 dbj }
433 1.1 dbj #endif
434 1.1 dbj
435 1.1 dbj DPRINTF(("DMA interrupt ipl (%ld) intr(0x%b)\n",
436 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
437 1.1 dbj
438 1.1 dbj {
439 1.1 dbj int state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
440 1.1 dbj
441 1.1 dbj state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
442 1.1 dbj DMACSR_SUPDATE | DMACSR_ENABLE);
443 1.1 dbj
444 1.1 dbj if (state & DMACSR_BUSEXC) {
445 1.1 dbj #if 0 /* This bit seems to get set periodically and I don't know why */
446 1.1 dbj next_dma_print(nd);
447 1.1 dbj panic("Bus exception in DMA ipl (%ld) intr(0x%b)\n",
448 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
449 1.1 dbj #endif
450 1.1 dbj }
451 1.1 dbj
452 1.1 dbj #ifdef DIAGNOSTIC
453 1.1 dbj if (!(state & DMACSR_COMPLETE)) {
454 1.1 dbj next_dma_print(nd);
455 1.2 dbj #if 0 /* This bit doesn't seem to get set every once in a while,
456 1.2 dbj * and I don't know why. Let's try treating it as a spurious
457 1.2 dbj * interrupt. ie. report it and ignore the interrupt.
458 1.2 dbj */
459 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
460 1.1 dbj panic("DMA ipl (%ld) intr(0x%b), DMACSR_COMPLETE not set in intr\n",
461 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
462 1.2 dbj #else
463 1.2 dbj printf("DMA ipl (%ld) intr(0x%b), DMACSR_COMPLETE not set in intr\n",
464 1.2 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
465 1.2 dbj return(1);
466 1.2 dbj #endif
467 1.1 dbj }
468 1.1 dbj #endif
469 1.1 dbj
470 1.1 dbj /* Set the length of the segment to match actual length.
471 1.1 dbj * @@@ is it okay to resize dma segments here?
472 1.1 dbj * i should probably ask jason about this.
473 1.1 dbj */
474 1.1 dbj if (nd->_nd_map) {
475 1.1 dbj
476 1.1 dbj bus_addr_t next;
477 1.1 dbj bus_addr_t limit;
478 1.1 dbj
479 1.1 dbj #if 0
480 1.1 dbj if (state & DMACSR_ENABLE) {
481 1.1 dbj next = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
482 1.1 dbj } else {
483 1.1 dbj next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
484 1.1 dbj }
485 1.1 dbj #else
486 1.1 dbj next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
487 1.1 dbj #endif
488 1.1 dbj limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
489 1.1 dbj
490 1.1 dbj if (nd->nd_intr == NEXT_I_ENETX_DMA) {
491 1.1 dbj limit &= ~0x80000000;
492 1.1 dbj }
493 1.1 dbj
494 1.1 dbj #ifdef DIAGNOSTIC
495 1.1 dbj if (next != nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr) {
496 1.1 dbj next_dma_print(nd);
497 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
498 1.1 dbj
499 1.1 dbj panic("DMA ipl (%ld) intr(0x%b), unexpected completed address\n",
500 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
501 1.1 dbj }
502 1.1 dbj #endif
503 1.1 dbj
504 1.1 dbj /* @@@ I observed a case where DMACSR_ENABLE wasn't set and
505 1.1 dbj * DD_SAVED_LIMIT didn't contain the expected limit value. This
506 1.1 dbj * should be tested, fixed, and removed. */
507 1.1 dbj
508 1.1 dbj if (((limit-next) > nd->_nd_map->dm_segs[nd->_nd_idx].ds_len)
509 1.1 dbj || (limit-next < 0)) {
510 1.1 dbj #if 0
511 1.1 dbj next_dma_print(nd);
512 1.1 dbj printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
513 1.1 dbj panic("DMA packlen: next = 0x%08x limit = 0x%08x\n",next,limit);
514 1.1 dbj #else
515 1.1 dbj DPRINTF(("DMA packlen: next = 0x%08x limit = 0x%08x",next,limit));
516 1.1 dbj #endif
517 1.1 dbj
518 1.1 dbj } else {
519 1.1 dbj nd->_nd_map->dm_segs[nd->_nd_idx].ds_len = limit - next;
520 1.1 dbj }
521 1.1 dbj }
522 1.1 dbj
523 1.1 dbj
524 1.1 dbj if ((state & DMACSR_ENABLE) == 0) {
525 1.1 dbj
526 1.1 dbj /* Non chaining interrupts shutdown immediately */
527 1.1 dbj if (!nd->nd_chaining_flag) {
528 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
529 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
530 1.1 dbj nd->_nd_map_cont = 0;
531 1.1 dbj nd->_nd_idx_cont = 0;
532 1.1 dbj }
533 1.1 dbj
534 1.1 dbj /* Call the completed callback for the last packet */
535 1.1 dbj if (nd->_nd_map && ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs)) {
536 1.1 dbj if (nd->nd_completed_cb)
537 1.1 dbj (*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
538 1.1 dbj }
539 1.1 dbj nd->_nd_map = 0;
540 1.1 dbj nd->_nd_idx = 0;
541 1.1 dbj
542 1.1 dbj if (nd->_nd_map_cont) {
543 1.1 dbj DPRINTF(("DMA ipl (%ld) intr(0x%b), restarting\n",
544 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
545 1.1 dbj
546 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
547 1.1 dbj DMACSR_SETSUPDATE | DMACSR_SETENABLE);
548 1.1 dbj
549 1.1 dbj } else {
550 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
551 1.1 dbj DMACSR_CLRCOMPLETE | DMACSR_RESET);
552 1.1 dbj DPRINTF(("DMA: enable not set w/o continue map, shutting down dma\n"));
553 1.1 dbj if (nd->nd_shutdown_cb) (*nd->nd_shutdown_cb)(nd->nd_cb_arg);
554 1.1 dbj }
555 1.1 dbj
556 1.1 dbj } else {
557 1.1 dbj next_dma_rotate(nd);
558 1.1 dbj next_dma_setup_cont_regs(nd);
559 1.1 dbj
560 1.1 dbj if (nd->_nd_map_cont) {
561 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
562 1.1 dbj DMACSR_SETSUPDATE | DMACSR_CLRCOMPLETE);
563 1.1 dbj } else {
564 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
565 1.1 dbj DMACSR_CLRCOMPLETE);
566 1.1 dbj }
567 1.1 dbj
568 1.1 dbj }
569 1.1 dbj
570 1.1 dbj }
571 1.1 dbj
572 1.1 dbj DPRINTF(("DMA exiting interrupt ipl (%ld) intr(0x%b)\n",
573 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
574 1.1 dbj
575 1.1 dbj return(1);
576 1.1 dbj }
577 1.1 dbj
578 1.1 dbj /*
579 1.1 dbj * Check to see if dma has finished for a channel */
580 1.1 dbj int
581 1.1 dbj nextdma_finished(nd)
582 1.1 dbj struct nextdma_config *nd;
583 1.1 dbj {
584 1.1 dbj int r;
585 1.1 dbj int s;
586 1.1 dbj s = spldma(); /* @@@ should this be splimp()? */
587 1.1 dbj r = (nd->_nd_map == NULL) && (nd->_nd_map_cont == NULL);
588 1.1 dbj splx(s);
589 1.1 dbj return(r);
590 1.1 dbj }
591 1.1 dbj
592 1.1 dbj void
593 1.1 dbj nextdma_start(nd, dmadir)
594 1.1 dbj struct nextdma_config *nd;
595 1.1 dbj u_long dmadir; /* DMACSR_READ or DMACSR_WRITE */
596 1.1 dbj {
597 1.1 dbj
598 1.1 dbj #ifdef DIAGNOSTIC
599 1.1 dbj if (!nextdma_finished(nd)) {
600 1.1 dbj panic("DMA trying to start before previous finished on intr(0x%b)\n",
601 1.1 dbj NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
602 1.1 dbj }
603 1.1 dbj #endif
604 1.1 dbj
605 1.1 dbj
606 1.1 dbj DPRINTF(("DMA start (%ld) intr(0x%b)\n",
607 1.1 dbj NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
608 1.1 dbj
609 1.1 dbj #ifdef DIAGNOSTIC
610 1.1 dbj if (nd->_nd_map) {
611 1.1 dbj next_dma_print(nd);
612 1.1 dbj panic("DMA: nextdma_start() with non null map\n");
613 1.1 dbj }
614 1.1 dbj if (nd->_nd_map_cont) {
615 1.1 dbj next_dma_print(nd);
616 1.1 dbj panic("DMA: nextdma_start() with non null continue map\n");
617 1.1 dbj }
618 1.1 dbj #endif
619 1.1 dbj
620 1.1 dbj next_dma_rotate(nd);
621 1.1 dbj
622 1.1 dbj #ifdef DIAGNOSTIC
623 1.1 dbj if (!nd->_nd_map_cont) {
624 1.1 dbj panic("No map available in nextdma_start()");
625 1.1 dbj }
626 1.1 dbj if (!DMA_BEGINALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_addr)) {
627 1.1 dbj panic("unaligned begin dma at start\n");
628 1.1 dbj }
629 1.1 dbj if (!DMA_ENDALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_addr +
630 1.1 dbj nd->_nd_map_cont->dm_segs[nd->_nd_idx].ds_len)) {
631 1.1 dbj panic("unaligned end dma at start\n");
632 1.1 dbj }
633 1.1 dbj #endif
634 1.1 dbj
635 1.1 dbj DPRINTF(("DMA initiating DMA %s of %d segments on intr(0x%b)\n",
636 1.1 dbj (dmadir == DMACSR_READ ? "read" : "write"), nd->_nd_map_cont->dm_nsegs,
637 1.1 dbj NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
638 1.1 dbj
639 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
640 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
641 1.1 dbj DMACSR_INITBUF | DMACSR_RESET | dmadir);
642 1.1 dbj
643 1.1 dbj next_dma_setup_cont_regs(nd);
644 1.1 dbj
645 1.1 dbj /* When starting DMA, we must put the continue map
646 1.1 dbj * into the current register. We reset the nd->_nd_map
647 1.1 dbj * pointer here to avoid duplicated completed callbacks
648 1.1 dbj * for the first buffer.
649 1.1 dbj */
650 1.1 dbj nd->_nd_map = nd->_nd_map_cont;
651 1.1 dbj nd->_nd_idx = nd->_nd_idx_cont;
652 1.1 dbj next_dma_setup_curr_regs(nd);
653 1.1 dbj nd->_nd_map = 0;
654 1.1 dbj nd->_nd_idx = 0;
655 1.1 dbj
656 1.1 dbj if (nd->nd_chaining_flag) {
657 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
658 1.1 dbj DMACSR_SETSUPDATE | DMACSR_SETENABLE);
659 1.1 dbj } else {
660 1.1 dbj bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
661 1.1 dbj DMACSR_SETENABLE);
662 1.1 dbj }
663 1.1 dbj
664 1.1 dbj }
665