Home | History | Annotate | Line # | Download | only in dev
nextdma.c revision 1.18
      1  1.18  dbj /*	$NetBSD: nextdma.c,v 1.18 1999/08/17 05:09:13 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.5  dbj 
     46   1.5  dbj #include <m68k/cacheops.h>
     47   1.1  dbj 
     48   1.1  dbj #include <next68k/next68k/isr.h>
     49   1.1  dbj 
     50  1.16  dbj #define _NEXT68K_BUS_DMA_PRIVATE
     51   1.1  dbj #include <machine/bus.h>
     52   1.1  dbj 
     53   1.1  dbj #include "nextdmareg.h"
     54   1.1  dbj #include "nextdmavar.h"
     55   1.1  dbj 
     56   1.8  dbj #if 1
     57   1.1  dbj #define ND_DEBUG
     58   1.1  dbj #endif
     59   1.1  dbj 
     60   1.1  dbj #if defined(ND_DEBUG)
     61   1.8  dbj int nextdma_debug = 0;
     62   1.8  dbj #define DPRINTF(x) if (nextdma_debug) printf x;
     63   1.1  dbj #else
     64   1.1  dbj #define DPRINTF(x)
     65   1.1  dbj #endif
     66   1.1  dbj 
     67   1.1  dbj void next_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     68   1.1  dbj                        bus_size_t, int));
     69   1.1  dbj int next_dma_continue __P((struct nextdma_config *));
     70   1.1  dbj void next_dma_rotate __P((struct nextdma_config *));
     71   1.1  dbj 
     72   1.1  dbj void next_dma_setup_cont_regs __P((struct nextdma_config *));
     73   1.1  dbj void next_dma_setup_curr_regs __P((struct nextdma_config *));
     74   1.1  dbj 
     75   1.1  dbj void
     76   1.1  dbj nextdma_config(nd)
     77   1.1  dbj 	struct nextdma_config *nd;
     78   1.1  dbj {
     79   1.1  dbj 	/* Initialize the dma_tag. As a hack, we currently
     80   1.1  dbj 	 * put the dma tag in the structure itself.  It shouldn't be there.
     81   1.1  dbj 	 */
     82   1.1  dbj 
     83   1.1  dbj 	{
     84   1.1  dbj 		bus_dma_tag_t t;
     85   1.1  dbj 		t = &nd->_nd_dmat;
     86   1.1  dbj 		t->_cookie = nd;
     87   1.1  dbj 		t->_dmamap_create = _bus_dmamap_create;
     88   1.1  dbj 		t->_dmamap_destroy = _bus_dmamap_destroy;
     89   1.1  dbj 		t->_dmamap_load = _bus_dmamap_load_direct;
     90   1.1  dbj 		t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
     91   1.1  dbj 		t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
     92   1.1  dbj 		t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
     93   1.1  dbj 		t->_dmamap_unload = _bus_dmamap_unload;
     94  1.16  dbj 		t->_dmamap_sync = _bus_dmamap_sync;
     95   1.1  dbj 
     96   1.1  dbj 		t->_dmamem_alloc = _bus_dmamem_alloc;
     97   1.1  dbj 		t->_dmamem_free = _bus_dmamem_free;
     98   1.1  dbj 		t->_dmamem_map = _bus_dmamem_map;
     99   1.1  dbj 		t->_dmamem_unmap = _bus_dmamem_unmap;
    100   1.1  dbj 		t->_dmamem_mmap = _bus_dmamem_mmap;
    101   1.1  dbj 
    102   1.1  dbj 		nd->nd_dmat = t;
    103   1.1  dbj 	}
    104   1.1  dbj 
    105   1.1  dbj 	nextdma_init(nd);
    106   1.1  dbj 
    107  1.14  dbj 	isrlink_autovec(nextdma_intr, nd, NEXT_I_IPL(nd->nd_intr), 10);
    108  1.14  dbj 	INTR_ENABLE(nd->nd_intr);
    109   1.1  dbj }
    110   1.1  dbj 
    111   1.1  dbj void
    112   1.1  dbj nextdma_init(nd)
    113   1.1  dbj 	struct nextdma_config *nd;
    114   1.1  dbj {
    115   1.1  dbj   DPRINTF(("DMA init ipl (%ld) intr(0x%b)\n",
    116   1.1  dbj 			NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
    117   1.1  dbj 
    118   1.1  dbj 	/* @@@ should probably check and free these maps */
    119   1.1  dbj 	nd->_nd_map = NULL;
    120   1.1  dbj 	nd->_nd_idx = 0;
    121   1.1  dbj 	nd->_nd_map_cont = NULL;
    122   1.1  dbj 	nd->_nd_idx_cont = 0;
    123   1.1  dbj 
    124   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    125   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    126   1.1  dbj 			DMACSR_INITBUF | DMACSR_CLRCOMPLETE | DMACSR_RESET);
    127   1.1  dbj 
    128   1.1  dbj 	next_dma_setup_curr_regs(nd);
    129   1.1  dbj 	next_dma_setup_cont_regs(nd);
    130   1.1  dbj 
    131   1.1  dbj #if 0 && defined(DIAGNOSTIC)
    132   1.1  dbj 	/* Today, my computer (mourning) appears to fail this test.
    133   1.1  dbj 	 * yesterday, another NeXT (milo) didn't have this problem
    134   1.1  dbj 	 * Darrin B. Jewell <jewell (at) mit.edu>  Mon May 25 07:53:05 1998
    135   1.1  dbj 	 */
    136   1.1  dbj 	{
    137   1.1  dbj 		u_long state;
    138   1.1  dbj 		state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    139   1.1  dbj 		state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    140   1.1  dbj     state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
    141   1.1  dbj               DMACSR_SUPDATE | DMACSR_ENABLE);
    142   1.1  dbj 
    143   1.1  dbj 		if (state) {
    144   1.1  dbj 			next_dma_print(nd);
    145   1.1  dbj 			panic("DMA did not reset\n");
    146   1.1  dbj 		}
    147   1.1  dbj 	}
    148   1.1  dbj #endif
    149   1.1  dbj }
    150   1.1  dbj 
    151   1.4  dbj 
    152   1.1  dbj void
    153   1.1  dbj nextdma_reset(nd)
    154   1.1  dbj 	struct nextdma_config *nd;
    155   1.1  dbj {
    156   1.1  dbj 	int s;
    157  1.18  dbj 	s = spldma();
    158   1.8  dbj 
    159   1.8  dbj 	DPRINTF(("DMA reset\n"));
    160   1.8  dbj 
    161   1.8  dbj #if (defined(ND_DEBUG))
    162   1.8  dbj 	if (nextdma_debug) next_dma_print(nd);
    163   1.8  dbj #endif
    164   1.8  dbj 
    165   1.1  dbj 	nextdma_init(nd);
    166   1.1  dbj 	splx(s);
    167   1.1  dbj }
    168   1.1  dbj 
    169   1.1  dbj /****************************************************************/
    170   1.1  dbj 
    171   1.1  dbj 
    172   1.1  dbj /* Call the completed and continue callbacks to try to fill
    173   1.1  dbj  * in the dma continue buffers.
    174   1.1  dbj  */
    175   1.1  dbj void
    176   1.1  dbj next_dma_rotate(nd)
    177   1.1  dbj 	struct nextdma_config *nd;
    178   1.1  dbj {
    179   1.1  dbj 
    180   1.1  dbj 	DPRINTF(("DMA next_dma_rotate()\n"));
    181   1.1  dbj 
    182  1.16  dbj #ifdef DIAGNOSTIC
    183  1.16  dbj 	if (nd->_nd_map &&
    184  1.17  dbj 			nd->_nd_map->dm_segs[nd->_nd_idx].ds_xfer_len == 0x1234beef) {
    185  1.16  dbj 		next_dma_print(nd);
    186  1.18  dbj 		panic("DMA didn't set xfer length of segment");
    187  1.16  dbj 	}
    188  1.16  dbj #endif
    189  1.16  dbj 
    190   1.1  dbj 	/* If we've reached the end of the current map, then inform
    191   1.1  dbj 	 * that we've completed that map.
    192   1.1  dbj 	 */
    193   1.1  dbj 	if (nd->_nd_map && ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs)) {
    194   1.1  dbj 		if (nd->nd_completed_cb)
    195   1.1  dbj 			(*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
    196   1.1  dbj 	}
    197   1.1  dbj 
    198   1.1  dbj 	/* Rotate the continue map into the current map */
    199   1.1  dbj 	nd->_nd_map = nd->_nd_map_cont;
    200   1.1  dbj 	nd->_nd_idx = nd->_nd_idx_cont;
    201   1.1  dbj 
    202   1.1  dbj 	if ((!nd->_nd_map_cont) ||
    203   1.1  dbj 			((nd->_nd_map_cont) &&
    204   1.1  dbj 					(++nd->_nd_idx_cont >= nd->_nd_map_cont->dm_nsegs))) {
    205   1.1  dbj 		if (nd->nd_continue_cb) {
    206   1.1  dbj 			nd->_nd_map_cont = (*nd->nd_continue_cb)(nd->nd_cb_arg);
    207   1.1  dbj 		} else {
    208   1.1  dbj 			nd->_nd_map_cont = 0;
    209   1.1  dbj 		}
    210   1.1  dbj 		nd->_nd_idx_cont = 0;
    211   1.1  dbj 	}
    212   1.7  dbj 
    213   1.7  dbj #ifdef DIAGNOSTIC
    214  1.16  dbj 	if (nd->_nd_map) {
    215  1.17  dbj 		nd->_nd_map->dm_segs[nd->_nd_idx].ds_xfer_len = 0x1234beef;
    216  1.16  dbj 	}
    217  1.16  dbj #endif
    218  1.16  dbj 
    219  1.16  dbj #ifdef DIAGNOSTIC
    220   1.7  dbj 	if (nd->_nd_map_cont) {
    221  1.12  dbj 		if (!DMA_BEGINALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr)) {
    222  1.12  dbj 			next_dma_print(nd);
    223   1.7  dbj 			panic("DMA request unaligned at start\n");
    224   1.7  dbj 		}
    225  1.12  dbj 		if (!DMA_ENDALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
    226  1.12  dbj 				nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len)) {
    227  1.12  dbj 			next_dma_print(nd);
    228   1.7  dbj 			panic("DMA request unaligned at end\n");
    229   1.7  dbj 		}
    230   1.7  dbj 	}
    231   1.7  dbj #endif
    232   1.7  dbj 
    233   1.1  dbj }
    234   1.1  dbj 
    235   1.1  dbj void
    236   1.1  dbj next_dma_setup_cont_regs(nd)
    237   1.1  dbj 	struct nextdma_config *nd;
    238   1.1  dbj {
    239   1.1  dbj 	DPRINTF(("DMA next_dma_setup_regs()\n"));
    240   1.1  dbj 
    241   1.1  dbj 	if (nd->_nd_map_cont) {
    242   1.1  dbj 
    243   1.1  dbj 		if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    244   1.1  dbj 			/* Ethernet transmit needs secret magic */
    245   1.1  dbj 
    246   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
    247   1.1  dbj 					nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
    248   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
    249   1.1  dbj 					((nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
    250   1.1  dbj 							nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len)
    251   1.1  dbj 							+ 0x0) | 0x80000000);
    252  1.15  dbj 
    253  1.15  dbj 		}
    254  1.15  dbj 		else {
    255   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START,
    256   1.1  dbj 					nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
    257   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP,
    258   1.1  dbj 					nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
    259   1.1  dbj 					nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
    260   1.1  dbj 		}
    261   1.1  dbj 
    262   1.1  dbj 	} else {
    263   1.1  dbj 
    264   1.6  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START, 0xdeadbeef);
    265   1.6  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP, 0xdeadbeef);
    266   1.1  dbj 	}
    267   1.1  dbj 
    268  1.10  dbj #if 1 /* 0xfeedbeef in these registers leads to instability.  it will
    269  1.10  dbj 			 * panic after a short while with 0xfeedbeef in the DD_START and DD_STOP
    270  1.10  dbj 			 * registers.  I suspect that an unexpected hardware restart
    271  1.10  dbj 			 * is cycling the bogus values into the active registers.  Until
    272  1.10  dbj 			 * that is understood, we seed these with the same as DD_START and DD_STOP
    273  1.10  dbj 			 */
    274   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START,
    275   1.1  dbj 			bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START));
    276   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP,
    277   1.1  dbj 			bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP));
    278   1.7  dbj #else
    279   1.7  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START, 0xfeedbeef);
    280   1.7  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP, 0xfeedbeef);
    281   1.7  dbj #endif
    282   1.1  dbj 
    283   1.1  dbj }
    284   1.1  dbj 
    285   1.1  dbj void
    286   1.1  dbj next_dma_setup_curr_regs(nd)
    287   1.1  dbj 	struct nextdma_config *nd;
    288   1.1  dbj {
    289   1.1  dbj 	DPRINTF(("DMA next_dma_setup_curr_regs()\n"));
    290   1.1  dbj 
    291  1.15  dbj 
    292  1.15  dbj 	if (nd->_nd_map) {
    293  1.15  dbj 
    294  1.15  dbj 		if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    295   1.1  dbj 			/* Ethernet transmit needs secret magic */
    296   1.1  dbj 
    297   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,
    298   1.1  dbj 					nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
    299   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
    300   1.1  dbj 					((nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
    301   1.1  dbj 							nd->_nd_map->dm_segs[nd->_nd_idx].ds_len)
    302   1.1  dbj 							+ 0x0) | 0x80000000);
    303   1.1  dbj 
    304   1.1  dbj 		}
    305  1.15  dbj 		else {
    306   1.9  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,
    307   1.1  dbj 					nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
    308   1.1  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT,
    309   1.1  dbj 					nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
    310   1.1  dbj 					nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    311  1.15  dbj 		}
    312   1.1  dbj 
    313  1.15  dbj 	} else {
    314  1.15  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF,0xdeadbeef);
    315  1.15  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, 0xdeadbeef);
    316  1.15  dbj 	}
    317   1.1  dbj 
    318  1.10  dbj #if 1  /* See comment in next_dma_setup_cont_regs() above */
    319   1.1  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT,
    320   1.9  dbj 				bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF));
    321   1.1  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT,
    322   1.1  dbj 				bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT));
    323   1.7  dbj #else
    324   1.7  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT, 0xfeedbeef);
    325   1.7  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT, 0xfeedbeef);
    326   1.7  dbj #endif
    327   1.1  dbj 
    328   1.1  dbj }
    329   1.1  dbj 
    330   1.1  dbj 
    331   1.1  dbj /* This routine is used for debugging */
    332   1.1  dbj 
    333   1.1  dbj void
    334   1.1  dbj next_dma_print(nd)
    335   1.1  dbj 	struct nextdma_config *nd;
    336   1.1  dbj {
    337   1.1  dbj 	u_long dd_csr;
    338   1.1  dbj 	u_long dd_next;
    339   1.1  dbj 	u_long dd_next_initbuf;
    340   1.1  dbj 	u_long dd_limit;
    341   1.1  dbj 	u_long dd_start;
    342   1.1  dbj 	u_long dd_stop;
    343   1.1  dbj 	u_long dd_saved_next;
    344   1.1  dbj 	u_long dd_saved_limit;
    345   1.1  dbj 	u_long dd_saved_start;
    346   1.1  dbj 	u_long dd_saved_stop;
    347   1.1  dbj 
    348   1.1  dbj   /* Read all of the registers before we print anything out,
    349   1.1  dbj 	 * in case something changes
    350   1.1  dbj 	 */
    351   1.1  dbj 	dd_csr          = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    352   1.1  dbj 	dd_next         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    353   1.1  dbj 	dd_next_initbuf = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
    354   1.1  dbj 	dd_limit        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    355   1.1  dbj 	dd_start        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START);
    356   1.1  dbj 	dd_stop         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP);
    357   1.1  dbj 	dd_saved_next   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    358   1.1  dbj 	dd_saved_limit  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    359   1.1  dbj 	dd_saved_start  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START);
    360   1.1  dbj 	dd_saved_stop   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP);
    361   1.1  dbj 
    362  1.12  dbj 	/* NDMAP is Next DMA Print (really!) */
    363  1.12  dbj 
    364  1.12  dbj 	printf("NDMAP: nd->_nd_dmadir = 0x%08x\n",nd->_nd_dmadir);
    365  1.12  dbj 
    366   1.1  dbj 	if (nd->_nd_map) {
    367  1.11  dbj 		printf("NDMAP: nd->_nd_map->dm_mapsize = %d\n",
    368  1.11  dbj 				nd->_nd_map->dm_mapsize);
    369  1.11  dbj 		printf("NDMAP: nd->_nd_map->dm_nsegs = %d\n",
    370  1.11  dbj 				nd->_nd_map->dm_nsegs);
    371   1.1  dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    372   1.1  dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
    373   1.1  dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_len = %d\n",
    374   1.1  dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    375  1.17  dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_xfer_len = %d\n",
    376  1.17  dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_xfer_len);
    377   1.1  dbj 	} else {
    378   1.1  dbj 		printf("NDMAP: nd->_nd_map = NULL\n");
    379   1.1  dbj 	}
    380   1.1  dbj 	if (nd->_nd_map_cont) {
    381  1.11  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_mapsize = %d\n",
    382  1.11  dbj 				nd->_nd_map_cont->dm_mapsize);
    383  1.11  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_nsegs = %d\n",
    384  1.11  dbj 				nd->_nd_map_cont->dm_nsegs);
    385   1.1  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    386   1.1  dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
    387   1.2  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_len = %d\n",
    388   1.1  dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
    389  1.17  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_xfer_len = %d\n",
    390  1.17  dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_xfer_len);
    391   1.1  dbj 	} else {
    392   1.1  dbj 		printf("NDMAP: nd->_nd_map_cont = NULL\n");
    393   1.1  dbj 	}
    394   1.1  dbj 
    395   1.1  dbj 	printf("NDMAP: dd->dd_csr          = 0x%b\n",   dd_csr,   DMACSR_BITS);
    396   1.1  dbj 	printf("NDMAP: dd->dd_saved_next   = 0x%08x\n", dd_saved_next);
    397   1.1  dbj 	printf("NDMAP: dd->dd_saved_limit  = 0x%08x\n", dd_saved_limit);
    398   1.1  dbj 	printf("NDMAP: dd->dd_saved_start  = 0x%08x\n", dd_saved_start);
    399   1.1  dbj 	printf("NDMAP: dd->dd_saved_stop   = 0x%08x\n", dd_saved_stop);
    400   1.1  dbj 	printf("NDMAP: dd->dd_next         = 0x%08x\n", dd_next);
    401   1.1  dbj 	printf("NDMAP: dd->dd_next_initbuf = 0x%08x\n", dd_next_initbuf);
    402   1.1  dbj 	printf("NDMAP: dd->dd_limit        = 0x%08x\n", dd_limit);
    403   1.1  dbj 	printf("NDMAP: dd->dd_start        = 0x%08x\n", dd_start);
    404   1.1  dbj 	printf("NDMAP: dd->dd_stop         = 0x%08x\n", dd_stop);
    405   1.1  dbj 
    406   1.1  dbj 	printf("NDMAP: interrupt ipl (%ld) intr(0x%b)\n",
    407   1.1  dbj 			NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
    408   1.1  dbj }
    409   1.1  dbj 
    410   1.1  dbj /****************************************************************/
    411   1.1  dbj 
    412   1.1  dbj int
    413   1.1  dbj nextdma_intr(arg)
    414   1.1  dbj      void *arg;
    415   1.1  dbj {
    416   1.1  dbj   /* @@@ This is bogus, we can't be certain of arg's type
    417  1.18  dbj 	 * unless the interrupt is for us.  For now we successfully
    418  1.18  dbj 	 * cheat because DMA interrupts are the only things invoked
    419  1.18  dbj 	 * at this interrupt level.
    420   1.1  dbj 	 */
    421  1.18  dbj   struct nextdma_config *nd = arg;
    422   1.1  dbj 
    423   1.1  dbj   if (!INTR_OCCURRED(nd->nd_intr)) return 0;
    424   1.1  dbj   /* Handle dma interrupts */
    425   1.1  dbj 
    426   1.1  dbj   DPRINTF(("DMA interrupt ipl (%ld) intr(0x%b)\n",
    427   1.1  dbj           NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
    428   1.1  dbj 
    429   1.7  dbj #ifdef DIAGNOSTIC
    430   1.7  dbj 	if (!nd->_nd_map) {
    431   1.7  dbj 		next_dma_print(nd);
    432   1.7  dbj 		panic("DMA missing current map in interrupt!\n");
    433   1.7  dbj 	}
    434   1.7  dbj #endif
    435   1.7  dbj 
    436   1.1  dbj   {
    437   1.1  dbj     int state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    438   1.1  dbj 
    439   1.7  dbj #ifdef DIAGNOSTIC
    440   1.7  dbj 		if (!(state & DMACSR_COMPLETE)) {
    441   1.1  dbj 			next_dma_print(nd);
    442   1.7  dbj 			printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    443   1.7  dbj 			panic("DMA  ipl (%ld) intr(0x%b), DMACSR_COMPLETE not set in intr\n",
    444   1.7  dbj 					NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
    445   1.7  dbj 		}
    446   1.1  dbj #endif
    447   1.1  dbj 
    448   1.7  dbj #if 0 /* This bit gets set sometimes & I don't know why. */
    449   1.1  dbj #ifdef DIAGNOSTIC
    450   1.7  dbj 		if (state & DMACSR_BUSEXC) {
    451   1.1  dbj 			next_dma_print(nd);
    452   1.1  dbj 			printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    453   1.1  dbj 			panic("DMA  ipl (%ld) intr(0x%b), DMACSR_COMPLETE not set in intr\n",
    454   1.1  dbj 					NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
    455   1.7  dbj 		}
    456   1.7  dbj #endif
    457   1.7  dbj #endif
    458   1.7  dbj 
    459   1.7  dbj 		/* Check to see if we are expecting dma to shut down */
    460   1.7  dbj 		if (!nd->_nd_map_cont) {
    461   1.7  dbj 
    462   1.7  dbj #ifdef DIAGNOSTIC
    463  1.12  dbj #if 1 /* Sometimes the DMA registers have totally bogus values when read.
    464   1.7  dbj 			 * Until that's understood, we skip this check
    465   1.7  dbj 			 */
    466   1.7  dbj 
    467   1.7  dbj 			/* Verify that the registers are laid out as expected */
    468   1.7  dbj 			{
    469   1.7  dbj 				bus_addr_t next;
    470   1.7  dbj 				bus_addr_t limit;
    471   1.7  dbj 				bus_addr_t expected_limit;
    472   1.7  dbj 				expected_limit =
    473   1.7  dbj 						nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
    474   1.7  dbj 						nd->_nd_map->dm_segs[nd->_nd_idx].ds_len;
    475   1.7  dbj 
    476   1.7  dbj 				if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    477   1.7  dbj 					next  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
    478   1.7  dbj 					limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT) & ~0x80000000;
    479  1.15  dbj 				}
    480  1.15  dbj 				else {
    481   1.7  dbj 					next  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    482   1.7  dbj 					limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    483   1.7  dbj 				}
    484   1.7  dbj 
    485   1.7  dbj 				if ((next != limit) || (limit != expected_limit)) {
    486   1.7  dbj 					next_dma_print(nd);
    487   1.7  dbj 					printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    488   1.7  dbj 					panic("unexpected DMA limit at shutdown 0x%08x, 0x%08x, 0x%08x",
    489   1.7  dbj 							next,limit,expected_limit);
    490   1.7  dbj 				}
    491  1.12  dbj 			}
    492  1.12  dbj #endif
    493  1.12  dbj #endif
    494  1.12  dbj 
    495  1.13  dbj #if 1
    496  1.12  dbj #ifdef DIAGNOSTIC
    497  1.12  dbj 			if (state & (DMACSR_SUPDATE|DMACSR_ENABLE)) {
    498  1.12  dbj 				next_dma_print(nd);
    499  1.12  dbj 				panic("DMA: unexpected bits set in DMA state at shutdown (0x%b)\n",
    500  1.12  dbj 						state,DMACSR_BITS);
    501   1.7  dbj 			}
    502   1.7  dbj #endif
    503   1.7  dbj #endif
    504   1.7  dbj 
    505   1.7  dbj 			if ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs) {
    506   1.7  dbj 				if (nd->nd_completed_cb)
    507   1.7  dbj 					(*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
    508   1.7  dbj 			}
    509   1.7  dbj 			nd->_nd_map = 0;
    510   1.7  dbj 			nd->_nd_idx = 0;
    511   1.7  dbj 
    512   1.7  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    513   1.7  dbj 					DMACSR_CLRCOMPLETE | DMACSR_RESET);
    514   1.7  dbj 
    515   1.7  dbj 			DPRINTF(("DMA: a normal and expected shutdown occurred\n"));
    516   1.7  dbj 			if (nd->nd_shutdown_cb) (*nd->nd_shutdown_cb)(nd->nd_cb_arg);
    517   1.7  dbj 
    518   1.2  dbj 			return(1);
    519   1.7  dbj 		}
    520   1.7  dbj 
    521   1.7  dbj #if 0
    522   1.7  dbj #ifdef DIAGNOSTIC
    523   1.7  dbj 		if (!(state & DMACSR_SUPDATE)) {
    524   1.7  dbj 			next_dma_print(nd);
    525   1.7  dbj 			printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    526   1.7  dbj 			panic("SUPDATE not set with continuing DMA");
    527   1.7  dbj 		}
    528   1.2  dbj #endif
    529   1.1  dbj #endif
    530   1.1  dbj 
    531   1.7  dbj 		/* Check that the buffer we are interrupted for is the one we expect.
    532   1.7  dbj 		 * Shorten the buffer if the dma completed with a short buffer
    533   1.1  dbj 		 */
    534   1.7  dbj 		{
    535   1.1  dbj 			bus_addr_t next;
    536   1.1  dbj 			bus_addr_t limit;
    537   1.7  dbj 			bus_addr_t expected_next;
    538   1.7  dbj 			bus_addr_t expected_limit;
    539   1.7  dbj 
    540   1.7  dbj 			expected_next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
    541   1.7  dbj 			expected_limit = expected_next + nd->_nd_map->dm_segs[nd->_nd_idx].ds_len;
    542   1.1  dbj 
    543   1.7  dbj #if 0 /* for some unknown reason, somtimes DD_SAVED_NEXT has value from
    544   1.7  dbj 			 * nd->_nd_map and sometimes it has value from nd->_nd_map_cont.
    545   1.7  dbj 			 * Somtimes, it has a completely different unknown value.
    546   1.7  dbj 			 * Until that's understood, we won't sanity check the expected_next value.
    547   1.7  dbj 			 */
    548   1.7  dbj 			next  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    549   1.1  dbj #else
    550   1.7  dbj 			next  = expected_next;
    551   1.1  dbj #endif
    552   1.1  dbj 			limit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    553   1.1  dbj 
    554   1.1  dbj 			if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    555   1.1  dbj 				limit &= ~0x80000000;
    556   1.1  dbj 			}
    557  1.16  dbj 
    558   1.7  dbj 			if ((limit-next < 0) ||
    559   1.7  dbj 					(limit-next >= expected_limit-expected_next)) {
    560   1.7  dbj #ifdef DIAGNOSTIC
    561   1.7  dbj #if 0 /* Sometimes, (under load I think) even DD_SAVED_LIMIT has
    562   1.7  dbj 			 * a bogus value.  Until that's understood, we don't panic
    563   1.7  dbj 			 * here.
    564   1.7  dbj 			 */
    565   1.7  dbj 				next_dma_print(nd);
    566   1.7  dbj 				printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    567   1.7  dbj 				panic("Unexpected saved registers values.");
    568   1.7  dbj #endif
    569   1.7  dbj #endif
    570  1.16  dbj 
    571  1.16  dbj 				/* @@@ we pretend the entire buffer transferred ok.
    572  1.16  dbj 				 * we might consider throwing away this transfer instead
    573  1.16  dbj 				 */
    574  1.17  dbj 				nd->_nd_map->dm_segs[nd->_nd_idx].ds_xfer_len = expected_limit-expected_next;
    575   1.7  dbj 			} else {
    576  1.17  dbj 				nd->_nd_map->dm_segs[nd->_nd_idx].ds_xfer_len = limit-next;
    577  1.16  dbj 				expected_limit = expected_next + (limit-next);
    578   1.7  dbj 			}
    579   1.1  dbj 
    580   1.7  dbj #if 0 /* these checks are turned off until the above mentioned weirdness is fixed. */
    581   1.1  dbj #ifdef DIAGNOSTIC
    582   1.7  dbj 			if (next != expected_next) {
    583   1.1  dbj 				next_dma_print(nd);
    584   1.1  dbj 				printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    585   1.7  dbj 				panic("unexpected DMA next buffer in interrupt (found 0x%08x, expected 0x%08x)",
    586   1.7  dbj 						next,expected_next);
    587   1.1  dbj 			}
    588   1.7  dbj 			if (limit != expected_limit) {
    589   1.1  dbj 				next_dma_print(nd);
    590   1.1  dbj 				printf("DEBUG: state = 0x%b\n", state,DMACSR_BITS);
    591   1.7  dbj 				panic("unexpected DMA limit buffer in interrupt (found 0x%08x, expected 0x%08x)",
    592   1.7  dbj 						limit,expected_limit);
    593   1.7  dbj 			}
    594   1.7  dbj #endif
    595   1.1  dbj #endif
    596   1.1  dbj 		}
    597   1.1  dbj 
    598   1.7  dbj 		next_dma_rotate(nd);
    599   1.7  dbj 		next_dma_setup_cont_regs(nd);
    600   1.1  dbj 
    601   1.7  dbj 		if (!(state & DMACSR_ENABLE)) {
    602  1.10  dbj 
    603  1.10  dbj 			DPRINTF(("Unexpected DMA shutdown, restarting\n"));
    604   1.1  dbj 
    605   1.1  dbj 			if (nd->_nd_map_cont) {
    606   1.1  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    607   1.9  dbj 						DMACSR_SETSUPDATE | DMACSR_SETENABLE | nd->_nd_dmadir);
    608   1.1  dbj 			} else {
    609   1.7  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    610   1.9  dbj 						DMACSR_SETENABLE | nd->_nd_dmadir);
    611   1.1  dbj 			}
    612   1.1  dbj 
    613   1.1  dbj 		} else {
    614   1.1  dbj 
    615   1.1  dbj 			if (nd->_nd_map_cont) {
    616   1.1  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    617   1.9  dbj 						DMACSR_SETSUPDATE | DMACSR_CLRCOMPLETE | nd->_nd_dmadir);
    618   1.1  dbj 			} else {
    619   1.1  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    620   1.9  dbj 						DMACSR_CLRCOMPLETE | nd->_nd_dmadir);
    621   1.1  dbj 			}
    622   1.1  dbj 		}
    623   1.1  dbj 
    624   1.1  dbj 	}
    625   1.1  dbj 
    626   1.1  dbj   DPRINTF(("DMA exiting interrupt ipl (%ld) intr(0x%b)\n",
    627   1.1  dbj           NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
    628   1.1  dbj 
    629   1.1  dbj   return(1);
    630   1.1  dbj }
    631   1.1  dbj 
    632   1.1  dbj /*
    633   1.1  dbj  * Check to see if dma has finished for a channel */
    634   1.1  dbj int
    635   1.1  dbj nextdma_finished(nd)
    636   1.1  dbj 	struct nextdma_config *nd;
    637   1.1  dbj {
    638   1.1  dbj 	int r;
    639   1.1  dbj 	int s;
    640   1.1  dbj 	s = spldma();									/* @@@ should this be splimp()? */
    641   1.1  dbj 	r = (nd->_nd_map == NULL) && (nd->_nd_map_cont == NULL);
    642   1.1  dbj 	splx(s);
    643   1.1  dbj 	return(r);
    644   1.1  dbj }
    645   1.1  dbj 
    646   1.1  dbj void
    647   1.1  dbj nextdma_start(nd, dmadir)
    648   1.1  dbj 	struct nextdma_config *nd;
    649   1.1  dbj 	u_long dmadir;								/* 	DMACSR_READ or DMACSR_WRITE */
    650   1.1  dbj {
    651   1.1  dbj 
    652   1.1  dbj #ifdef DIAGNOSTIC
    653   1.1  dbj 	if (!nextdma_finished(nd)) {
    654   1.1  dbj 		panic("DMA trying to start before previous finished on intr(0x%b)\n",
    655   1.1  dbj 				NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS);
    656   1.1  dbj 	}
    657   1.1  dbj #endif
    658   1.1  dbj 
    659   1.1  dbj   DPRINTF(("DMA start (%ld) intr(0x%b)\n",
    660   1.1  dbj           NEXT_I_IPL(nd->nd_intr), NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
    661   1.1  dbj 
    662   1.1  dbj #ifdef DIAGNOSTIC
    663   1.1  dbj 	if (nd->_nd_map) {
    664   1.1  dbj 		next_dma_print(nd);
    665   1.1  dbj 		panic("DMA: nextdma_start() with non null map\n");
    666   1.1  dbj 	}
    667   1.1  dbj 	if (nd->_nd_map_cont) {
    668   1.1  dbj 		next_dma_print(nd);
    669   1.1  dbj 		panic("DMA: nextdma_start() with non null continue map\n");
    670   1.1  dbj 	}
    671   1.1  dbj #endif
    672   1.1  dbj 
    673   1.9  dbj #ifdef DIAGNOSTIC
    674   1.9  dbj 	if ((dmadir != DMACSR_READ) && (dmadir != DMACSR_WRITE)) {
    675   1.9  dbj 		panic("DMA: nextdma_start(), dmadir arg must be DMACSR_READ or DMACSR_WRITE\n");
    676   1.9  dbj 	}
    677   1.9  dbj #endif
    678   1.9  dbj 
    679   1.9  dbj 	nd->_nd_dmadir = dmadir;
    680   1.9  dbj 
    681   1.7  dbj 	/* preload both the current and the continue maps */
    682   1.1  dbj 	next_dma_rotate(nd);
    683   1.1  dbj 
    684   1.1  dbj #ifdef DIAGNOSTIC
    685   1.1  dbj 	if (!nd->_nd_map_cont) {
    686   1.1  dbj 		panic("No map available in nextdma_start()");
    687   1.1  dbj 	}
    688   1.1  dbj #endif
    689   1.1  dbj 
    690   1.7  dbj 	next_dma_rotate(nd);
    691   1.7  dbj 
    692   1.1  dbj 	DPRINTF(("DMA initiating DMA %s of %d segments on intr(0x%b)\n",
    693   1.9  dbj 			(nd->_nd_dmadir == DMACSR_READ ? "read" : "write"), nd->_nd_map->dm_nsegs,
    694   1.1  dbj 			NEXT_I_BIT(nd->nd_intr),NEXT_INTR_BITS));
    695   1.1  dbj 
    696   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    697   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    698   1.9  dbj 			DMACSR_INITBUF | DMACSR_RESET | nd->_nd_dmadir);
    699   1.1  dbj 
    700   1.7  dbj 	next_dma_setup_curr_regs(nd);
    701   1.1  dbj 	next_dma_setup_cont_regs(nd);
    702   1.1  dbj 
    703   1.4  dbj #if (defined(ND_DEBUG))
    704   1.8  dbj 	if (nextdma_debug) next_dma_print(nd);
    705   1.4  dbj #endif
    706   1.1  dbj 
    707   1.7  dbj 	if (nd->_nd_map_cont) {
    708   1.1  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    709   1.9  dbj 				DMACSR_SETSUPDATE | DMACSR_SETENABLE | nd->_nd_dmadir);
    710   1.1  dbj 	} else {
    711   1.1  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    712   1.9  dbj 				DMACSR_SETENABLE | nd->_nd_dmadir);
    713   1.1  dbj 	}
    714   1.1  dbj 
    715   1.1  dbj }
    716