Home | History | Annotate | Line # | Download | only in dev
nextdma.c revision 1.26
      1  1.26  dbj /*	$NetBSD: nextdma.c,v 1.26 2001/04/16 14:12:12 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.26  dbj #if defined(ND_DEBUG)
     68  1.26  dbj int nextdma_debug_enetr_idx = 0;
     69  1.26  dbj unsigned int nextdma_debug_enetr_state[100] = { 0 };
     70  1.26  dbj int nextdma_debug_scsi_idx = 0;
     71  1.26  dbj unsigned int nextdma_debug_scsi_state[100] = { 0 };
     72  1.26  dbj 
     73  1.26  dbj void nextdma_debug_initstate(struct nextdma_config *nd);
     74  1.26  dbj void nextdma_debug_savestate(struct nextdma_config *nd, unsigned int state);
     75  1.26  dbj void nextdma_debug_scsi_dumpstate(void);
     76  1.26  dbj void nextdma_debug_enetr_dumpstate(void);
     77  1.26  dbj 
     78  1.26  dbj void
     79  1.26  dbj nextdma_debug_initstate(struct nextdma_config *nd)
     80  1.26  dbj {
     81  1.26  dbj 	switch(nd->nd_intr) {
     82  1.26  dbj 	case NEXT_I_ENETR_DMA:
     83  1.26  dbj 		memset(nextdma_debug_enetr_state,0,sizeof(nextdma_debug_enetr_state));
     84  1.26  dbj 		break;
     85  1.26  dbj 	case NEXT_I_SCSI_DMA:
     86  1.26  dbj 		memset(nextdma_debug_scsi_state,0,sizeof(nextdma_debug_scsi_state));
     87  1.26  dbj 		break;
     88  1.26  dbj 	}
     89  1.26  dbj }
     90  1.26  dbj 
     91  1.26  dbj void
     92  1.26  dbj nextdma_debug_savestate(struct nextdma_config *nd, unsigned int state)
     93  1.26  dbj {
     94  1.26  dbj 	switch(nd->nd_intr) {
     95  1.26  dbj 	case NEXT_I_ENETR_DMA:
     96  1.26  dbj 		nextdma_debug_enetr_state[nextdma_debug_enetr_idx++] = state;
     97  1.26  dbj 		nextdma_debug_enetr_idx %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
     98  1.26  dbj 		break;
     99  1.26  dbj 	case NEXT_I_SCSI_DMA:
    100  1.26  dbj 		nextdma_debug_scsi_state[nextdma_debug_scsi_idx++] = state;
    101  1.26  dbj 		nextdma_debug_scsi_idx %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
    102  1.26  dbj 		break;
    103  1.26  dbj 	}
    104  1.26  dbj }
    105  1.26  dbj 
    106  1.26  dbj void
    107  1.26  dbj nextdma_debug_enetr_dumpstate(void)
    108  1.26  dbj {
    109  1.26  dbj 	int i;
    110  1.26  dbj 	int s;
    111  1.26  dbj 	s = spldma();
    112  1.26  dbj 	i = nextdma_debug_enetr_idx;
    113  1.26  dbj 	do {
    114  1.26  dbj 		char sbuf[256];
    115  1.26  dbj 		if (nextdma_debug_enetr_state[i]) {
    116  1.26  dbj 			bitmask_snprintf(nextdma_debug_enetr_state[i], DMACSR_BITS, sbuf, sizeof(sbuf));
    117  1.26  dbj 			printf("DMA: 0x%02x state 0x%s\n",i,sbuf);
    118  1.26  dbj 		}
    119  1.26  dbj 		i++;
    120  1.26  dbj 		i %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
    121  1.26  dbj 	} while (i != nextdma_debug_enetr_idx);
    122  1.26  dbj 	splx(s);
    123  1.26  dbj }
    124  1.26  dbj 
    125  1.26  dbj void
    126  1.26  dbj nextdma_debug_scsi_dumpstate(void)
    127  1.26  dbj {
    128  1.26  dbj 	int i;
    129  1.26  dbj 	int s;
    130  1.26  dbj 	s = spldma();
    131  1.26  dbj 	i = nextdma_debug_scsi_idx;
    132  1.26  dbj 	do {
    133  1.26  dbj 		char sbuf[256];
    134  1.26  dbj 		if (nextdma_debug_scsi_state[i]) {
    135  1.26  dbj 			bitmask_snprintf(nextdma_debug_scsi_state[i], DMACSR_BITS, sbuf, sizeof(sbuf));
    136  1.26  dbj 			printf("DMA: 0x%02x state 0x%s\n",i,sbuf);
    137  1.26  dbj 		}
    138  1.26  dbj 		i++;
    139  1.26  dbj 		i %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
    140  1.26  dbj 	} while (i != nextdma_debug_scsi_idx);
    141  1.26  dbj 	splx(s);
    142  1.26  dbj }
    143  1.26  dbj #endif
    144  1.26  dbj 
    145  1.26  dbj 
    146   1.1  dbj void next_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    147   1.1  dbj                        bus_size_t, int));
    148   1.1  dbj int next_dma_continue __P((struct nextdma_config *));
    149   1.1  dbj void next_dma_rotate __P((struct nextdma_config *));
    150   1.1  dbj 
    151   1.1  dbj void next_dma_setup_cont_regs __P((struct nextdma_config *));
    152   1.1  dbj void next_dma_setup_curr_regs __P((struct nextdma_config *));
    153   1.1  dbj 
    154   1.1  dbj void
    155   1.1  dbj nextdma_config(nd)
    156   1.1  dbj 	struct nextdma_config *nd;
    157   1.1  dbj {
    158   1.1  dbj 	/* Initialize the dma_tag. As a hack, we currently
    159   1.1  dbj 	 * put the dma tag in the structure itself.  It shouldn't be there.
    160   1.1  dbj 	 */
    161   1.1  dbj 
    162   1.1  dbj 	{
    163   1.1  dbj 		bus_dma_tag_t t;
    164   1.1  dbj 		t = &nd->_nd_dmat;
    165   1.1  dbj 		t->_cookie = nd;
    166   1.1  dbj 		t->_dmamap_create = _bus_dmamap_create;
    167   1.1  dbj 		t->_dmamap_destroy = _bus_dmamap_destroy;
    168   1.1  dbj 		t->_dmamap_load = _bus_dmamap_load_direct;
    169   1.1  dbj 		t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    170   1.1  dbj 		t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    171   1.1  dbj 		t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    172   1.1  dbj 		t->_dmamap_unload = _bus_dmamap_unload;
    173  1.16  dbj 		t->_dmamap_sync = _bus_dmamap_sync;
    174   1.1  dbj 
    175   1.1  dbj 		t->_dmamem_alloc = _bus_dmamem_alloc;
    176   1.1  dbj 		t->_dmamem_free = _bus_dmamem_free;
    177   1.1  dbj 		t->_dmamem_map = _bus_dmamem_map;
    178   1.1  dbj 		t->_dmamem_unmap = _bus_dmamem_unmap;
    179   1.1  dbj 		t->_dmamem_mmap = _bus_dmamem_mmap;
    180   1.1  dbj 
    181   1.1  dbj 		nd->nd_dmat = t;
    182   1.1  dbj 	}
    183   1.1  dbj 
    184   1.1  dbj 	nextdma_init(nd);
    185   1.1  dbj 
    186  1.14  dbj 	isrlink_autovec(nextdma_intr, nd, NEXT_I_IPL(nd->nd_intr), 10);
    187  1.14  dbj 	INTR_ENABLE(nd->nd_intr);
    188   1.1  dbj }
    189   1.1  dbj 
    190   1.1  dbj void
    191   1.1  dbj nextdma_init(nd)
    192   1.1  dbj 	struct nextdma_config *nd;
    193   1.1  dbj {
    194  1.22   tv #ifdef ND_DEBUG
    195  1.22   tv 	if (nextdma_debug) {
    196  1.22   tv 		char sbuf[256];
    197  1.22   tv 
    198  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    199  1.22   tv 				 sbuf, sizeof(sbuf));
    200  1.22   tv 		printf("DMA init ipl (%ld) intr(0x%s)\n",
    201  1.22   tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    202  1.22   tv 	}
    203  1.22   tv #endif
    204   1.1  dbj 
    205   1.1  dbj 	nd->_nd_map = NULL;
    206   1.1  dbj 	nd->_nd_idx = 0;
    207   1.1  dbj 	nd->_nd_map_cont = NULL;
    208   1.1  dbj 	nd->_nd_idx_cont = 0;
    209   1.1  dbj 
    210   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    211   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    212  1.20  dbj 			DMACSR_RESET | DMACSR_INITBUF);
    213   1.1  dbj 
    214   1.1  dbj 	next_dma_setup_curr_regs(nd);
    215   1.1  dbj 	next_dma_setup_cont_regs(nd);
    216   1.1  dbj 
    217  1.20  dbj #if defined(DIAGNOSTIC)
    218   1.1  dbj 	{
    219   1.1  dbj 		u_long state;
    220   1.1  dbj 		state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    221  1.20  dbj 
    222  1.20  dbj #if 1
    223  1.20  dbj 	/* mourning (a 25Mhz 68040 mono slab) appears to set BUSEXC
    224  1.20  dbj 	 * milo (a 25Mhz 68040 mono cube) didn't have this problem
    225  1.20  dbj 	 * Darrin B. Jewell <jewell (at) mit.edu>  Mon May 25 07:53:05 1998
    226  1.20  dbj 	 */
    227  1.20  dbj     state &= (DMACSR_COMPLETE | DMACSR_SUPDATE | DMACSR_ENABLE);
    228  1.20  dbj #else
    229   1.1  dbj     state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
    230   1.1  dbj               DMACSR_SUPDATE | DMACSR_ENABLE);
    231  1.20  dbj #endif
    232   1.1  dbj 		if (state) {
    233   1.1  dbj 			next_dma_print(nd);
    234  1.20  dbj 			panic("DMA did not reset");
    235   1.1  dbj 		}
    236   1.1  dbj 	}
    237   1.1  dbj #endif
    238   1.1  dbj }
    239   1.1  dbj 
    240   1.4  dbj 
    241   1.1  dbj void
    242   1.1  dbj nextdma_reset(nd)
    243   1.1  dbj 	struct nextdma_config *nd;
    244   1.1  dbj {
    245   1.1  dbj 	int s;
    246  1.18  dbj 	s = spldma();
    247   1.8  dbj 
    248   1.8  dbj 	DPRINTF(("DMA reset\n"));
    249   1.8  dbj 
    250   1.8  dbj #if (defined(ND_DEBUG))
    251   1.8  dbj 	if (nextdma_debug) next_dma_print(nd);
    252   1.8  dbj #endif
    253   1.8  dbj 
    254  1.26  dbj 	if ((nd->_nd_map) || (nd->_nd_map_cont)) {
    255  1.26  dbj 		/* @@@ clean up dma maps */
    256  1.26  dbj 		panic("DMA abort not implemented\n");
    257  1.26  dbj 	}
    258  1.20  dbj 
    259   1.1  dbj 	nextdma_init(nd);
    260   1.1  dbj 	splx(s);
    261   1.1  dbj }
    262   1.1  dbj 
    263   1.1  dbj /****************************************************************/
    264   1.1  dbj 
    265   1.1  dbj 
    266   1.1  dbj /* Call the completed and continue callbacks to try to fill
    267   1.1  dbj  * in the dma continue buffers.
    268   1.1  dbj  */
    269   1.1  dbj void
    270   1.1  dbj next_dma_rotate(nd)
    271   1.1  dbj 	struct nextdma_config *nd;
    272   1.1  dbj {
    273   1.1  dbj 
    274   1.1  dbj 	DPRINTF(("DMA next_dma_rotate()\n"));
    275   1.1  dbj 
    276   1.1  dbj 	/* Rotate the continue map into the current map */
    277   1.1  dbj 	nd->_nd_map = nd->_nd_map_cont;
    278   1.1  dbj 	nd->_nd_idx = nd->_nd_idx_cont;
    279   1.1  dbj 
    280   1.1  dbj 	if ((!nd->_nd_map_cont) ||
    281   1.1  dbj 			((nd->_nd_map_cont) &&
    282   1.1  dbj 					(++nd->_nd_idx_cont >= nd->_nd_map_cont->dm_nsegs))) {
    283   1.1  dbj 		if (nd->nd_continue_cb) {
    284   1.1  dbj 			nd->_nd_map_cont = (*nd->nd_continue_cb)(nd->nd_cb_arg);
    285  1.26  dbj 			if (nd->_nd_map_cont) {
    286  1.26  dbj 				nd->_nd_map_cont->dm_xfer_len = 0;
    287  1.26  dbj 			}
    288   1.1  dbj 		} else {
    289   1.1  dbj 			nd->_nd_map_cont = 0;
    290   1.1  dbj 		}
    291   1.1  dbj 		nd->_nd_idx_cont = 0;
    292   1.1  dbj 	}
    293   1.7  dbj 
    294   1.7  dbj #ifdef DIAGNOSTIC
    295  1.24  dbj 	if (nd->_nd_map_cont) {
    296  1.12  dbj 		if (!DMA_BEGINALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr)) {
    297  1.12  dbj 			next_dma_print(nd);
    298   1.7  dbj 			panic("DMA request unaligned at start\n");
    299   1.7  dbj 		}
    300  1.12  dbj 		if (!DMA_ENDALIGNED(nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
    301  1.12  dbj 				nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len)) {
    302  1.12  dbj 			next_dma_print(nd);
    303   1.7  dbj 			panic("DMA request unaligned at end\n");
    304   1.7  dbj 		}
    305   1.7  dbj 	}
    306   1.7  dbj #endif
    307   1.7  dbj 
    308   1.1  dbj }
    309   1.1  dbj 
    310   1.1  dbj void
    311   1.1  dbj next_dma_setup_cont_regs(nd)
    312   1.1  dbj 	struct nextdma_config *nd;
    313   1.1  dbj {
    314  1.20  dbj 	bus_addr_t dd_start;
    315  1.20  dbj 	bus_addr_t dd_stop;
    316  1.20  dbj 	bus_addr_t dd_saved_start;
    317  1.20  dbj 	bus_addr_t dd_saved_stop;
    318  1.20  dbj 
    319   1.1  dbj 	DPRINTF(("DMA next_dma_setup_regs()\n"));
    320   1.1  dbj 
    321   1.1  dbj 	if (nd->_nd_map_cont) {
    322  1.20  dbj 		dd_start = nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr;
    323  1.20  dbj 		dd_stop  = (nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr +
    324  1.20  dbj 				nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
    325   1.1  dbj 
    326   1.1  dbj 		if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    327  1.20  dbj 			dd_stop |= 0x80000000;		/* Ethernet transmit needs secret magic */
    328  1.20  dbj 		}
    329  1.20  dbj 	} else {
    330  1.20  dbj 		dd_start = 0xdeadbeef;
    331  1.20  dbj 		dd_stop = 0xdeadbeef;
    332  1.20  dbj 	}
    333   1.1  dbj 
    334  1.20  dbj 	dd_saved_start = dd_start;
    335  1.20  dbj 	dd_saved_stop  = dd_stop;
    336  1.15  dbj 
    337  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START, dd_start);
    338  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP, dd_stop);
    339  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START, dd_saved_start);
    340  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP, dd_saved_stop);
    341   1.1  dbj 
    342  1.20  dbj #ifdef DIAGNOSTIC
    343  1.24  dbj 	if (   (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START) != dd_start)
    344  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP) != dd_stop)
    345  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START) != dd_saved_start)
    346  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP) != dd_saved_stop)
    347  1.24  dbj 			) {
    348  1.20  dbj 		next_dma_print(nd);
    349  1.20  dbj 		panic("DMA failure writing to continue regs");
    350   1.1  dbj 	}
    351   1.7  dbj #endif
    352   1.1  dbj }
    353   1.1  dbj 
    354   1.1  dbj void
    355   1.1  dbj next_dma_setup_curr_regs(nd)
    356   1.1  dbj 	struct nextdma_config *nd;
    357   1.1  dbj {
    358  1.20  dbj 	bus_addr_t dd_next;
    359  1.20  dbj 	bus_addr_t dd_limit;
    360  1.20  dbj 	bus_addr_t dd_saved_next;
    361  1.20  dbj 	bus_addr_t dd_saved_limit;
    362  1.20  dbj 
    363   1.1  dbj 	DPRINTF(("DMA next_dma_setup_curr_regs()\n"));
    364   1.1  dbj 
    365  1.15  dbj 
    366  1.15  dbj 	if (nd->_nd_map) {
    367  1.20  dbj 		dd_next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
    368  1.20  dbj 		dd_limit = (nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
    369  1.20  dbj 				nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    370  1.24  dbj 
    371  1.15  dbj 		if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    372  1.20  dbj 			dd_limit |= 0x80000000; /* Ethernet transmit needs secret magic */
    373  1.20  dbj 		}
    374  1.20  dbj 	} else {
    375  1.20  dbj 		dd_next = 0xdeadbeef;
    376  1.20  dbj 		dd_limit = 0xdeadbeef;
    377  1.20  dbj 	}
    378   1.1  dbj 
    379  1.20  dbj 	dd_saved_next = dd_next;
    380  1.20  dbj 	dd_saved_limit = dd_limit;
    381   1.1  dbj 
    382  1.20  dbj 	if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    383  1.20  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF, dd_next);
    384  1.15  dbj 	} else {
    385  1.20  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT, dd_next);
    386  1.15  dbj 	}
    387  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, dd_limit);
    388  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT, dd_saved_next);
    389  1.20  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT, dd_saved_limit);
    390   1.1  dbj 
    391  1.20  dbj #ifdef DIAGNOSTIC
    392  1.24  dbj 	if (   (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF) != dd_next)
    393  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT) != dd_next)
    394  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT) != dd_limit)
    395  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT) != dd_saved_next)
    396  1.24  dbj 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT) != dd_saved_limit)
    397  1.24  dbj 			) {
    398  1.20  dbj 		next_dma_print(nd);
    399  1.20  dbj 		panic("DMA failure writing to current regs");
    400  1.20  dbj 	}
    401   1.7  dbj #endif
    402   1.1  dbj }
    403   1.1  dbj 
    404   1.1  dbj 
    405   1.1  dbj /* This routine is used for debugging */
    406   1.1  dbj 
    407   1.1  dbj void
    408   1.1  dbj next_dma_print(nd)
    409   1.1  dbj 	struct nextdma_config *nd;
    410   1.1  dbj {
    411   1.1  dbj 	u_long dd_csr;
    412   1.1  dbj 	u_long dd_next;
    413   1.1  dbj 	u_long dd_next_initbuf;
    414   1.1  dbj 	u_long dd_limit;
    415   1.1  dbj 	u_long dd_start;
    416   1.1  dbj 	u_long dd_stop;
    417   1.1  dbj 	u_long dd_saved_next;
    418   1.1  dbj 	u_long dd_saved_limit;
    419   1.1  dbj 	u_long dd_saved_start;
    420   1.1  dbj 	u_long dd_saved_stop;
    421  1.22   tv 	char sbuf[256];
    422   1.1  dbj 
    423  1.22   tv 	/* Read all of the registers before we print anything out,
    424   1.1  dbj 	 * in case something changes
    425   1.1  dbj 	 */
    426   1.1  dbj 	dd_csr          = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    427   1.1  dbj 	dd_next         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    428   1.1  dbj 	dd_next_initbuf = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
    429   1.1  dbj 	dd_limit        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    430   1.1  dbj 	dd_start        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START);
    431   1.1  dbj 	dd_stop         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP);
    432   1.1  dbj 	dd_saved_next   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    433   1.1  dbj 	dd_saved_limit  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    434   1.1  dbj 	dd_saved_start  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START);
    435   1.1  dbj 	dd_saved_stop   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP);
    436   1.1  dbj 
    437  1.22   tv 	bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    438  1.22   tv 			 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    439  1.22   tv 	printf("NDMAP: *intrstat = 0x%s\n", sbuf);
    440  1.22   tv 
    441  1.22   tv 	bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
    442  1.22   tv 			 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    443  1.22   tv 	printf("NDMAP: *intrmask = 0x%s\n", sbuf);
    444  1.20  dbj 
    445  1.12  dbj 	/* NDMAP is Next DMA Print (really!) */
    446  1.12  dbj 
    447   1.1  dbj 	if (nd->_nd_map) {
    448  1.11  dbj 		printf("NDMAP: nd->_nd_map->dm_mapsize = %d\n",
    449  1.11  dbj 				nd->_nd_map->dm_mapsize);
    450  1.11  dbj 		printf("NDMAP: nd->_nd_map->dm_nsegs = %d\n",
    451  1.11  dbj 				nd->_nd_map->dm_nsegs);
    452  1.26  dbj 		printf("NDMAP: nd->_nd_map->dm_xfer_len = %d\n",
    453  1.26  dbj 				nd->_nd_map->dm_xfer_len);
    454   1.1  dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    455   1.1  dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
    456   1.1  dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_len = %d\n",
    457   1.1  dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    458  1.24  dbj 		{
    459  1.24  dbj 			int i;
    460  1.24  dbj 			printf("NDMAP: Entire map;\n");
    461  1.24  dbj 			for(i=0;i<nd->_nd_map->dm_nsegs;i++) {
    462  1.24  dbj 				printf("NDMAP:   nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    463  1.24  dbj 						i,nd->_nd_map->dm_segs[i].ds_addr);
    464  1.24  dbj 				printf("NDMAP:   nd->_nd_map->dm_segs[%d].ds_len = %d\n",
    465  1.24  dbj 						i,nd->_nd_map->dm_segs[i].ds_len);
    466  1.24  dbj 			}
    467  1.24  dbj 		}
    468   1.1  dbj 	} else {
    469   1.1  dbj 		printf("NDMAP: nd->_nd_map = NULL\n");
    470   1.1  dbj 	}
    471   1.1  dbj 	if (nd->_nd_map_cont) {
    472  1.11  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_mapsize = %d\n",
    473  1.11  dbj 				nd->_nd_map_cont->dm_mapsize);
    474  1.11  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_nsegs = %d\n",
    475  1.11  dbj 				nd->_nd_map_cont->dm_nsegs);
    476  1.26  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_xfer_len = %d\n",
    477  1.26  dbj 				nd->_nd_map_cont->dm_xfer_len);
    478   1.1  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    479   1.1  dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
    480   1.2  dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_len = %d\n",
    481   1.1  dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
    482  1.24  dbj 		if (nd->_nd_map_cont != nd->_nd_map) {
    483  1.24  dbj 			int i;
    484  1.24  dbj 			printf("NDMAP: Entire map;\n");
    485  1.24  dbj 			for(i=0;i<nd->_nd_map_cont->dm_nsegs;i++) {
    486  1.24  dbj 				printf("NDMAP:   nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    487  1.24  dbj 						i,nd->_nd_map_cont->dm_segs[i].ds_addr);
    488  1.24  dbj 				printf("NDMAP:   nd->_nd_map_cont->dm_segs[%d].ds_len = %d\n",
    489  1.24  dbj 						i,nd->_nd_map_cont->dm_segs[i].ds_len);
    490  1.24  dbj 			}
    491  1.24  dbj 		}
    492   1.1  dbj 	} else {
    493   1.1  dbj 		printf("NDMAP: nd->_nd_map_cont = NULL\n");
    494   1.1  dbj 	}
    495   1.1  dbj 
    496  1.22   tv 	bitmask_snprintf(dd_csr, DMACSR_BITS, sbuf, sizeof(sbuf));
    497  1.22   tv 	printf("NDMAP: dd->dd_csr          = 0x%s\n",   sbuf);
    498  1.22   tv 
    499   1.1  dbj 	printf("NDMAP: dd->dd_saved_next   = 0x%08x\n", dd_saved_next);
    500   1.1  dbj 	printf("NDMAP: dd->dd_saved_limit  = 0x%08x\n", dd_saved_limit);
    501   1.1  dbj 	printf("NDMAP: dd->dd_saved_start  = 0x%08x\n", dd_saved_start);
    502   1.1  dbj 	printf("NDMAP: dd->dd_saved_stop   = 0x%08x\n", dd_saved_stop);
    503   1.1  dbj 	printf("NDMAP: dd->dd_next         = 0x%08x\n", dd_next);
    504   1.1  dbj 	printf("NDMAP: dd->dd_next_initbuf = 0x%08x\n", dd_next_initbuf);
    505   1.1  dbj 	printf("NDMAP: dd->dd_limit        = 0x%08x\n", dd_limit);
    506   1.1  dbj 	printf("NDMAP: dd->dd_start        = 0x%08x\n", dd_start);
    507   1.1  dbj 	printf("NDMAP: dd->dd_stop         = 0x%08x\n", dd_stop);
    508   1.1  dbj 
    509  1.22   tv 	bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    510  1.22   tv 			 sbuf, sizeof(sbuf));
    511  1.22   tv 	printf("NDMAP: interrupt ipl (%ld) intr(0x%s)\n",
    512  1.22   tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    513   1.1  dbj }
    514   1.1  dbj 
    515   1.1  dbj /****************************************************************/
    516   1.1  dbj 
    517   1.1  dbj int
    518   1.1  dbj nextdma_intr(arg)
    519   1.1  dbj      void *arg;
    520   1.1  dbj {
    521   1.1  dbj   /* @@@ This is bogus, we can't be certain of arg's type
    522  1.18  dbj 	 * unless the interrupt is for us.  For now we successfully
    523  1.18  dbj 	 * cheat because DMA interrupts are the only things invoked
    524  1.18  dbj 	 * at this interrupt level.
    525   1.1  dbj 	 */
    526  1.18  dbj   struct nextdma_config *nd = arg;
    527   1.1  dbj 
    528   1.1  dbj   if (!INTR_OCCURRED(nd->nd_intr)) return 0;
    529   1.1  dbj   /* Handle dma interrupts */
    530   1.1  dbj 
    531  1.22   tv #ifdef ND_DEBUG
    532  1.22   tv 	if (nextdma_debug) {
    533  1.22   tv 		char sbuf[256];
    534  1.22   tv 
    535  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    536  1.22   tv 				 sbuf, sizeof(sbuf));
    537  1.22   tv 		printf("DMA interrupt ipl (%ld) intr(0x%s)\n",
    538  1.22   tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    539  1.22   tv 	}
    540  1.22   tv #endif
    541   1.1  dbj 
    542   1.7  dbj #ifdef DIAGNOSTIC
    543   1.7  dbj 	if (!nd->_nd_map) {
    544   1.7  dbj 		next_dma_print(nd);
    545   1.7  dbj 		panic("DMA missing current map in interrupt!\n");
    546   1.7  dbj 	}
    547   1.7  dbj #endif
    548   1.7  dbj 
    549   1.1  dbj   {
    550  1.24  dbj     unsigned int state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    551  1.26  dbj 
    552  1.26  dbj #if defined(ND_DEBUG)
    553  1.26  dbj 		nextdma_debug_savestate(nd,state);
    554  1.26  dbj #endif
    555  1.26  dbj 
    556  1.26  dbj #ifdef DIAGNOSTIC
    557  1.26  dbj 		if (!(state & DMACSR_COMPLETE)) {
    558  1.26  dbj 			char sbuf[256];
    559  1.26  dbj 			next_dma_print(nd);
    560  1.26  dbj 			bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    561  1.26  dbj 			printf("DMA: state 0x%s\n",sbuf);
    562  1.26  dbj 			panic("DMA complete not set in interrupt\n");
    563  1.26  dbj 		}
    564  1.26  dbj #endif
    565  1.26  dbj 
    566  1.26  dbj 		{
    567  1.23  dbj 			bus_addr_t onext;
    568  1.23  dbj 			bus_addr_t olimit;
    569  1.23  dbj 			bus_addr_t slimit;
    570   1.1  dbj 
    571  1.23  dbj 			DPRINTF(("DMA: finishing xfer\n"));
    572  1.23  dbj 
    573  1.23  dbj 			onext = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
    574  1.23  dbj 			olimit = onext + nd->_nd_map->dm_segs[nd->_nd_idx].ds_len;
    575  1.23  dbj 
    576  1.24  dbj 			{
    577  1.24  dbj 				int result = 0;
    578  1.24  dbj 				if (state & DMACSR_ENABLE) {
    579  1.24  dbj 					/* enable bit was set */
    580  1.24  dbj 					result |= 0x01;
    581  1.24  dbj 				}
    582  1.23  dbj 				if (state & DMACSR_SUPDATE) {
    583  1.24  dbj 					/* supdate bit was set */
    584  1.24  dbj 					result |= 0x02;
    585  1.24  dbj 				}
    586  1.26  dbj 				if (nd->_nd_map_cont == NULL) {
    587  1.24  dbj 					/* Expecting a shutdown, didn't SETSUPDATE last turn */
    588  1.24  dbj 					result |= 0x04;
    589  1.24  dbj 				}
    590  1.24  dbj 				if (state & DMACSR_BUSEXC) {
    591  1.24  dbj 					/* bus exception bit was set */
    592  1.24  dbj 					result |= 0x08;
    593  1.24  dbj 				}
    594  1.24  dbj 				switch (result) {
    595  1.24  dbj 				case 0x00: /* !BUSEXC && !expecting && !SUPDATE && !ENABLE */
    596  1.24  dbj 				case 0x08: /* BUSEXC && !expecting && !SUPDATE && !ENABLE */
    597  1.24  dbj 					slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    598  1.24  dbj 					break;
    599  1.24  dbj 				case 0x01: /* !BUSEXC && !expecting && !SUPDATE && ENABLE */
    600  1.24  dbj 				case 0x09: /* BUSEXC && !expecting && !SUPDATE && ENABLE */
    601  1.26  dbj 					if (nd->nd_intr == NEXT_I_SCSI_DMA) {
    602  1.26  dbj 						bus_addr_t snext;
    603  1.26  dbj 						snext = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    604  1.26  dbj 						if (snext != onext) {
    605  1.26  dbj 							slimit = olimit;
    606  1.26  dbj 						} else {
    607  1.26  dbj 							slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    608  1.26  dbj 						}
    609  1.26  dbj 					} else {
    610  1.26  dbj 						slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    611  1.24  dbj 					}
    612  1.24  dbj 					break;
    613  1.24  dbj 				case 0x02: /* !BUSEXC && !expecting && SUPDATE && !ENABLE */
    614  1.24  dbj 				case 0x0a: /* BUSEXC && !expecting && SUPDATE && !ENABLE */
    615  1.24  dbj 					slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    616  1.24  dbj 					break;
    617  1.24  dbj 				case 0x04:  /* !BUSEXC && expecting && !SUPDATE && !ENABLE */
    618  1.24  dbj 				case 0x0c: /* BUSEXC && expecting && !SUPDATE && !ENABLE */
    619  1.24  dbj 					slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    620  1.24  dbj 					break;
    621  1.24  dbj 				default:
    622   1.7  dbj #ifdef DIAGNOSTIC
    623  1.26  dbj 					{
    624  1.26  dbj 						char sbuf[256];
    625  1.26  dbj 						printf("DMA: please send this output to port-next68k-maintainer (at) netbsd.org:\n");
    626  1.26  dbj 						bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    627  1.26  dbj 						printf("DMA: state 0x%s\n",sbuf);
    628  1.26  dbj 						next_dma_print(nd);
    629  1.26  dbj 						panic("DMA: condition 0x%02x not yet documented to occur\n",result);
    630  1.26  dbj 					}
    631  1.23  dbj #endif
    632  1.24  dbj 					slimit = olimit;
    633  1.24  dbj 					break;
    634  1.23  dbj 				}
    635  1.23  dbj 			}
    636  1.22   tv 
    637  1.23  dbj 			if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    638  1.23  dbj 				slimit &= ~0x80000000;
    639  1.23  dbj 			}
    640  1.22   tv 
    641  1.23  dbj #ifdef DIAGNOSTIC
    642  1.23  dbj 			if ((slimit < onext) || (slimit > olimit)) {
    643  1.26  dbj 				char sbuf[256];
    644  1.26  dbj 				bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    645  1.26  dbj 				printf("DMA: state 0x%s\n",sbuf);
    646  1.23  dbj 				next_dma_print(nd);
    647  1.24  dbj 				panic("DMA: Unexpected limit register (0x%08x) in finish_xfer\n",slimit);
    648  1.23  dbj 			}
    649   1.1  dbj #endif
    650   1.1  dbj 
    651  1.26  dbj #ifdef DIAGNOSTIC
    652  1.26  dbj 			if ((state & DMACSR_ENABLE) && ((nd->_nd_idx+1) != nd->_nd_map->dm_nsegs)) {
    653  1.26  dbj 				if (slimit != olimit) {
    654  1.26  dbj 					char sbuf[256];
    655  1.26  dbj 					bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    656  1.26  dbj 					printf("DMA: state 0x%s\n",sbuf);
    657  1.26  dbj 					next_dma_print(nd);
    658  1.26  dbj 					panic("DMA: short limit register (0x%08x) w/o finishing map.\n",slimit);
    659  1.26  dbj 				}
    660  1.26  dbj 			}
    661  1.26  dbj #endif
    662  1.26  dbj 
    663  1.23  dbj #if (defined(ND_DEBUG))
    664  1.23  dbj 			if (nextdma_debug > 2) next_dma_print(nd);
    665  1.23  dbj #endif
    666   1.7  dbj 
    667  1.26  dbj 			nd->_nd_map->dm_xfer_len += slimit-onext;
    668  1.12  dbj 
    669  1.23  dbj 			/* If we've reached the end of the current map, then inform
    670  1.23  dbj 			 * that we've completed that map.
    671  1.23  dbj 			 */
    672  1.26  dbj 			if ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs) {
    673  1.23  dbj 				if (nd->nd_completed_cb)
    674  1.23  dbj 					(*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
    675  1.23  dbj 			}
    676  1.23  dbj 			nd->_nd_map = 0;
    677  1.23  dbj 			nd->_nd_idx = 0;
    678  1.23  dbj 		}
    679  1.23  dbj 
    680  1.23  dbj 		if (state & DMACSR_ENABLE) {
    681  1.22   tv 
    682  1.23  dbj 			next_dma_rotate(nd);
    683  1.23  dbj 			next_dma_setup_cont_regs(nd);
    684  1.22   tv 
    685  1.23  dbj 			{
    686  1.23  dbj 				u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    687  1.23  dbj 
    688  1.23  dbj 				if (state & DMACSR_READ) {
    689  1.23  dbj 					dmadir = DMACSR_SETREAD;
    690  1.23  dbj 				} else {
    691  1.23  dbj 					dmadir = DMACSR_SETWRITE;
    692  1.23  dbj 				}
    693  1.23  dbj 
    694  1.26  dbj 				if (nd->_nd_map_cont == NULL) {
    695  1.23  dbj 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    696  1.23  dbj 							DMACSR_CLRCOMPLETE | dmadir);
    697  1.23  dbj 				} else {
    698  1.23  dbj 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    699  1.23  dbj 							DMACSR_CLRCOMPLETE | dmadir | DMACSR_SETSUPDATE);
    700  1.23  dbj 				}
    701   1.7  dbj 			}
    702   1.7  dbj 
    703  1.23  dbj 		} else {
    704   1.7  dbj 
    705  1.25  dbj 			DPRINTF(("DMA: a shutdown occurred\n"));
    706  1.25  dbj 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
    707  1.25  dbj 
    708  1.23  dbj 			/* Cleanup more incomplete transfers */
    709  1.26  dbj #if 1
    710  1.26  dbj 			/* cleanup continue map */
    711  1.23  dbj 			if (nd->_nd_map_cont) {
    712  1.23  dbj 				DPRINTF(("DMA: shutting down with non null continue map\n"));
    713  1.23  dbj 				if (nd->nd_completed_cb)
    714  1.23  dbj 					(*nd->nd_completed_cb)(nd->_nd_map_cont, nd->nd_cb_arg);
    715  1.23  dbj 
    716  1.23  dbj 				nd->_nd_map_cont = 0;
    717  1.23  dbj 				nd->_nd_idx_cont = 0;
    718  1.20  dbj 			}
    719  1.25  dbj #else
    720  1.26  dbj 			/* Do an automatic dma restart */
    721  1.26  dbj 			if (nd->_nd_map_cont) {
    722  1.26  dbj 				u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    723  1.26  dbj 
    724  1.25  dbj 				next_dma_rotate(nd);
    725  1.25  dbj 
    726  1.25  dbj 				if (state & DMACSR_READ) {
    727  1.25  dbj 					dmadir = DMACSR_SETREAD;
    728  1.25  dbj 				} else {
    729  1.25  dbj 					dmadir = DMACSR_SETWRITE;
    730  1.25  dbj 				}
    731  1.25  dbj 
    732  1.25  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    733  1.25  dbj 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    734  1.25  dbj 						DMACSR_INITBUF | DMACSR_RESET | dmadir);
    735  1.25  dbj 
    736  1.25  dbj 				next_dma_setup_curr_regs(nd);
    737  1.25  dbj 				next_dma_setup_cont_regs(nd);
    738  1.25  dbj 
    739  1.26  dbj 				if (nd->_nd_map_cont == NULL) {
    740  1.25  dbj 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    741  1.25  dbj 							DMACSR_SETENABLE | dmadir);
    742  1.25  dbj 				} else {
    743  1.25  dbj 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    744  1.25  dbj 							DMACSR_SETSUPDATE | DMACSR_SETENABLE | dmadir);
    745  1.25  dbj 				}
    746  1.25  dbj 				return 1;
    747  1.25  dbj 			}
    748  1.25  dbj #endif
    749  1.23  dbj 			if (nd->nd_shutdown_cb) (*nd->nd_shutdown_cb)(nd->nd_cb_arg);
    750   1.1  dbj 		}
    751   1.1  dbj 	}
    752   1.1  dbj 
    753  1.22   tv #ifdef ND_DEBUG
    754  1.22   tv 	if (nextdma_debug) {
    755  1.22   tv 		char sbuf[256];
    756  1.22   tv 
    757  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    758  1.22   tv 				 sbuf, sizeof(sbuf));
    759  1.22   tv 		printf("DMA exiting interrupt ipl (%ld) intr(0x%s)\n",
    760  1.22   tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    761  1.22   tv 	}
    762  1.22   tv #endif
    763   1.1  dbj 
    764   1.1  dbj   return(1);
    765   1.1  dbj }
    766   1.1  dbj 
    767   1.1  dbj /*
    768   1.1  dbj  * Check to see if dma has finished for a channel */
    769   1.1  dbj int
    770   1.1  dbj nextdma_finished(nd)
    771   1.1  dbj 	struct nextdma_config *nd;
    772   1.1  dbj {
    773   1.1  dbj 	int r;
    774   1.1  dbj 	int s;
    775   1.1  dbj 	s = spldma();									/* @@@ should this be splimp()? */
    776   1.1  dbj 	r = (nd->_nd_map == NULL) && (nd->_nd_map_cont == NULL);
    777   1.1  dbj 	splx(s);
    778   1.1  dbj 	return(r);
    779   1.1  dbj }
    780   1.1  dbj 
    781   1.1  dbj void
    782   1.1  dbj nextdma_start(nd, dmadir)
    783   1.1  dbj 	struct nextdma_config *nd;
    784  1.19  dbj 	u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    785   1.1  dbj {
    786   1.1  dbj 
    787   1.1  dbj #ifdef DIAGNOSTIC
    788   1.1  dbj 	if (!nextdma_finished(nd)) {
    789  1.22   tv 		char sbuf[256];
    790  1.22   tv 
    791  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    792  1.22   tv 				 sbuf, sizeof(sbuf));
    793  1.22   tv 		panic("DMA trying to start before previous finished on intr(0x%s)\n", sbuf);
    794   1.1  dbj 	}
    795   1.1  dbj #endif
    796   1.1  dbj 
    797  1.22   tv #ifdef ND_DEBUG
    798  1.22   tv 	if (nextdma_debug) {
    799  1.22   tv 		char sbuf[256];
    800  1.22   tv 
    801  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    802  1.22   tv 				 sbuf, sizeof(sbuf));
    803  1.22   tv 		printf("DMA start (%ld) intr(0x%s)\n",
    804  1.22   tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    805  1.22   tv 	}
    806  1.22   tv #endif
    807   1.1  dbj 
    808   1.1  dbj #ifdef DIAGNOSTIC
    809   1.1  dbj 	if (nd->_nd_map) {
    810   1.1  dbj 		next_dma_print(nd);
    811   1.1  dbj 		panic("DMA: nextdma_start() with non null map\n");
    812   1.1  dbj 	}
    813   1.1  dbj 	if (nd->_nd_map_cont) {
    814   1.1  dbj 		next_dma_print(nd);
    815   1.1  dbj 		panic("DMA: nextdma_start() with non null continue map\n");
    816   1.1  dbj 	}
    817   1.1  dbj #endif
    818   1.1  dbj 
    819   1.9  dbj #ifdef DIAGNOSTIC
    820  1.19  dbj 	if ((dmadir != DMACSR_SETREAD) && (dmadir != DMACSR_SETWRITE)) {
    821  1.19  dbj 		panic("DMA: nextdma_start(), dmadir arg must be DMACSR_SETREAD or DMACSR_SETWRITE\n");
    822   1.9  dbj 	}
    823   1.9  dbj #endif
    824   1.9  dbj 
    825  1.26  dbj #if defined(ND_DEBUG)
    826  1.26  dbj 	nextdma_debug_initstate(nd);
    827  1.26  dbj #endif
    828  1.26  dbj 
    829   1.7  dbj 	/* preload both the current and the continue maps */
    830   1.1  dbj 	next_dma_rotate(nd);
    831   1.1  dbj 
    832   1.1  dbj #ifdef DIAGNOSTIC
    833   1.1  dbj 	if (!nd->_nd_map_cont) {
    834   1.1  dbj 		panic("No map available in nextdma_start()");
    835   1.1  dbj 	}
    836   1.1  dbj #endif
    837   1.1  dbj 
    838   1.7  dbj 	next_dma_rotate(nd);
    839   1.7  dbj 
    840  1.22   tv #ifdef ND_DEBUG
    841  1.22   tv 	if (nextdma_debug) {
    842  1.22   tv 		char sbuf[256];
    843  1.22   tv 
    844  1.22   tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    845  1.22   tv 				 sbuf, sizeof(sbuf));
    846  1.22   tv 		printf("DMA initiating DMA %s of %d segments on intr(0x%s)\n",
    847  1.22   tv 			(dmadir == DMACSR_SETREAD ? "read" : "write"), nd->_nd_map->dm_nsegs, sbuf);
    848  1.22   tv 	}
    849  1.22   tv #endif
    850   1.1  dbj 
    851   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    852   1.1  dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    853  1.20  dbj 			DMACSR_INITBUF | DMACSR_RESET | dmadir);
    854   1.1  dbj 
    855   1.7  dbj 	next_dma_setup_curr_regs(nd);
    856   1.1  dbj 	next_dma_setup_cont_regs(nd);
    857   1.1  dbj 
    858   1.4  dbj #if (defined(ND_DEBUG))
    859  1.23  dbj 	if (nextdma_debug > 2) next_dma_print(nd);
    860   1.4  dbj #endif
    861   1.1  dbj 
    862  1.26  dbj 	if (nd->_nd_map_cont == NULL) {
    863  1.20  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    864  1.20  dbj 				DMACSR_SETENABLE | dmadir);
    865  1.20  dbj 	} else {
    866   1.1  dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    867  1.20  dbj 				DMACSR_SETSUPDATE | DMACSR_SETENABLE | dmadir);
    868   1.1  dbj 	}
    869   1.1  dbj }
    870