Home | History | Annotate | Line # | Download | only in dev
nextdma.c revision 1.22.2.2
      1  1.22.2.2  nathanw /*	$NetBSD: nextdma.c,v 1.22.2.2 2001/06/21 19:30:18 nathanw 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.22.2.2  nathanw #if defined(ND_DEBUG)
     68  1.22.2.2  nathanw int nextdma_debug_enetr_idx = 0;
     69  1.22.2.2  nathanw unsigned int nextdma_debug_enetr_state[100] = { 0 };
     70  1.22.2.2  nathanw int nextdma_debug_scsi_idx = 0;
     71  1.22.2.2  nathanw unsigned int nextdma_debug_scsi_state[100] = { 0 };
     72  1.22.2.2  nathanw 
     73  1.22.2.2  nathanw void nextdma_debug_initstate(struct nextdma_config *nd);
     74  1.22.2.2  nathanw void nextdma_debug_savestate(struct nextdma_config *nd, unsigned int state);
     75  1.22.2.2  nathanw void nextdma_debug_scsi_dumpstate(void);
     76  1.22.2.2  nathanw void nextdma_debug_enetr_dumpstate(void);
     77  1.22.2.2  nathanw 
     78  1.22.2.2  nathanw void
     79  1.22.2.2  nathanw nextdma_debug_initstate(struct nextdma_config *nd)
     80  1.22.2.2  nathanw {
     81  1.22.2.2  nathanw 	switch(nd->nd_intr) {
     82  1.22.2.2  nathanw 	case NEXT_I_ENETR_DMA:
     83  1.22.2.2  nathanw 		memset(nextdma_debug_enetr_state,0,sizeof(nextdma_debug_enetr_state));
     84  1.22.2.2  nathanw 		break;
     85  1.22.2.2  nathanw 	case NEXT_I_SCSI_DMA:
     86  1.22.2.2  nathanw 		memset(nextdma_debug_scsi_state,0,sizeof(nextdma_debug_scsi_state));
     87  1.22.2.2  nathanw 		break;
     88  1.22.2.2  nathanw 	}
     89  1.22.2.2  nathanw }
     90  1.22.2.2  nathanw 
     91  1.22.2.2  nathanw void
     92  1.22.2.2  nathanw nextdma_debug_savestate(struct nextdma_config *nd, unsigned int state)
     93  1.22.2.2  nathanw {
     94  1.22.2.2  nathanw 	switch(nd->nd_intr) {
     95  1.22.2.2  nathanw 	case NEXT_I_ENETR_DMA:
     96  1.22.2.2  nathanw 		nextdma_debug_enetr_state[nextdma_debug_enetr_idx++] = state;
     97  1.22.2.2  nathanw 		nextdma_debug_enetr_idx %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
     98  1.22.2.2  nathanw 		break;
     99  1.22.2.2  nathanw 	case NEXT_I_SCSI_DMA:
    100  1.22.2.2  nathanw 		nextdma_debug_scsi_state[nextdma_debug_scsi_idx++] = state;
    101  1.22.2.2  nathanw 		nextdma_debug_scsi_idx %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
    102  1.22.2.2  nathanw 		break;
    103  1.22.2.2  nathanw 	}
    104  1.22.2.2  nathanw }
    105  1.22.2.2  nathanw 
    106  1.22.2.2  nathanw void
    107  1.22.2.2  nathanw nextdma_debug_enetr_dumpstate(void)
    108  1.22.2.2  nathanw {
    109  1.22.2.2  nathanw 	int i;
    110  1.22.2.2  nathanw 	int s;
    111  1.22.2.2  nathanw 	s = spldma();
    112  1.22.2.2  nathanw 	i = nextdma_debug_enetr_idx;
    113  1.22.2.2  nathanw 	do {
    114  1.22.2.2  nathanw 		char sbuf[256];
    115  1.22.2.2  nathanw 		if (nextdma_debug_enetr_state[i]) {
    116  1.22.2.2  nathanw 			bitmask_snprintf(nextdma_debug_enetr_state[i], DMACSR_BITS, sbuf, sizeof(sbuf));
    117  1.22.2.2  nathanw 			printf("DMA: 0x%02x state 0x%s\n",i,sbuf);
    118  1.22.2.2  nathanw 		}
    119  1.22.2.2  nathanw 		i++;
    120  1.22.2.2  nathanw 		i %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
    121  1.22.2.2  nathanw 	} while (i != nextdma_debug_enetr_idx);
    122  1.22.2.2  nathanw 	splx(s);
    123  1.22.2.2  nathanw }
    124  1.22.2.2  nathanw 
    125  1.22.2.2  nathanw void
    126  1.22.2.2  nathanw nextdma_debug_scsi_dumpstate(void)
    127  1.22.2.2  nathanw {
    128  1.22.2.2  nathanw 	int i;
    129  1.22.2.2  nathanw 	int s;
    130  1.22.2.2  nathanw 	s = spldma();
    131  1.22.2.2  nathanw 	i = nextdma_debug_scsi_idx;
    132  1.22.2.2  nathanw 	do {
    133  1.22.2.2  nathanw 		char sbuf[256];
    134  1.22.2.2  nathanw 		if (nextdma_debug_scsi_state[i]) {
    135  1.22.2.2  nathanw 			bitmask_snprintf(nextdma_debug_scsi_state[i], DMACSR_BITS, sbuf, sizeof(sbuf));
    136  1.22.2.2  nathanw 			printf("DMA: 0x%02x state 0x%s\n",i,sbuf);
    137  1.22.2.2  nathanw 		}
    138  1.22.2.2  nathanw 		i++;
    139  1.22.2.2  nathanw 		i %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
    140  1.22.2.2  nathanw 	} while (i != nextdma_debug_scsi_idx);
    141  1.22.2.2  nathanw 	splx(s);
    142  1.22.2.2  nathanw }
    143  1.22.2.2  nathanw #endif
    144  1.22.2.2  nathanw 
    145  1.22.2.2  nathanw 
    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.22.2.2  nathanw 	if ((nd->_nd_map) || (nd->_nd_map_cont)) {
    255  1.22.2.2  nathanw 		/* @@@ clean up dma maps */
    256  1.22.2.2  nathanw 		panic("DMA abort not implemented\n");
    257  1.22.2.2  nathanw 	}
    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.22.2.2  nathanw 			if (nd->_nd_map_cont) {
    286  1.22.2.2  nathanw 				nd->_nd_map_cont->dm_xfer_len = 0;
    287  1.22.2.2  nathanw 			}
    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.22.2.2  nathanw #if defined(DIAGNOSTIC) && 0
    295       1.7      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.22.2.2  nathanw 			dd_stop += 15;
    329      1.20      dbj 		}
    330      1.20      dbj 	} else {
    331      1.20      dbj 		dd_start = 0xdeadbeef;
    332      1.20      dbj 		dd_stop = 0xdeadbeef;
    333      1.20      dbj 	}
    334       1.1      dbj 
    335      1.20      dbj 	dd_saved_start = dd_start;
    336      1.20      dbj 	dd_saved_stop  = dd_stop;
    337      1.15      dbj 
    338      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_START, dd_start);
    339      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_STOP, dd_stop);
    340      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START, dd_saved_start);
    341      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP, dd_saved_stop);
    342       1.1      dbj 
    343      1.20      dbj #ifdef DIAGNOSTIC
    344  1.22.2.1  nathanw 	if (   (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START) != dd_start)
    345  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP) != dd_stop)
    346  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START) != dd_saved_start)
    347  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP) != dd_saved_stop)
    348  1.22.2.1  nathanw 			) {
    349      1.20      dbj 		next_dma_print(nd);
    350      1.20      dbj 		panic("DMA failure writing to continue regs");
    351       1.1      dbj 	}
    352       1.7      dbj #endif
    353       1.1      dbj }
    354       1.1      dbj 
    355       1.1      dbj void
    356       1.1      dbj next_dma_setup_curr_regs(nd)
    357       1.1      dbj 	struct nextdma_config *nd;
    358       1.1      dbj {
    359      1.20      dbj 	bus_addr_t dd_next;
    360      1.20      dbj 	bus_addr_t dd_limit;
    361      1.20      dbj 	bus_addr_t dd_saved_next;
    362      1.20      dbj 	bus_addr_t dd_saved_limit;
    363      1.20      dbj 
    364       1.1      dbj 	DPRINTF(("DMA next_dma_setup_curr_regs()\n"));
    365       1.1      dbj 
    366      1.15      dbj 
    367      1.15      dbj 	if (nd->_nd_map) {
    368      1.20      dbj 		dd_next = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
    369      1.20      dbj 		dd_limit = (nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr +
    370      1.20      dbj 				nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    371  1.22.2.1  nathanw 
    372      1.15      dbj 		if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    373      1.20      dbj 			dd_limit |= 0x80000000; /* Ethernet transmit needs secret magic */
    374  1.22.2.2  nathanw 			dd_limit += 15;
    375      1.20      dbj 		}
    376      1.20      dbj 	} else {
    377      1.20      dbj 		dd_next = 0xdeadbeef;
    378      1.20      dbj 		dd_limit = 0xdeadbeef;
    379      1.20      dbj 	}
    380       1.1      dbj 
    381      1.20      dbj 	dd_saved_next = dd_next;
    382      1.20      dbj 	dd_saved_limit = dd_limit;
    383       1.1      dbj 
    384      1.20      dbj 	if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    385      1.20      dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF, dd_next);
    386      1.15      dbj 	} else {
    387      1.20      dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_NEXT, dd_next);
    388      1.15      dbj 	}
    389      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT, dd_limit);
    390      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT, dd_saved_next);
    391      1.20      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT, dd_saved_limit);
    392       1.1      dbj 
    393      1.20      dbj #ifdef DIAGNOSTIC
    394  1.22.2.1  nathanw 	if (   (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF) != dd_next)
    395  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT) != dd_next)
    396  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT) != dd_limit)
    397  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT) != dd_saved_next)
    398  1.22.2.1  nathanw 			|| (bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT) != dd_saved_limit)
    399  1.22.2.1  nathanw 			) {
    400      1.20      dbj 		next_dma_print(nd);
    401      1.20      dbj 		panic("DMA failure writing to current regs");
    402      1.20      dbj 	}
    403       1.7      dbj #endif
    404       1.1      dbj }
    405       1.1      dbj 
    406       1.1      dbj 
    407       1.1      dbj /* This routine is used for debugging */
    408       1.1      dbj 
    409       1.1      dbj void
    410       1.1      dbj next_dma_print(nd)
    411       1.1      dbj 	struct nextdma_config *nd;
    412       1.1      dbj {
    413       1.1      dbj 	u_long dd_csr;
    414       1.1      dbj 	u_long dd_next;
    415       1.1      dbj 	u_long dd_next_initbuf;
    416       1.1      dbj 	u_long dd_limit;
    417       1.1      dbj 	u_long dd_start;
    418       1.1      dbj 	u_long dd_stop;
    419       1.1      dbj 	u_long dd_saved_next;
    420       1.1      dbj 	u_long dd_saved_limit;
    421       1.1      dbj 	u_long dd_saved_start;
    422       1.1      dbj 	u_long dd_saved_stop;
    423      1.22       tv 	char sbuf[256];
    424       1.1      dbj 
    425      1.22       tv 	/* Read all of the registers before we print anything out,
    426       1.1      dbj 	 * in case something changes
    427       1.1      dbj 	 */
    428       1.1      dbj 	dd_csr          = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    429       1.1      dbj 	dd_next         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    430       1.1      dbj 	dd_next_initbuf = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT_INITBUF);
    431       1.1      dbj 	dd_limit        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    432       1.1      dbj 	dd_start        = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_START);
    433       1.1      dbj 	dd_stop         = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_STOP);
    434       1.1      dbj 	dd_saved_next   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    435       1.1      dbj 	dd_saved_limit  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    436       1.1      dbj 	dd_saved_start  = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_START);
    437       1.1      dbj 	dd_saved_stop   = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_STOP);
    438       1.1      dbj 
    439      1.22       tv 	bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    440      1.22       tv 			 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    441      1.22       tv 	printf("NDMAP: *intrstat = 0x%s\n", sbuf);
    442      1.22       tv 
    443      1.22       tv 	bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
    444      1.22       tv 			 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    445      1.22       tv 	printf("NDMAP: *intrmask = 0x%s\n", sbuf);
    446      1.20      dbj 
    447      1.12      dbj 	/* NDMAP is Next DMA Print (really!) */
    448      1.12      dbj 
    449       1.1      dbj 	if (nd->_nd_map) {
    450  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map->dm_mapsize = %ld\n",
    451      1.11      dbj 				nd->_nd_map->dm_mapsize);
    452      1.11      dbj 		printf("NDMAP: nd->_nd_map->dm_nsegs = %d\n",
    453      1.11      dbj 				nd->_nd_map->dm_nsegs);
    454  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map->dm_xfer_len = %ld\n",
    455  1.22.2.2  nathanw 				nd->_nd_map->dm_xfer_len);
    456       1.1      dbj 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    457       1.1      dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr);
    458  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map->dm_segs[%d].ds_len = %ld\n",
    459       1.1      dbj 				nd->_nd_idx,nd->_nd_map->dm_segs[nd->_nd_idx].ds_len);
    460  1.22.2.1  nathanw 		{
    461  1.22.2.1  nathanw 			int i;
    462  1.22.2.1  nathanw 			printf("NDMAP: Entire map;\n");
    463  1.22.2.1  nathanw 			for(i=0;i<nd->_nd_map->dm_nsegs;i++) {
    464  1.22.2.1  nathanw 				printf("NDMAP:   nd->_nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    465  1.22.2.1  nathanw 						i,nd->_nd_map->dm_segs[i].ds_addr);
    466  1.22.2.2  nathanw 				printf("NDMAP:   nd->_nd_map->dm_segs[%d].ds_len = %ld\n",
    467  1.22.2.1  nathanw 						i,nd->_nd_map->dm_segs[i].ds_len);
    468  1.22.2.1  nathanw 			}
    469  1.22.2.1  nathanw 		}
    470       1.1      dbj 	} else {
    471       1.1      dbj 		printf("NDMAP: nd->_nd_map = NULL\n");
    472       1.1      dbj 	}
    473       1.1      dbj 	if (nd->_nd_map_cont) {
    474  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map_cont->dm_mapsize = %ld\n",
    475      1.11      dbj 				nd->_nd_map_cont->dm_mapsize);
    476      1.11      dbj 		printf("NDMAP: nd->_nd_map_cont->dm_nsegs = %d\n",
    477      1.11      dbj 				nd->_nd_map_cont->dm_nsegs);
    478  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map_cont->dm_xfer_len = %ld\n",
    479  1.22.2.2  nathanw 				nd->_nd_map_cont->dm_xfer_len);
    480       1.1      dbj 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    481       1.1      dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_addr);
    482  1.22.2.2  nathanw 		printf("NDMAP: nd->_nd_map_cont->dm_segs[%d].ds_len = %ld\n",
    483       1.1      dbj 				nd->_nd_idx_cont,nd->_nd_map_cont->dm_segs[nd->_nd_idx_cont].ds_len);
    484  1.22.2.1  nathanw 		if (nd->_nd_map_cont != nd->_nd_map) {
    485  1.22.2.1  nathanw 			int i;
    486  1.22.2.1  nathanw 			printf("NDMAP: Entire map;\n");
    487  1.22.2.1  nathanw 			for(i=0;i<nd->_nd_map_cont->dm_nsegs;i++) {
    488  1.22.2.1  nathanw 				printf("NDMAP:   nd->_nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    489  1.22.2.1  nathanw 						i,nd->_nd_map_cont->dm_segs[i].ds_addr);
    490  1.22.2.2  nathanw 				printf("NDMAP:   nd->_nd_map_cont->dm_segs[%d].ds_len = %ld\n",
    491  1.22.2.1  nathanw 						i,nd->_nd_map_cont->dm_segs[i].ds_len);
    492  1.22.2.1  nathanw 			}
    493  1.22.2.1  nathanw 		}
    494       1.1      dbj 	} else {
    495       1.1      dbj 		printf("NDMAP: nd->_nd_map_cont = NULL\n");
    496       1.1      dbj 	}
    497       1.1      dbj 
    498      1.22       tv 	bitmask_snprintf(dd_csr, DMACSR_BITS, sbuf, sizeof(sbuf));
    499      1.22       tv 	printf("NDMAP: dd->dd_csr          = 0x%s\n",   sbuf);
    500      1.22       tv 
    501  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_saved_next   = 0x%08lx\n", dd_saved_next);
    502  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_saved_limit  = 0x%08lx\n", dd_saved_limit);
    503  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_saved_start  = 0x%08lx\n", dd_saved_start);
    504  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_saved_stop   = 0x%08lx\n", dd_saved_stop);
    505  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_next         = 0x%08lx\n", dd_next);
    506  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_next_initbuf = 0x%08lx\n", dd_next_initbuf);
    507  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_limit        = 0x%08lx\n", dd_limit);
    508  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_start        = 0x%08lx\n", dd_start);
    509  1.22.2.2  nathanw 	printf("NDMAP: dd->dd_stop         = 0x%08lx\n", dd_stop);
    510       1.1      dbj 
    511      1.22       tv 	bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    512      1.22       tv 			 sbuf, sizeof(sbuf));
    513      1.22       tv 	printf("NDMAP: interrupt ipl (%ld) intr(0x%s)\n",
    514      1.22       tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    515       1.1      dbj }
    516       1.1      dbj 
    517       1.1      dbj /****************************************************************/
    518       1.1      dbj 
    519       1.1      dbj int
    520       1.1      dbj nextdma_intr(arg)
    521       1.1      dbj      void *arg;
    522       1.1      dbj {
    523       1.1      dbj   /* @@@ This is bogus, we can't be certain of arg's type
    524      1.18      dbj 	 * unless the interrupt is for us.  For now we successfully
    525      1.18      dbj 	 * cheat because DMA interrupts are the only things invoked
    526      1.18      dbj 	 * at this interrupt level.
    527       1.1      dbj 	 */
    528      1.18      dbj   struct nextdma_config *nd = arg;
    529       1.1      dbj 
    530       1.1      dbj   if (!INTR_OCCURRED(nd->nd_intr)) return 0;
    531       1.1      dbj   /* Handle dma interrupts */
    532       1.1      dbj 
    533      1.22       tv #ifdef ND_DEBUG
    534      1.22       tv 	if (nextdma_debug) {
    535      1.22       tv 		char sbuf[256];
    536      1.22       tv 
    537      1.22       tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    538      1.22       tv 				 sbuf, sizeof(sbuf));
    539      1.22       tv 		printf("DMA interrupt ipl (%ld) intr(0x%s)\n",
    540      1.22       tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    541      1.22       tv 	}
    542      1.22       tv #endif
    543       1.1      dbj 
    544       1.7      dbj #ifdef DIAGNOSTIC
    545       1.7      dbj 	if (!nd->_nd_map) {
    546       1.7      dbj 		next_dma_print(nd);
    547       1.7      dbj 		panic("DMA missing current map in interrupt!\n");
    548       1.7      dbj 	}
    549       1.7      dbj #endif
    550       1.7      dbj 
    551       1.1      dbj   {
    552  1.22.2.1  nathanw     unsigned int state = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_CSR);
    553  1.22.2.2  nathanw 
    554  1.22.2.2  nathanw #if defined(ND_DEBUG)
    555  1.22.2.2  nathanw 		nextdma_debug_savestate(nd,state);
    556  1.22.2.2  nathanw #endif
    557  1.22.2.2  nathanw 
    558  1.22.2.2  nathanw #ifdef DIAGNOSTIC
    559  1.22.2.2  nathanw 		if (!(state & DMACSR_COMPLETE)) {
    560  1.22.2.2  nathanw 			char sbuf[256];
    561  1.22.2.2  nathanw 			next_dma_print(nd);
    562  1.22.2.2  nathanw 			bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    563  1.22.2.2  nathanw 			printf("DMA: state 0x%s\n",sbuf);
    564  1.22.2.2  nathanw 			panic("DMA complete not set in interrupt\n");
    565  1.22.2.2  nathanw 		}
    566  1.22.2.2  nathanw #endif
    567  1.22.2.2  nathanw 
    568  1.22.2.2  nathanw 		{
    569  1.22.2.1  nathanw 			bus_addr_t onext;
    570  1.22.2.1  nathanw 			bus_addr_t olimit;
    571  1.22.2.1  nathanw 			bus_addr_t slimit;
    572       1.1      dbj 
    573  1.22.2.1  nathanw 			DPRINTF(("DMA: finishing xfer\n"));
    574  1.22.2.1  nathanw 
    575  1.22.2.1  nathanw 			onext = nd->_nd_map->dm_segs[nd->_nd_idx].ds_addr;
    576  1.22.2.1  nathanw 			olimit = onext + nd->_nd_map->dm_segs[nd->_nd_idx].ds_len;
    577      1.22       tv 
    578  1.22.2.1  nathanw 			{
    579  1.22.2.1  nathanw 				int result = 0;
    580  1.22.2.1  nathanw 				if (state & DMACSR_ENABLE) {
    581  1.22.2.1  nathanw 					/* enable bit was set */
    582  1.22.2.1  nathanw 					result |= 0x01;
    583  1.22.2.1  nathanw 				}
    584  1.22.2.1  nathanw 				if (state & DMACSR_SUPDATE) {
    585  1.22.2.1  nathanw 					/* supdate bit was set */
    586  1.22.2.1  nathanw 					result |= 0x02;
    587  1.22.2.1  nathanw 				}
    588  1.22.2.2  nathanw 				if (nd->_nd_map_cont == NULL) {
    589  1.22.2.2  nathanw 					KASSERT(nd->_nd_idx+1 == nd->_nd_map->dm_nsegs);
    590  1.22.2.1  nathanw 					/* Expecting a shutdown, didn't SETSUPDATE last turn */
    591  1.22.2.1  nathanw 					result |= 0x04;
    592  1.22.2.1  nathanw 				}
    593  1.22.2.1  nathanw 				if (state & DMACSR_BUSEXC) {
    594  1.22.2.1  nathanw 					/* bus exception bit was set */
    595  1.22.2.1  nathanw 					result |= 0x08;
    596  1.22.2.1  nathanw 				}
    597  1.22.2.1  nathanw 				switch (result) {
    598  1.22.2.1  nathanw 				case 0x00: /* !BUSEXC && !expecting && !SUPDATE && !ENABLE */
    599  1.22.2.1  nathanw 				case 0x08: /* BUSEXC && !expecting && !SUPDATE && !ENABLE */
    600  1.22.2.2  nathanw 					if (nd->nd_intr == NEXT_I_SCSI_DMA) {
    601  1.22.2.2  nathanw 						slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    602  1.22.2.2  nathanw 					} else {
    603  1.22.2.2  nathanw 						slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    604  1.22.2.2  nathanw 					}
    605  1.22.2.1  nathanw 					break;
    606  1.22.2.1  nathanw 				case 0x01: /* !BUSEXC && !expecting && !SUPDATE && ENABLE */
    607  1.22.2.1  nathanw 				case 0x09: /* BUSEXC && !expecting && !SUPDATE && ENABLE */
    608  1.22.2.2  nathanw 					if (nd->nd_intr == NEXT_I_SCSI_DMA) {
    609  1.22.2.2  nathanw 						bus_addr_t snext;
    610  1.22.2.2  nathanw 						snext = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_NEXT);
    611  1.22.2.2  nathanw 						if (snext != onext) {
    612  1.22.2.2  nathanw 							slimit = olimit;
    613  1.22.2.2  nathanw 						} else {
    614  1.22.2.2  nathanw 							slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    615  1.22.2.2  nathanw 						}
    616  1.22.2.2  nathanw 					} else {
    617  1.22.2.2  nathanw 						slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_SAVED_LIMIT);
    618  1.22.2.1  nathanw 					}
    619  1.22.2.1  nathanw 					break;
    620  1.22.2.1  nathanw 				case 0x02: /* !BUSEXC && !expecting && SUPDATE && !ENABLE */
    621  1.22.2.1  nathanw 				case 0x0a: /* BUSEXC && !expecting && SUPDATE && !ENABLE */
    622  1.22.2.1  nathanw 					slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_NEXT);
    623  1.22.2.1  nathanw 					break;
    624  1.22.2.1  nathanw 				case 0x04:  /* !BUSEXC && expecting && !SUPDATE && !ENABLE */
    625  1.22.2.1  nathanw 				case 0x0c: /* BUSEXC && expecting && !SUPDATE && !ENABLE */
    626  1.22.2.1  nathanw 					slimit = bus_space_read_4(nd->nd_bst, nd->nd_bsh, DD_LIMIT);
    627  1.22.2.1  nathanw 					break;
    628  1.22.2.1  nathanw 				default:
    629  1.22.2.1  nathanw #ifdef DIAGNOSTIC
    630  1.22.2.2  nathanw 					{
    631  1.22.2.2  nathanw 						char sbuf[256];
    632  1.22.2.2  nathanw 						printf("DMA: please send this output to port-next68k-maintainer (at) netbsd.org:\n");
    633  1.22.2.2  nathanw 						bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    634  1.22.2.2  nathanw 						printf("DMA: state 0x%s\n",sbuf);
    635  1.22.2.2  nathanw 						next_dma_print(nd);
    636  1.22.2.2  nathanw 						panic("DMA: condition 0x%02x not yet documented to occur\n",result);
    637  1.22.2.2  nathanw 					}
    638  1.22.2.1  nathanw #endif
    639  1.22.2.1  nathanw 					slimit = olimit;
    640  1.22.2.1  nathanw 					break;
    641  1.22.2.1  nathanw 				}
    642  1.22.2.1  nathanw 			}
    643      1.22       tv 
    644  1.22.2.1  nathanw 			if (nd->nd_intr == NEXT_I_ENETX_DMA) {
    645  1.22.2.1  nathanw 				slimit &= ~0x80000000;
    646  1.22.2.2  nathanw 				slimit -= 15;
    647  1.22.2.1  nathanw 			}
    648  1.22.2.1  nathanw 
    649  1.22.2.1  nathanw #ifdef DIAGNOSTIC
    650  1.22.2.1  nathanw 			if ((slimit < onext) || (slimit > olimit)) {
    651  1.22.2.2  nathanw 				char sbuf[256];
    652  1.22.2.2  nathanw 				bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    653  1.22.2.2  nathanw 				printf("DMA: state 0x%s\n",sbuf);
    654  1.22.2.1  nathanw 				next_dma_print(nd);
    655  1.22.2.2  nathanw 				panic("DMA: Unexpected limit register (0x%08lx) in finish_xfer\n",slimit);
    656  1.22.2.2  nathanw 			}
    657  1.22.2.2  nathanw #endif
    658  1.22.2.2  nathanw 
    659  1.22.2.2  nathanw #ifdef DIAGNOSTIC
    660  1.22.2.2  nathanw 			if ((state & DMACSR_ENABLE) && ((nd->_nd_idx+1) != nd->_nd_map->dm_nsegs)) {
    661  1.22.2.2  nathanw 				if (slimit != olimit) {
    662  1.22.2.2  nathanw 					char sbuf[256];
    663  1.22.2.2  nathanw 					bitmask_snprintf(state, DMACSR_BITS, sbuf, sizeof(sbuf));
    664  1.22.2.2  nathanw 					printf("DMA: state 0x%s\n",sbuf);
    665  1.22.2.2  nathanw 					next_dma_print(nd);
    666  1.22.2.2  nathanw 					panic("DMA: short limit register (0x%08lx) w/o finishing map.\n",slimit);
    667  1.22.2.2  nathanw 				}
    668  1.22.2.1  nathanw 			}
    669       1.1      dbj #endif
    670       1.1      dbj 
    671  1.22.2.1  nathanw #if (defined(ND_DEBUG))
    672  1.22.2.1  nathanw 			if (nextdma_debug > 2) next_dma_print(nd);
    673  1.22.2.1  nathanw #endif
    674       1.7      dbj 
    675  1.22.2.2  nathanw 			nd->_nd_map->dm_xfer_len += slimit-onext;
    676      1.12      dbj 
    677  1.22.2.1  nathanw 			/* If we've reached the end of the current map, then inform
    678  1.22.2.1  nathanw 			 * that we've completed that map.
    679  1.22.2.1  nathanw 			 */
    680  1.22.2.2  nathanw 			if ((nd->_nd_idx+1) == nd->_nd_map->dm_nsegs) {
    681  1.22.2.1  nathanw 				if (nd->nd_completed_cb)
    682  1.22.2.1  nathanw 					(*nd->nd_completed_cb)(nd->_nd_map, nd->nd_cb_arg);
    683  1.22.2.2  nathanw 			} else {
    684  1.22.2.2  nathanw 				KASSERT(nd->_nd_map == nd->_nd_map_cont);
    685  1.22.2.2  nathanw 				KASSERT(nd->_nd_idx+1 == nd->_nd_idx_cont);
    686  1.22.2.1  nathanw 			}
    687  1.22.2.1  nathanw 			nd->_nd_map = 0;
    688  1.22.2.1  nathanw 			nd->_nd_idx = 0;
    689  1.22.2.1  nathanw 		}
    690      1.22       tv 
    691  1.22.2.1  nathanw 		if (state & DMACSR_ENABLE) {
    692  1.22.2.1  nathanw 
    693  1.22.2.1  nathanw 			next_dma_rotate(nd);
    694  1.22.2.1  nathanw 			next_dma_setup_cont_regs(nd);
    695      1.22       tv 
    696  1.22.2.1  nathanw 			{
    697  1.22.2.1  nathanw 				u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    698  1.22.2.1  nathanw 
    699  1.22.2.1  nathanw 				if (state & DMACSR_READ) {
    700  1.22.2.1  nathanw 					dmadir = DMACSR_SETREAD;
    701  1.22.2.1  nathanw 				} else {
    702  1.22.2.1  nathanw 					dmadir = DMACSR_SETWRITE;
    703  1.22.2.1  nathanw 				}
    704  1.22.2.1  nathanw 
    705  1.22.2.2  nathanw 				if (nd->_nd_map_cont == NULL) {
    706  1.22.2.2  nathanw 					KASSERT(nd->_nd_idx+1 == nd->_nd_map->dm_nsegs);
    707  1.22.2.1  nathanw 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    708  1.22.2.1  nathanw 							DMACSR_CLRCOMPLETE | dmadir);
    709  1.22.2.1  nathanw 				} else {
    710  1.22.2.1  nathanw 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    711  1.22.2.1  nathanw 							DMACSR_CLRCOMPLETE | dmadir | DMACSR_SETSUPDATE);
    712  1.22.2.1  nathanw 				}
    713       1.7      dbj 			}
    714       1.7      dbj 
    715  1.22.2.1  nathanw 		} else {
    716       1.7      dbj 
    717  1.22.2.1  nathanw 			DPRINTF(("DMA: a shutdown occurred\n"));
    718  1.22.2.1  nathanw 			bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
    719       1.7      dbj 
    720  1.22.2.1  nathanw 			/* Cleanup more incomplete transfers */
    721  1.22.2.2  nathanw #if 1
    722  1.22.2.2  nathanw 			/* cleanup continue map */
    723  1.22.2.1  nathanw 			if (nd->_nd_map_cont) {
    724  1.22.2.1  nathanw 				DPRINTF(("DMA: shutting down with non null continue map\n"));
    725  1.22.2.1  nathanw 				if (nd->nd_completed_cb)
    726  1.22.2.1  nathanw 					(*nd->nd_completed_cb)(nd->_nd_map_cont, nd->nd_cb_arg);
    727  1.22.2.1  nathanw 
    728  1.22.2.1  nathanw 				nd->_nd_map_cont = 0;
    729  1.22.2.1  nathanw 				nd->_nd_idx_cont = 0;
    730  1.22.2.1  nathanw 			}
    731  1.22.2.1  nathanw #else
    732  1.22.2.2  nathanw 			/* Do an automatic dma restart */
    733  1.22.2.2  nathanw 			if (nd->_nd_map_cont) {
    734  1.22.2.2  nathanw 				u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    735  1.22.2.2  nathanw 
    736  1.22.2.1  nathanw 				next_dma_rotate(nd);
    737       1.1      dbj 
    738  1.22.2.1  nathanw 				if (state & DMACSR_READ) {
    739  1.22.2.1  nathanw 					dmadir = DMACSR_SETREAD;
    740  1.22.2.1  nathanw 				} else {
    741  1.22.2.1  nathanw 					dmadir = DMACSR_SETWRITE;
    742  1.22.2.1  nathanw 				}
    743  1.22.2.1  nathanw 
    744  1.22.2.1  nathanw 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    745  1.22.2.1  nathanw 				bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    746  1.22.2.1  nathanw 						DMACSR_INITBUF | DMACSR_RESET | dmadir);
    747  1.22.2.1  nathanw 
    748  1.22.2.1  nathanw 				next_dma_setup_curr_regs(nd);
    749  1.22.2.1  nathanw 				next_dma_setup_cont_regs(nd);
    750  1.22.2.1  nathanw 
    751  1.22.2.2  nathanw 				if (nd->_nd_map_cont == NULL) {
    752  1.22.2.2  nathanw 					KASSERT(nd->_nd_idx+1 == nd->_nd_map->dm_nsegs);
    753  1.22.2.1  nathanw 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    754  1.22.2.1  nathanw 							DMACSR_SETENABLE | dmadir);
    755  1.22.2.1  nathanw 				} else {
    756  1.22.2.1  nathanw 					bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    757  1.22.2.1  nathanw 							DMACSR_SETSUPDATE | DMACSR_SETENABLE | dmadir);
    758  1.22.2.1  nathanw 				}
    759  1.22.2.1  nathanw 				return 1;
    760      1.21      dbj 			}
    761  1.22.2.1  nathanw #endif
    762  1.22.2.1  nathanw 			if (nd->nd_shutdown_cb) (*nd->nd_shutdown_cb)(nd->nd_cb_arg);
    763       1.1      dbj 		}
    764       1.1      dbj 	}
    765       1.1      dbj 
    766      1.22       tv #ifdef ND_DEBUG
    767      1.22       tv 	if (nextdma_debug) {
    768      1.22       tv 		char sbuf[256];
    769      1.22       tv 
    770      1.22       tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    771      1.22       tv 				 sbuf, sizeof(sbuf));
    772      1.22       tv 		printf("DMA exiting interrupt ipl (%ld) intr(0x%s)\n",
    773      1.22       tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    774      1.22       tv 	}
    775      1.22       tv #endif
    776       1.1      dbj 
    777       1.1      dbj   return(1);
    778       1.1      dbj }
    779       1.1      dbj 
    780       1.1      dbj /*
    781       1.1      dbj  * Check to see if dma has finished for a channel */
    782       1.1      dbj int
    783       1.1      dbj nextdma_finished(nd)
    784       1.1      dbj 	struct nextdma_config *nd;
    785       1.1      dbj {
    786       1.1      dbj 	int r;
    787       1.1      dbj 	int s;
    788       1.1      dbj 	s = spldma();									/* @@@ should this be splimp()? */
    789       1.1      dbj 	r = (nd->_nd_map == NULL) && (nd->_nd_map_cont == NULL);
    790       1.1      dbj 	splx(s);
    791       1.1      dbj 	return(r);
    792       1.1      dbj }
    793       1.1      dbj 
    794       1.1      dbj void
    795       1.1      dbj nextdma_start(nd, dmadir)
    796       1.1      dbj 	struct nextdma_config *nd;
    797      1.19      dbj 	u_long dmadir;								/* 	DMACSR_SETREAD or DMACSR_SETWRITE */
    798       1.1      dbj {
    799       1.1      dbj 
    800       1.1      dbj #ifdef DIAGNOSTIC
    801       1.1      dbj 	if (!nextdma_finished(nd)) {
    802      1.22       tv 		char sbuf[256];
    803      1.22       tv 
    804      1.22       tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    805      1.22       tv 				 sbuf, sizeof(sbuf));
    806      1.22       tv 		panic("DMA trying to start before previous finished on intr(0x%s)\n", sbuf);
    807       1.1      dbj 	}
    808       1.1      dbj #endif
    809       1.1      dbj 
    810      1.22       tv #ifdef ND_DEBUG
    811      1.22       tv 	if (nextdma_debug) {
    812      1.22       tv 		char sbuf[256];
    813      1.22       tv 
    814      1.22       tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    815      1.22       tv 				 sbuf, sizeof(sbuf));
    816      1.22       tv 		printf("DMA start (%ld) intr(0x%s)\n",
    817      1.22       tv 			NEXT_I_IPL(nd->nd_intr), sbuf);
    818      1.22       tv 	}
    819      1.22       tv #endif
    820       1.1      dbj 
    821       1.1      dbj #ifdef DIAGNOSTIC
    822       1.1      dbj 	if (nd->_nd_map) {
    823       1.1      dbj 		next_dma_print(nd);
    824       1.1      dbj 		panic("DMA: nextdma_start() with non null map\n");
    825       1.1      dbj 	}
    826       1.1      dbj 	if (nd->_nd_map_cont) {
    827       1.1      dbj 		next_dma_print(nd);
    828       1.1      dbj 		panic("DMA: nextdma_start() with non null continue map\n");
    829       1.1      dbj 	}
    830       1.1      dbj #endif
    831       1.1      dbj 
    832       1.9      dbj #ifdef DIAGNOSTIC
    833      1.19      dbj 	if ((dmadir != DMACSR_SETREAD) && (dmadir != DMACSR_SETWRITE)) {
    834      1.19      dbj 		panic("DMA: nextdma_start(), dmadir arg must be DMACSR_SETREAD or DMACSR_SETWRITE\n");
    835       1.9      dbj 	}
    836       1.9      dbj #endif
    837       1.9      dbj 
    838  1.22.2.2  nathanw #if defined(ND_DEBUG)
    839  1.22.2.2  nathanw 	nextdma_debug_initstate(nd);
    840  1.22.2.2  nathanw #endif
    841  1.22.2.2  nathanw 
    842       1.7      dbj 	/* preload both the current and the continue maps */
    843       1.1      dbj 	next_dma_rotate(nd);
    844       1.1      dbj 
    845       1.1      dbj #ifdef DIAGNOSTIC
    846       1.1      dbj 	if (!nd->_nd_map_cont) {
    847       1.1      dbj 		panic("No map available in nextdma_start()");
    848       1.1      dbj 	}
    849       1.1      dbj #endif
    850       1.1      dbj 
    851       1.7      dbj 	next_dma_rotate(nd);
    852       1.7      dbj 
    853      1.22       tv #ifdef ND_DEBUG
    854      1.22       tv 	if (nextdma_debug) {
    855      1.22       tv 		char sbuf[256];
    856      1.22       tv 
    857      1.22       tv 		bitmask_snprintf(NEXT_I_BIT(nd->nd_intr), NEXT_INTR_BITS,
    858      1.22       tv 				 sbuf, sizeof(sbuf));
    859      1.22       tv 		printf("DMA initiating DMA %s of %d segments on intr(0x%s)\n",
    860      1.22       tv 			(dmadir == DMACSR_SETREAD ? "read" : "write"), nd->_nd_map->dm_nsegs, sbuf);
    861      1.22       tv 	}
    862      1.22       tv #endif
    863       1.1      dbj 
    864       1.1      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR, 0);
    865       1.1      dbj 	bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    866      1.20      dbj 			DMACSR_INITBUF | DMACSR_RESET | dmadir);
    867       1.1      dbj 
    868       1.7      dbj 	next_dma_setup_curr_regs(nd);
    869       1.1      dbj 	next_dma_setup_cont_regs(nd);
    870       1.1      dbj 
    871       1.4      dbj #if (defined(ND_DEBUG))
    872  1.22.2.1  nathanw 	if (nextdma_debug > 2) next_dma_print(nd);
    873       1.4      dbj #endif
    874       1.1      dbj 
    875  1.22.2.2  nathanw 	if (nd->_nd_map_cont == NULL) {
    876      1.20      dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    877      1.20      dbj 				DMACSR_SETENABLE | dmadir);
    878      1.20      dbj 	} else {
    879       1.1      dbj 		bus_space_write_4(nd->nd_bst, nd->nd_bsh, DD_CSR,
    880      1.20      dbj 				DMACSR_SETSUPDATE | DMACSR_SETENABLE | dmadir);
    881       1.1      dbj 	}
    882       1.1      dbj }
    883