Home | History | Annotate | Line # | Download | only in dev
nextdma.c revision 1.49.10.1
      1  1.49.10.1  pgoyette /*	$NetBSD: nextdma.c,v 1.49.10.1 2017/04/26 02:53:06 pgoyette 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  *
     15        1.1       dbj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16        1.1       dbj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17        1.1       dbj  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18        1.1       dbj  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19        1.1       dbj  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20        1.1       dbj  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21        1.1       dbj  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22        1.1       dbj  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23        1.1       dbj  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24        1.1       dbj  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25        1.1       dbj  */
     26       1.35     lukem 
     27       1.35     lukem #include <sys/cdefs.h>
     28  1.49.10.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: nextdma.c,v 1.49.10.1 2017/04/26 02:53:06 pgoyette Exp $");
     29        1.1       dbj 
     30        1.1       dbj #include <sys/param.h>
     31        1.1       dbj #include <sys/systm.h>
     32        1.1       dbj #include <sys/mbuf.h>
     33        1.1       dbj #include <sys/syslog.h>
     34        1.1       dbj #include <sys/socket.h>
     35        1.1       dbj #include <sys/device.h>
     36        1.1       dbj #include <sys/malloc.h>
     37        1.1       dbj #include <sys/ioctl.h>
     38        1.1       dbj #include <sys/errno.h>
     39        1.1       dbj 
     40       1.31   mycroft #define _M68K_BUS_DMA_PRIVATE
     41        1.1       dbj #include <machine/autoconf.h>
     42        1.1       dbj #include <machine/cpu.h>
     43        1.1       dbj #include <machine/intr.h>
     44        1.5       dbj 
     45        1.5       dbj #include <m68k/cacheops.h>
     46        1.1       dbj 
     47        1.1       dbj #include <next68k/next68k/isr.h>
     48       1.31   mycroft #include <next68k/next68k/nextrom.h>
     49        1.1       dbj 
     50       1.31   mycroft #include <next68k/dev/intiovar.h>
     51        1.1       dbj 
     52        1.1       dbj #include "nextdmareg.h"
     53        1.1       dbj #include "nextdmavar.h"
     54        1.1       dbj 
     55       1.31   mycroft #include "esp.h"
     56       1.31   mycroft #include "xe.h"
     57       1.31   mycroft 
     58       1.31   mycroft #if DEBUG
     59        1.1       dbj #define ND_DEBUG
     60        1.1       dbj #endif
     61        1.1       dbj 
     62       1.31   mycroft extern int turbo;
     63       1.31   mycroft 
     64       1.40     perry #define panic		__asm volatile("trap  #15"); printf
     65       1.30  christos 
     66       1.31   mycroft #define NEXTDMA_DEBUG nextdma_debug
     67       1.31   mycroft /* (nsc->sc_chan->nd_intr == NEXT_I_SCSI_DMA) && nextdma_debug */
     68        1.1       dbj #if defined(ND_DEBUG)
     69        1.8       dbj int nextdma_debug = 0;
     70       1.30  christos #define DPRINTF(x) if (NEXTDMA_DEBUG) printf x;
     71       1.49  christos int ndtrace_show = 0;
     72       1.49  christos char ndtrace_buf[8192+100];
     73       1.49  christos size_t ndtrace_len = 0;
     74       1.49  christos #define NDTRACEIF(x) if (10) do {x;} while (0)
     75        1.1       dbj #else
     76        1.1       dbj #define DPRINTF(x)
     77       1.31   mycroft #define NDTRACEIF(x)
     78        1.1       dbj #endif
     79       1.31   mycroft #define PRINTF(x) printf x
     80        1.1       dbj 
     81       1.49  christos void
     82       1.49  christos ndtrace_printf(const char *fmt, ...) {
     83       1.49  christos #ifdef ND_DEBUG
     84       1.49  christos 	int len;
     85       1.49  christos 	va_list ap;
     86       1.49  christos 
     87       1.49  christos 	va_start(ap, fmt);
     88       1.49  christos 	len = vsnprintf(ndtrace_buf + ndtrace_len, sizeof(ndtrace_buf)
     89       1.49  christos 	    - ndtrace_len, fmt, ap);
     90       1.49  christos 	va_end(ap);
     91       1.49  christos 	ndtrace_len += len;
     92       1.49  christos #endif
     93       1.49  christos }
     94       1.49  christos 
     95       1.49  christos int
     96       1.49  christos ndtrace_empty(void) {
     97       1.49  christos #ifdef ND_DEBUG
     98       1.49  christos 	return ndtrace_len == 0;
     99       1.49  christos #else
    100       1.49  christos 	return 1;
    101       1.49  christos #endif
    102       1.49  christos }
    103       1.49  christos 
    104       1.49  christos void
    105       1.49  christos ndtrace_reset(void) {
    106       1.49  christos #ifdef ND_DEBUG
    107       1.49  christos 	ndtrace_len = 0;
    108       1.49  christos #endif
    109       1.49  christos }
    110       1.49  christos 
    111       1.49  christos void
    112       1.49  christos ndtrace_addc(int c) {
    113       1.49  christos #ifdef ND_DEBUG
    114       1.49  christos 	if (ndtrace_len < sizeof(ndtrace_buf) - 1) {
    115       1.49  christos 		ndtrace_buf[ndtrace_len++] = c;
    116       1.49  christos 		ndtrace_buf[ndtrace_len] = '\0';
    117       1.49  christos 	}
    118       1.49  christos #endif
    119       1.49  christos }
    120       1.49  christos 
    121       1.49  christos const char *
    122       1.49  christos ndtrace_get(void) {
    123       1.49  christos #ifdef ND_DEBUG
    124       1.49  christos 	return ndtrace_buf;
    125       1.49  christos #else
    126       1.49  christos 	return NULL;
    127       1.49  christos #endif
    128       1.49  christos }
    129       1.49  christos 
    130       1.49  christos 
    131       1.26       dbj #if defined(ND_DEBUG)
    132       1.26       dbj int nextdma_debug_enetr_idx = 0;
    133       1.26       dbj unsigned int nextdma_debug_enetr_state[100] = { 0 };
    134       1.26       dbj int nextdma_debug_scsi_idx = 0;
    135       1.26       dbj unsigned int nextdma_debug_scsi_state[100] = { 0 };
    136       1.26       dbj 
    137       1.31   mycroft void nextdma_debug_initstate(struct nextdma_softc *);
    138       1.31   mycroft void nextdma_debug_savestate(struct nextdma_softc *, unsigned int);
    139       1.26       dbj void nextdma_debug_scsi_dumpstate(void);
    140       1.26       dbj void nextdma_debug_enetr_dumpstate(void);
    141       1.31   mycroft #endif
    142       1.31   mycroft 
    143       1.31   mycroft 
    144       1.48       chs int	nextdma_match(device_t, cfdata_t, void *);
    145       1.48       chs void	nextdma_attach(device_t, device_t, void *);
    146       1.31   mycroft 
    147       1.37       chs void nextdmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, bus_size_t, int);
    148       1.37       chs int nextdma_continue(struct nextdma_softc *);
    149       1.37       chs void nextdma_rotate(struct nextdma_softc *);
    150       1.31   mycroft 
    151       1.37       chs void nextdma_setup_cont_regs(struct nextdma_softc *);
    152       1.37       chs void nextdma_setup_curr_regs(struct nextdma_softc *);
    153       1.31   mycroft 
    154       1.31   mycroft #if NESP > 0
    155       1.37       chs static int nextdma_esp_intr(void *);
    156       1.31   mycroft #endif
    157       1.31   mycroft #if NXE > 0
    158       1.37       chs static int nextdma_enet_intr(void *);
    159       1.31   mycroft #endif
    160       1.31   mycroft 
    161       1.37       chs #define nd_bsr4(reg) \
    162       1.37       chs 	bus_space_read_4(nsc->sc_bst, nsc->sc_bsh, (reg))
    163       1.37       chs #define nd_bsw4(reg,val) \
    164       1.37       chs 	bus_space_write_4(nsc->sc_bst, nsc->sc_bsh, (reg), (val))
    165       1.26       dbj 
    166       1.48       chs CFATTACH_DECL_NEW(nextdma, sizeof(struct nextdma_softc),
    167       1.34   thorpej     nextdma_match, nextdma_attach, NULL, NULL);
    168       1.31   mycroft 
    169       1.31   mycroft static struct nextdma_channel nextdma_channel[] = {
    170       1.31   mycroft #if NESP > 0
    171       1.31   mycroft 	{ "scsi", NEXT_P_SCSI_CSR, DD_SIZE, NEXT_I_SCSI_DMA, &nextdma_esp_intr },
    172       1.31   mycroft #endif
    173       1.31   mycroft #if NXE > 0
    174       1.31   mycroft 	{ "enetx", NEXT_P_ENETX_CSR, DD_SIZE, NEXT_I_ENETX_DMA, &nextdma_enet_intr },
    175       1.31   mycroft 	{ "enetr", NEXT_P_ENETR_CSR, DD_SIZE, NEXT_I_ENETR_DMA, &nextdma_enet_intr },
    176       1.31   mycroft #endif
    177       1.31   mycroft };
    178       1.31   mycroft static int nnextdma_channels = (sizeof(nextdma_channel)/sizeof(nextdma_channel[0]));
    179       1.31   mycroft 
    180       1.31   mycroft static int attached = 0;
    181       1.26       dbj 
    182       1.31   mycroft struct nextdma_softc *
    183       1.38        he nextdma_findchannel(const char *name)
    184       1.26       dbj {
    185       1.45    dyoung 	device_t dev;
    186       1.45    dyoung 	deviter_t di;
    187       1.26       dbj 
    188       1.45    dyoung 	for (dev = deviter_first(&di, DEVITER_F_ROOT_FIRST);
    189       1.45    dyoung 	     dev != NULL;
    190       1.45    dyoung 	     dev = deviter_next(&di)) {
    191       1.48       chs 		if (strncmp(device_xname(dev), "nextdma", 7) == 0) {
    192       1.45    dyoung 			struct nextdma_softc *nsc = device_private(dev);
    193       1.45    dyoung 			if (strcmp(nsc->sc_chan->nd_name, name) == 0)
    194       1.45    dyoung 				break;
    195       1.45    dyoung 		}
    196       1.45    dyoung 	}
    197       1.45    dyoung 	deviter_release(&di);
    198       1.45    dyoung 	if (dev == NULL)
    199       1.45    dyoung 		return NULL;
    200       1.45    dyoung 	return device_private(dev);
    201       1.26       dbj }
    202       1.26       dbj 
    203       1.31   mycroft int
    204       1.48       chs nextdma_match(device_t parent, cfdata_t match, void *aux)
    205       1.26       dbj {
    206       1.31   mycroft 	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
    207       1.26       dbj 
    208       1.31   mycroft 	if (attached >= nnextdma_channels)
    209       1.31   mycroft 		return (0);
    210       1.26       dbj 
    211       1.31   mycroft 	ia->ia_addr = (void *)nextdma_channel[attached].nd_base;
    212        1.1       dbj 
    213       1.31   mycroft 	return (1);
    214       1.31   mycroft }
    215        1.1       dbj 
    216        1.1       dbj void
    217       1.48       chs nextdma_attach(device_t parent, device_t self, void *aux)
    218        1.1       dbj {
    219       1.48       chs 	struct nextdma_softc *nsc = device_private(self);
    220       1.31   mycroft 	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
    221       1.31   mycroft 
    222       1.31   mycroft 	if (attached >= nnextdma_channels)
    223       1.31   mycroft 		return;
    224       1.31   mycroft 
    225       1.48       chs 	nsc->sc_dev = self;
    226       1.31   mycroft 	nsc->sc_chan = &nextdma_channel[attached];
    227        1.1       dbj 
    228       1.31   mycroft 	nsc->sc_dmat = ia->ia_dmat;
    229       1.31   mycroft 	nsc->sc_bst = ia->ia_bst;
    230        1.1       dbj 
    231       1.31   mycroft 	if (bus_space_map(nsc->sc_bst, nsc->sc_chan->nd_base,
    232       1.31   mycroft 			  nsc->sc_chan->nd_size, 0, &nsc->sc_bsh)) {
    233       1.32    provos 		panic("%s: can't map DMA registers for channel %s",
    234       1.48       chs 		      device_xname(self), nsc->sc_chan->nd_name);
    235        1.1       dbj 	}
    236        1.1       dbj 
    237       1.31   mycroft 	nextdma_init (nsc);
    238       1.30  christos 
    239       1.31   mycroft 	isrlink_autovec(nsc->sc_chan->nd_intrfunc, nsc,
    240       1.31   mycroft 			NEXT_I_IPL(nsc->sc_chan->nd_intr), 10, NULL);
    241       1.31   mycroft 	INTR_ENABLE(nsc->sc_chan->nd_intr);
    242        1.1       dbj 
    243       1.31   mycroft 	printf (": channel %d (%s)\n", attached,
    244       1.31   mycroft 		nsc->sc_chan->nd_name);
    245       1.31   mycroft 	attached++;
    246       1.31   mycroft 
    247       1.31   mycroft 	return;
    248        1.1       dbj }
    249        1.1       dbj 
    250        1.1       dbj void
    251       1.37       chs nextdma_init(struct nextdma_softc *nsc)
    252        1.1       dbj {
    253       1.22        tv #ifdef ND_DEBUG
    254       1.30  christos 	if (NEXTDMA_DEBUG) {
    255       1.22        tv 		char sbuf[256];
    256       1.22        tv 
    257       1.43  christos 		snprintb(sbuf, sizeof(sbuf), NEXT_INTR_BITS,
    258       1.47       mrg 		    NEXT_I_BIT(nsc->sc_chan->nd_intr));
    259  1.49.10.1  pgoyette 		printf("DMA init ipl (%ld) intr(%s)\n",
    260       1.31   mycroft 			NEXT_I_IPL(nsc->sc_chan->nd_intr), sbuf);
    261       1.22        tv 	}
    262       1.22        tv #endif
    263        1.1       dbj 
    264       1.31   mycroft 	nsc->sc_stat.nd_map = NULL;
    265       1.31   mycroft 	nsc->sc_stat.nd_idx = 0;
    266       1.31   mycroft 	nsc->sc_stat.nd_map_cont = NULL;
    267       1.31   mycroft 	nsc->sc_stat.nd_idx_cont = 0;
    268       1.31   mycroft 	nsc->sc_stat.nd_exception = 0;
    269        1.1       dbj 
    270       1.31   mycroft 	nd_bsw4 (DD_CSR, DMACSR_RESET | DMACSR_CLRCOMPLETE);
    271       1.31   mycroft 	nd_bsw4 (DD_CSR, 0);
    272        1.1       dbj 
    273       1.31   mycroft #if 01
    274       1.31   mycroft 	nextdma_setup_curr_regs(nsc);
    275       1.31   mycroft 	nextdma_setup_cont_regs(nsc);
    276       1.31   mycroft #endif
    277        1.1       dbj 
    278       1.20       dbj #if defined(DIAGNOSTIC)
    279        1.1       dbj 	{
    280        1.1       dbj 		u_long state;
    281       1.31   mycroft 		state = nd_bsr4 (DD_CSR);
    282       1.20       dbj 
    283       1.20       dbj #if 1
    284       1.41     lukem 		/* mourning (a 25 MHz 68040 mono slab) appears to set BUSEXC
    285       1.41     lukem 		 * milo (a 25 MHz 68040 mono cube) didn't have this problem
    286       1.31   mycroft 		 * Darrin B. Jewell <jewell (at) mit.edu>  Mon May 25 07:53:05 1998
    287       1.31   mycroft 		 */
    288       1.31   mycroft 		state &= (DMACSR_COMPLETE | DMACSR_SUPDATE | DMACSR_ENABLE);
    289       1.20       dbj #else
    290       1.31   mycroft 		state &= (DMACSR_BUSEXC | DMACSR_COMPLETE |
    291       1.31   mycroft 			  DMACSR_SUPDATE | DMACSR_ENABLE);
    292       1.20       dbj #endif
    293        1.1       dbj 		if (state) {
    294       1.31   mycroft 			nextdma_print(nsc);
    295       1.20       dbj 			panic("DMA did not reset");
    296        1.1       dbj 		}
    297        1.1       dbj 	}
    298        1.1       dbj #endif
    299        1.1       dbj }
    300        1.1       dbj 
    301        1.1       dbj void
    302       1.37       chs nextdma_reset(struct nextdma_softc *nsc)
    303        1.1       dbj {
    304        1.1       dbj 	int s;
    305       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    306       1.31   mycroft 
    307       1.18       dbj 	s = spldma();
    308        1.8       dbj 
    309        1.8       dbj 	DPRINTF(("DMA reset\n"));
    310        1.8       dbj 
    311        1.8       dbj #if (defined(ND_DEBUG))
    312       1.31   mycroft 	if (NEXTDMA_DEBUG > 1) nextdma_print(nsc);
    313        1.8       dbj #endif
    314        1.8       dbj 
    315       1.31   mycroft 	nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
    316       1.31   mycroft 	if ((stat->nd_map) || (stat->nd_map_cont)) {
    317       1.31   mycroft 		if (stat->nd_map_cont) {
    318       1.30  christos 			DPRINTF(("DMA: resetting with non null continue map\n"));
    319       1.31   mycroft 			if (nsc->sc_conf.nd_completed_cb)
    320       1.31   mycroft 				(*nsc->sc_conf.nd_completed_cb)
    321       1.31   mycroft 					(stat->nd_map_cont, nsc->sc_conf.nd_cb_arg);
    322       1.30  christos 
    323       1.31   mycroft 			stat->nd_map_cont = 0;
    324       1.31   mycroft 			stat->nd_idx_cont = 0;
    325       1.30  christos 		}
    326       1.31   mycroft 		if (nsc->sc_conf.nd_shutdown_cb)
    327       1.31   mycroft 			(*nsc->sc_conf.nd_shutdown_cb)(nsc->sc_conf.nd_cb_arg);
    328       1.31   mycroft 		stat->nd_map = 0;
    329       1.31   mycroft 		stat->nd_idx = 0;
    330       1.26       dbj 	}
    331       1.20       dbj 
    332        1.1       dbj 	splx(s);
    333        1.1       dbj }
    334        1.1       dbj 
    335        1.1       dbj /****************************************************************/
    336        1.1       dbj 
    337        1.1       dbj 
    338        1.1       dbj /* Call the completed and continue callbacks to try to fill
    339        1.1       dbj  * in the dma continue buffers.
    340        1.1       dbj  */
    341        1.1       dbj void
    342       1.37       chs nextdma_rotate(struct nextdma_softc *nsc)
    343        1.1       dbj {
    344       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    345        1.1       dbj 
    346       1.49  christos 	NDTRACEIF (ndtrace_addc('r'));
    347       1.31   mycroft 	DPRINTF(("DMA nextdma_rotate()\n"));
    348        1.1       dbj 
    349        1.1       dbj 	/* Rotate the continue map into the current map */
    350       1.31   mycroft 	stat->nd_map = stat->nd_map_cont;
    351       1.31   mycroft 	stat->nd_idx = stat->nd_idx_cont;
    352        1.1       dbj 
    353       1.31   mycroft 	if ((!stat->nd_map_cont) ||
    354       1.31   mycroft 	    ((++stat->nd_idx_cont >= stat->nd_map_cont->dm_nsegs))) {
    355       1.31   mycroft 		if (nsc->sc_conf.nd_continue_cb) {
    356       1.31   mycroft 			stat->nd_map_cont = (*nsc->sc_conf.nd_continue_cb)
    357       1.31   mycroft 				(nsc->sc_conf.nd_cb_arg);
    358       1.31   mycroft 			if (stat->nd_map_cont) {
    359       1.31   mycroft 				stat->nd_map_cont->dm_xfer_len = 0;
    360       1.26       dbj 			}
    361        1.1       dbj 		} else {
    362       1.31   mycroft 			stat->nd_map_cont = 0;
    363        1.1       dbj 		}
    364       1.31   mycroft 		stat->nd_idx_cont = 0;
    365        1.1       dbj 	}
    366        1.7       dbj 
    367       1.29       dbj #if defined(DIAGNOSTIC) && 0
    368       1.31   mycroft 	if (stat->nd_map_cont) {
    369       1.31   mycroft 		if (!DMA_BEGINALIGNED(stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr)) {
    370       1.31   mycroft 			nextdma_print(nsc);
    371       1.32    provos 			panic("DMA request unaligned at start");
    372        1.7       dbj 		}
    373       1.31   mycroft 		if (!DMA_ENDALIGNED(stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
    374       1.31   mycroft 				stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len)) {
    375       1.31   mycroft 			nextdma_print(nsc);
    376       1.32    provos 			panic("DMA request unaligned at end");
    377        1.7       dbj 		}
    378        1.7       dbj 	}
    379        1.7       dbj #endif
    380        1.7       dbj 
    381        1.1       dbj }
    382        1.1       dbj 
    383        1.1       dbj void
    384       1.37       chs nextdma_setup_curr_regs(struct nextdma_softc *nsc)
    385        1.1       dbj {
    386       1.20       dbj 	bus_addr_t dd_next;
    387       1.20       dbj 	bus_addr_t dd_limit;
    388       1.20       dbj 	bus_addr_t dd_saved_next;
    389       1.20       dbj 	bus_addr_t dd_saved_limit;
    390       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    391       1.20       dbj 
    392       1.49  christos 	NDTRACEIF (ndtrace_addc('C'));
    393       1.31   mycroft 	DPRINTF(("DMA nextdma_setup_curr_regs()\n"));
    394        1.1       dbj 
    395       1.31   mycroft 	if (stat->nd_map) {
    396       1.31   mycroft 		dd_next = stat->nd_map->dm_segs[stat->nd_idx].ds_addr;
    397       1.31   mycroft 		dd_limit = (stat->nd_map->dm_segs[stat->nd_idx].ds_addr +
    398       1.31   mycroft 			    stat->nd_map->dm_segs[stat->nd_idx].ds_len);
    399       1.15       dbj 
    400       1.31   mycroft 		if (!turbo && nsc->sc_chan->nd_intr == NEXT_I_ENETX_DMA) {
    401       1.20       dbj 			dd_limit |= 0x80000000; /* Ethernet transmit needs secret magic */
    402       1.29       dbj 			dd_limit += 15;
    403       1.20       dbj 		}
    404       1.20       dbj 	} else {
    405       1.31   mycroft 		dd_next = turbo ? 0 : 0xdeadbeef;
    406       1.31   mycroft 		dd_limit = turbo ? 0 : 0xdeadbeef;
    407       1.20       dbj 	}
    408        1.1       dbj 
    409       1.20       dbj 	dd_saved_next = dd_next;
    410       1.20       dbj 	dd_saved_limit = dd_limit;
    411        1.1       dbj 
    412       1.31   mycroft 	NDTRACEIF (if (stat->nd_map) {
    413       1.49  christos 		ndtrace_printf("%ld", stat->nd_map->dm_segs[stat->nd_idx].ds_len);
    414       1.31   mycroft 	});
    415       1.30  christos 
    416       1.31   mycroft 	if (!turbo && (nsc->sc_chan->nd_intr == NEXT_I_ENETX_DMA)) {
    417       1.31   mycroft 		nd_bsw4 (DD_NEXT_INITBUF, dd_next);
    418       1.15       dbj 	} else {
    419       1.31   mycroft 		nd_bsw4 (DD_NEXT, dd_next);
    420       1.15       dbj 	}
    421       1.31   mycroft 	nd_bsw4 (DD_LIMIT, dd_limit);
    422       1.31   mycroft 	if (!turbo) nd_bsw4 (DD_SAVED_NEXT, dd_saved_next);
    423       1.31   mycroft 	if (!turbo) nd_bsw4 (DD_SAVED_LIMIT, dd_saved_limit);
    424        1.1       dbj 
    425       1.20       dbj #ifdef DIAGNOSTIC
    426       1.31   mycroft 	if ((nd_bsr4 (DD_NEXT_INITBUF) != dd_next)
    427       1.31   mycroft 	    || (nd_bsr4 (DD_NEXT) != dd_next)
    428       1.31   mycroft 	    || (nd_bsr4 (DD_LIMIT) != dd_limit)
    429       1.31   mycroft 	    || (!turbo && (nd_bsr4 (DD_SAVED_NEXT) != dd_saved_next))
    430       1.31   mycroft 	    || (!turbo && (nd_bsr4 (DD_SAVED_LIMIT) != dd_saved_limit))
    431       1.31   mycroft 		) {
    432       1.31   mycroft 		nextdma_print(nsc);
    433       1.20       dbj 		panic("DMA failure writing to current regs");
    434       1.20       dbj 	}
    435        1.7       dbj #endif
    436        1.1       dbj }
    437        1.1       dbj 
    438        1.1       dbj void
    439       1.37       chs nextdma_setup_cont_regs(struct nextdma_softc *nsc)
    440        1.1       dbj {
    441       1.31   mycroft 	bus_addr_t dd_start;
    442       1.31   mycroft 	bus_addr_t dd_stop;
    443       1.31   mycroft 	bus_addr_t dd_saved_start;
    444       1.31   mycroft 	bus_addr_t dd_saved_stop;
    445       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    446        1.1       dbj 
    447       1.49  christos 	NDTRACEIF (ndtrace_addc('c'));
    448       1.31   mycroft 	DPRINTF(("DMA nextdma_setup_regs()\n"));
    449        1.1       dbj 
    450       1.31   mycroft 	if (stat->nd_map_cont) {
    451       1.31   mycroft 		dd_start = stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr;
    452       1.31   mycroft 		dd_stop  = (stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
    453       1.31   mycroft 			    stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len);
    454       1.22        tv 
    455       1.31   mycroft 		if (!turbo && nsc->sc_chan->nd_intr == NEXT_I_ENETX_DMA) {
    456       1.31   mycroft 			dd_stop |= 0x80000000; /* Ethernet transmit needs secret magic */
    457       1.31   mycroft 			dd_stop += 15;
    458       1.24       dbj 		}
    459        1.1       dbj 	} else {
    460       1.31   mycroft 		dd_start = turbo ? nd_bsr4 (DD_NEXT) : 0xdeadbee0;
    461       1.31   mycroft 		dd_stop = turbo ? 0 : 0xdeadbee0;
    462        1.1       dbj 	}
    463        1.1       dbj 
    464       1.31   mycroft 	dd_saved_start = dd_start;
    465       1.31   mycroft 	dd_saved_stop  = dd_stop;
    466       1.22        tv 
    467       1.31   mycroft 	NDTRACEIF (if (stat->nd_map_cont) {
    468       1.49  christos 		ndtrace_printf("%ld", stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len);
    469       1.31   mycroft 	});
    470       1.31   mycroft 
    471       1.31   mycroft 	nd_bsw4 (DD_START, dd_start);
    472       1.31   mycroft 	nd_bsw4 (DD_STOP, dd_stop);
    473       1.31   mycroft 	if (!turbo) nd_bsw4 (DD_SAVED_START, dd_saved_start);
    474       1.31   mycroft 	if (!turbo) nd_bsw4 (DD_SAVED_STOP, dd_saved_stop);
    475       1.31   mycroft 	if (turbo && nsc->sc_chan->nd_intr == NEXT_I_ENETR_DMA)
    476       1.31   mycroft 		nd_bsw4 (DD_STOP - 0x40, dd_start);
    477       1.31   mycroft 
    478       1.31   mycroft #ifdef DIAGNOSTIC
    479       1.31   mycroft 	if ((nd_bsr4 (DD_START) != dd_start)
    480       1.31   mycroft 	    || (dd_stop && (nd_bsr4 (DD_STOP) != dd_stop))
    481       1.31   mycroft 	    || (!turbo && (nd_bsr4 (DD_SAVED_START) != dd_saved_start))
    482       1.31   mycroft 	    || (!turbo && (nd_bsr4 (DD_SAVED_STOP) != dd_saved_stop))
    483       1.31   mycroft 		) {
    484       1.31   mycroft 		nextdma_print(nsc);
    485       1.31   mycroft 		panic("DMA failure writing to continue regs");
    486       1.31   mycroft 	}
    487       1.31   mycroft #endif
    488        1.1       dbj }
    489        1.1       dbj 
    490        1.1       dbj /****************************************************************/
    491        1.1       dbj 
    492       1.31   mycroft #if NESP > 0
    493       1.31   mycroft static int
    494       1.37       chs nextdma_esp_intr(void *arg)
    495        1.1       dbj {
    496       1.31   mycroft 	/* @@@ This is bogus, we can't be certain of arg's type
    497       1.18       dbj 	 * unless the interrupt is for us.  For now we successfully
    498       1.18       dbj 	 * cheat because DMA interrupts are the only things invoked
    499       1.18       dbj 	 * at this interrupt level.
    500        1.1       dbj 	 */
    501       1.31   mycroft 	struct nextdma_softc *nsc = arg;
    502       1.37       chs 	int esp_dma_int(void *); /* XXX */
    503       1.31   mycroft 
    504       1.31   mycroft 	if (!INTR_OCCURRED(nsc->sc_chan->nd_intr))
    505       1.31   mycroft 		return 0;
    506       1.31   mycroft 	/* Handle dma interrupts */
    507        1.1       dbj 
    508       1.31   mycroft 	return esp_dma_int (nsc->sc_conf.nd_cb_arg);
    509        1.1       dbj 
    510       1.31   mycroft }
    511       1.30  christos #endif
    512       1.30  christos 
    513       1.31   mycroft #if NXE > 0
    514       1.31   mycroft static int
    515       1.37       chs nextdma_enet_intr(void *arg)
    516       1.31   mycroft {
    517       1.31   mycroft 	/* @@@ This is bogus, we can't be certain of arg's type
    518       1.31   mycroft 	 * unless the interrupt is for us.  For now we successfully
    519       1.31   mycroft 	 * cheat because DMA interrupts are the only things invoked
    520       1.31   mycroft 	 * at this interrupt level.
    521       1.31   mycroft 	 */
    522       1.31   mycroft 	struct nextdma_softc *nsc = arg;
    523       1.31   mycroft 	unsigned int state;
    524       1.31   mycroft 	bus_addr_t onext;
    525       1.31   mycroft 	bus_addr_t olimit;
    526       1.31   mycroft 	bus_addr_t slimit;
    527       1.31   mycroft 	int result;
    528       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    529       1.31   mycroft 
    530       1.31   mycroft 	if (!INTR_OCCURRED(nsc->sc_chan->nd_intr))
    531       1.31   mycroft 		return 0;
    532       1.31   mycroft 	/* Handle dma interrupts */
    533       1.31   mycroft 
    534       1.49  christos 	NDTRACEIF (ndtrace_addc('D'));
    535       1.22        tv #ifdef ND_DEBUG
    536       1.30  christos 	if (NEXTDMA_DEBUG) {
    537       1.22        tv 		char sbuf[256];
    538       1.22        tv 
    539       1.43  christos 		snprintb(sbuf, sizeof(sbuf), NEXT_INTR_BITS,
    540       1.47       mrg 		    NEXT_I_BIT(nsc->sc_chan->nd_intr));
    541  1.49.10.1  pgoyette 		printf("DMA interrupt ipl (%ld) intr(%s)\n",
    542       1.31   mycroft 		       NEXT_I_IPL(nsc->sc_chan->nd_intr), sbuf);
    543       1.22        tv 	}
    544       1.22        tv #endif
    545        1.1       dbj 
    546        1.7       dbj #ifdef DIAGNOSTIC
    547       1.31   mycroft 	if (!stat->nd_map) {
    548       1.31   mycroft 		nextdma_print(nsc);
    549       1.32    provos 		panic("DMA missing current map in interrupt!");
    550        1.7       dbj 	}
    551        1.7       dbj #endif
    552        1.7       dbj 
    553       1.31   mycroft 	state = nd_bsr4 (DD_CSR);
    554       1.26       dbj 
    555       1.26       dbj #if defined(ND_DEBUG)
    556       1.31   mycroft 	nextdma_debug_savestate(nsc, state);
    557       1.26       dbj #endif
    558       1.26       dbj 
    559       1.26       dbj #ifdef DIAGNOSTIC
    560       1.31   mycroft 	if (/* (state & DMACSR_READ) || */ !(state & DMACSR_COMPLETE)) {
    561       1.31   mycroft 		char sbuf[256];
    562       1.31   mycroft 		nextdma_print(nsc);
    563       1.43  christos 		snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, state);
    564  1.49.10.1  pgoyette 		printf("DMA: state %s\n",sbuf);
    565       1.32    provos 		panic("DMA complete not set in interrupt");
    566       1.31   mycroft 	}
    567       1.26       dbj #endif
    568       1.26       dbj 
    569       1.31   mycroft 	DPRINTF(("DMA: finishing xfer\n"));
    570       1.23       dbj 
    571       1.31   mycroft 	onext = stat->nd_map->dm_segs[stat->nd_idx].ds_addr;
    572       1.31   mycroft 	olimit = onext + stat->nd_map->dm_segs[stat->nd_idx].ds_len;
    573       1.23       dbj 
    574       1.31   mycroft 	result = 0;
    575       1.31   mycroft 	if (state & DMACSR_ENABLE) {
    576       1.31   mycroft 		/* enable bit was set */
    577       1.31   mycroft 		result |= 0x01;
    578       1.31   mycroft 	}
    579       1.31   mycroft 	if (state & DMACSR_SUPDATE) {
    580       1.31   mycroft 		/* supdate bit was set */
    581       1.31   mycroft 		result |= 0x02;
    582       1.31   mycroft 	}
    583       1.31   mycroft 	if (stat->nd_map_cont == NULL) {
    584       1.31   mycroft 		KASSERT(stat->nd_idx+1 == stat->nd_map->dm_nsegs);
    585       1.31   mycroft 		/* Expecting a shutdown, didn't SETSUPDATE last turn */
    586       1.31   mycroft 		result |= 0x04;
    587       1.31   mycroft 	}
    588       1.31   mycroft 	if (state & DMACSR_BUSEXC) {
    589       1.31   mycroft 		/* bus exception bit was set */
    590       1.31   mycroft 		result |= 0x08;
    591       1.31   mycroft 	}
    592       1.31   mycroft 	switch (result) {
    593       1.31   mycroft 	case 0x00: /* !BUSEXC && !expecting && !SUPDATE && !ENABLE */
    594       1.31   mycroft 	case 0x08: /* BUSEXC && !expecting && !SUPDATE && !ENABLE */
    595       1.31   mycroft 		if (turbo) {
    596       1.31   mycroft 			volatile u_int *limit = (volatile u_int *)IIOV(0x2000050+0x4000);
    597       1.31   mycroft 			slimit = *limit;
    598       1.31   mycroft 		} else {
    599       1.31   mycroft 			slimit = nd_bsr4 (DD_SAVED_LIMIT);
    600       1.31   mycroft 		}
    601       1.31   mycroft 		break;
    602       1.31   mycroft 	case 0x01: /* !BUSEXC && !expecting && !SUPDATE && ENABLE */
    603       1.31   mycroft 	case 0x09: /* BUSEXC && !expecting && !SUPDATE && ENABLE */
    604       1.31   mycroft 		if (turbo) {
    605       1.31   mycroft 			volatile u_int *limit = (volatile u_int *)IIOV(0x2000050+0x4000);
    606       1.31   mycroft 			slimit = *limit;
    607       1.31   mycroft 		} else {
    608       1.31   mycroft 			slimit = nd_bsr4 (DD_SAVED_LIMIT);
    609       1.31   mycroft 		}
    610       1.31   mycroft 		break;
    611       1.31   mycroft 	case 0x02: /* !BUSEXC && !expecting && SUPDATE && !ENABLE */
    612       1.31   mycroft 	case 0x0a: /* BUSEXC && !expecting && SUPDATE && !ENABLE */
    613       1.31   mycroft 		slimit = nd_bsr4 (DD_NEXT);
    614       1.31   mycroft 		break;
    615       1.31   mycroft 	case 0x04:  /* !BUSEXC && expecting && !SUPDATE && !ENABLE */
    616       1.31   mycroft 	case 0x0c: /* BUSEXC && expecting && !SUPDATE && !ENABLE */
    617       1.31   mycroft 		slimit = nd_bsr4 (DD_LIMIT);
    618       1.31   mycroft 		break;
    619       1.31   mycroft 	default:
    620       1.31   mycroft #ifdef DIAGNOSTIC
    621       1.31   mycroft 	{
    622       1.31   mycroft 		char sbuf[256];
    623       1.36    keihan 		printf("DMA: please send this output to port-next68k-maintainer (at) NetBSD.org:\n");
    624       1.43  christos 		snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, state);
    625  1.49.10.1  pgoyette 		printf("DMA: state %s\n",sbuf);
    626       1.31   mycroft 		nextdma_print(nsc);
    627       1.32    provos 		panic("DMA: condition 0x%02x not yet documented to occur",result);
    628       1.31   mycroft 	}
    629       1.31   mycroft #endif
    630       1.31   mycroft 	slimit = olimit;
    631       1.31   mycroft 	break;
    632       1.31   mycroft 	}
    633       1.22        tv 
    634       1.31   mycroft 	if (!turbo && nsc->sc_chan->nd_intr == NEXT_I_ENETX_DMA) {
    635       1.31   mycroft 		slimit &= ~0x80000000;
    636       1.31   mycroft 		slimit -= 15;
    637       1.31   mycroft 	}
    638       1.22        tv 
    639       1.23       dbj #ifdef DIAGNOSTIC
    640       1.31   mycroft 	if ((state & DMACSR_READ))
    641       1.31   mycroft 		DPRINTF (("limits: 0x%08lx <= 0x%08lx <= 0x%08lx %s\n", onext, slimit, olimit,
    642       1.31   mycroft 			  (state & DMACSR_READ) ? "read" : "write"));
    643       1.31   mycroft 	if ((slimit < onext) || (slimit > olimit)) {
    644       1.31   mycroft 		char sbuf[256];
    645       1.43  christos 		snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, state);
    646  1.49.10.1  pgoyette 		printf("DMA: state %s\n",sbuf);
    647       1.31   mycroft 		nextdma_print(nsc);
    648       1.32    provos 		panic("DMA: Unexpected limit register (0x%08lx) in finish_xfer",slimit);
    649       1.31   mycroft 	}
    650        1.1       dbj #endif
    651        1.1       dbj 
    652       1.26       dbj #ifdef DIAGNOSTIC
    653       1.31   mycroft 	if ((state & DMACSR_ENABLE) && ((stat->nd_idx+1) != stat->nd_map->dm_nsegs)) {
    654       1.31   mycroft 		if (slimit != olimit) {
    655       1.31   mycroft 			char sbuf[256];
    656       1.43  christos 			snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, state);
    657  1.49.10.1  pgoyette 			printf("DMA: state %s\n",sbuf);
    658       1.31   mycroft 			nextdma_print(nsc);
    659       1.32    provos 			panic("DMA: short limit register (0x%08lx) w/o finishing map.",slimit);
    660       1.31   mycroft 		}
    661       1.31   mycroft 	}
    662       1.26       dbj #endif
    663       1.26       dbj 
    664       1.23       dbj #if (defined(ND_DEBUG))
    665       1.31   mycroft 	if (NEXTDMA_DEBUG > 2) nextdma_print(nsc);
    666       1.23       dbj #endif
    667        1.7       dbj 
    668       1.31   mycroft 	stat->nd_map->dm_xfer_len += slimit-onext;
    669       1.31   mycroft 
    670       1.31   mycroft 	/* If we've reached the end of the current map, then inform
    671       1.31   mycroft 	 * that we've completed that map.
    672       1.31   mycroft 	 */
    673       1.31   mycroft 	if ((stat->nd_idx+1) == stat->nd_map->dm_nsegs) {
    674       1.31   mycroft 		if (nsc->sc_conf.nd_completed_cb)
    675       1.31   mycroft 			(*nsc->sc_conf.nd_completed_cb)
    676       1.31   mycroft 				(stat->nd_map, nsc->sc_conf.nd_cb_arg);
    677       1.31   mycroft 	} else {
    678       1.31   mycroft 		KASSERT(stat->nd_map == stat->nd_map_cont);
    679       1.31   mycroft 		KASSERT(stat->nd_idx+1 == stat->nd_idx_cont);
    680       1.31   mycroft 	}
    681       1.31   mycroft 	stat->nd_map = 0;
    682       1.31   mycroft 	stat->nd_idx = 0;
    683       1.12       dbj 
    684       1.31   mycroft #if (defined(ND_DEBUG))
    685       1.31   mycroft 	if (NEXTDMA_DEBUG) {
    686       1.31   mycroft 		char sbuf[256];
    687       1.43  christos 		snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, state);
    688  1.49.10.1  pgoyette 		printf("CLNDMAP: dd->dd_csr          = %s\n",   sbuf);
    689       1.31   mycroft 	}
    690       1.31   mycroft #endif
    691       1.31   mycroft 	if (state & DMACSR_ENABLE) {
    692       1.31   mycroft 		u_long dmadir;		/* DMACSR_SETREAD or DMACSR_SETWRITE */
    693       1.23       dbj 
    694       1.31   mycroft 		nextdma_rotate(nsc);
    695       1.31   mycroft 		nextdma_setup_cont_regs(nsc);
    696       1.31   mycroft 
    697       1.31   mycroft 		if (state & DMACSR_READ) {
    698       1.31   mycroft 			dmadir = DMACSR_SETREAD;
    699       1.31   mycroft 		} else {
    700       1.31   mycroft 			dmadir = DMACSR_SETWRITE;
    701       1.30  christos 		}
    702       1.22        tv 
    703       1.31   mycroft 		if (stat->nd_map_cont == NULL) {
    704       1.31   mycroft 			KASSERT(stat->nd_idx+1 == stat->nd_map->dm_nsegs);
    705       1.31   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | dmadir);
    706       1.49  christos 			NDTRACEIF (ndtrace_addc('g'));
    707       1.23       dbj 		} else {
    708       1.31   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | dmadir | DMACSR_SETSUPDATE);
    709       1.49  christos 			NDTRACEIF (ndtrace_addc('G'));
    710       1.31   mycroft 		}
    711       1.31   mycroft 	} else {
    712       1.31   mycroft 		DPRINTF(("DMA: a shutdown occurred\n"));
    713       1.31   mycroft 		nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
    714       1.31   mycroft 
    715       1.31   mycroft 		/* Cleanup more incomplete transfers */
    716       1.31   mycroft 		/* cleanup continue map */
    717       1.31   mycroft 		if (stat->nd_map_cont) {
    718       1.31   mycroft 			DPRINTF(("DMA: shutting down with non null continue map\n"));
    719       1.31   mycroft 			if (nsc->sc_conf.nd_completed_cb)
    720       1.31   mycroft 				(*nsc->sc_conf.nd_completed_cb)
    721       1.31   mycroft 					(stat->nd_map_cont, nsc->sc_conf.nd_cb_arg);
    722       1.23       dbj 
    723       1.31   mycroft 			stat->nd_map_cont = 0;
    724       1.31   mycroft 			stat->nd_idx_cont = 0;
    725        1.1       dbj 		}
    726       1.31   mycroft 		if (nsc->sc_conf.nd_shutdown_cb)
    727       1.31   mycroft 			(*nsc->sc_conf.nd_shutdown_cb)(nsc->sc_conf.nd_cb_arg);
    728        1.1       dbj 	}
    729       1.30  christos 
    730       1.22        tv #ifdef ND_DEBUG
    731       1.30  christos 	if (NEXTDMA_DEBUG) {
    732       1.22        tv 		char sbuf[256];
    733       1.22        tv 
    734       1.43  christos 		snprintb(sbuf, sizeof(sbuf),
    735       1.43  christos 		    NEXT_INTR_BITS, NEXT_I_BIT(nsc->sc_chan->nd_intr));
    736  1.49.10.1  pgoyette 		printf("DMA exiting interrupt ipl (%ld) intr(%s)\n",
    737       1.31   mycroft 		       NEXT_I_IPL(nsc->sc_chan->nd_intr), sbuf);
    738       1.22        tv 	}
    739       1.22        tv #endif
    740       1.31   mycroft 
    741       1.31   mycroft 	return(1);
    742        1.1       dbj }
    743       1.31   mycroft #endif
    744        1.1       dbj 
    745        1.1       dbj /*
    746        1.1       dbj  * Check to see if dma has finished for a channel */
    747        1.1       dbj int
    748       1.37       chs nextdma_finished(struct nextdma_softc *nsc)
    749        1.1       dbj {
    750        1.1       dbj 	int r;
    751        1.1       dbj 	int s;
    752       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    753       1.31   mycroft 
    754       1.31   mycroft 	s = spldma();
    755       1.31   mycroft 	r = (stat->nd_map == NULL) && (stat->nd_map_cont == NULL);
    756        1.1       dbj 	splx(s);
    757       1.31   mycroft 
    758        1.1       dbj 	return(r);
    759        1.1       dbj }
    760        1.1       dbj 
    761        1.1       dbj void
    762       1.37       chs nextdma_start(struct nextdma_softc *nsc, u_long dmadir)
    763        1.1       dbj {
    764       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    765        1.1       dbj 
    766       1.49  christos 	NDTRACEIF (ndtrace_addc('n'));
    767        1.1       dbj #ifdef DIAGNOSTIC
    768       1.31   mycroft 	if (!nextdma_finished(nsc)) {
    769       1.22        tv 		char sbuf[256];
    770       1.22        tv 
    771       1.43  christos 		snprintb(sbuf, sizeof(sbuf),
    772       1.43  christos 		    NEXT_INTR_BITS, NEXT_I_BIT(nsc->sc_chan->nd_intr));
    773  1.49.10.1  pgoyette 		panic("DMA trying to start before previous finished on intr(%s)", sbuf);
    774        1.1       dbj 	}
    775        1.1       dbj #endif
    776        1.1       dbj 
    777       1.22        tv #ifdef ND_DEBUG
    778       1.30  christos 	if (NEXTDMA_DEBUG) {
    779       1.22        tv 		char sbuf[256];
    780       1.22        tv 
    781       1.43  christos 		snprintb(sbuf, sizeof(sbuf),
    782       1.43  christos 		    NEXT_INTR_BITS, NEXT_I_BIT(nsc->sc_chan->nd_intr));
    783  1.49.10.1  pgoyette 		printf("DMA start (%ld) intr(%s)\n",
    784       1.31   mycroft 		       NEXT_I_IPL(nsc->sc_chan->nd_intr), sbuf);
    785       1.22        tv 	}
    786       1.22        tv #endif
    787        1.1       dbj 
    788        1.1       dbj #ifdef DIAGNOSTIC
    789       1.31   mycroft 	if (stat->nd_map) {
    790       1.31   mycroft 		nextdma_print(nsc);
    791       1.32    provos 		panic("DMA: nextdma_start() with non null map");
    792        1.1       dbj 	}
    793       1.31   mycroft 	if (stat->nd_map_cont) {
    794       1.31   mycroft 		nextdma_print(nsc);
    795       1.32    provos 		panic("DMA: nextdma_start() with non null continue map");
    796        1.1       dbj 	}
    797        1.1       dbj #endif
    798        1.1       dbj 
    799        1.9       dbj #ifdef DIAGNOSTIC
    800       1.19       dbj 	if ((dmadir != DMACSR_SETREAD) && (dmadir != DMACSR_SETWRITE)) {
    801       1.32    provos 		panic("DMA: nextdma_start(), dmadir arg must be DMACSR_SETREAD or DMACSR_SETWRITE");
    802        1.9       dbj 	}
    803        1.9       dbj #endif
    804        1.9       dbj 
    805       1.26       dbj #if defined(ND_DEBUG)
    806       1.31   mycroft 	nextdma_debug_initstate(nsc);
    807       1.26       dbj #endif
    808       1.26       dbj 
    809        1.7       dbj 	/* preload both the current and the continue maps */
    810       1.31   mycroft 	nextdma_rotate(nsc);
    811        1.1       dbj 
    812        1.1       dbj #ifdef DIAGNOSTIC
    813       1.31   mycroft 	if (!stat->nd_map_cont) {
    814        1.1       dbj 		panic("No map available in nextdma_start()");
    815        1.1       dbj 	}
    816        1.1       dbj #endif
    817        1.1       dbj 
    818       1.31   mycroft 	nextdma_rotate(nsc);
    819        1.7       dbj 
    820       1.22        tv #ifdef ND_DEBUG
    821       1.30  christos 	if (NEXTDMA_DEBUG) {
    822       1.22        tv 		char sbuf[256];
    823       1.22        tv 
    824       1.43  christos 		snprintb(sbuf, sizeof(sbuf),
    825       1.43  christos 		    NEXT_INTR_BITS, NEXT_I_BIT(nsc->sc_chan->nd_intr));
    826  1.49.10.1  pgoyette 		printf("DMA initiating DMA %s of %d segments on intr(%s)\n",
    827       1.31   mycroft 		       (dmadir == DMACSR_SETREAD ? "read" : "write"), stat->nd_map->dm_nsegs, sbuf);
    828       1.22        tv 	}
    829       1.22        tv #endif
    830        1.1       dbj 
    831       1.31   mycroft 	nd_bsw4 (DD_CSR, (turbo ? DMACSR_INITBUFTURBO : DMACSR_INITBUF) |
    832       1.31   mycroft 		 DMACSR_RESET | dmadir);
    833       1.31   mycroft 	nd_bsw4 (DD_CSR, 0);
    834        1.1       dbj 
    835       1.31   mycroft 	nextdma_setup_curr_regs(nsc);
    836       1.31   mycroft 	nextdma_setup_cont_regs(nsc);
    837        1.1       dbj 
    838        1.4       dbj #if (defined(ND_DEBUG))
    839       1.31   mycroft 	if (NEXTDMA_DEBUG > 2) nextdma_print(nsc);
    840        1.4       dbj #endif
    841        1.1       dbj 
    842       1.31   mycroft 	if (stat->nd_map_cont == NULL) {
    843       1.31   mycroft 		nd_bsw4 (DD_CSR, DMACSR_SETENABLE | dmadir);
    844       1.20       dbj 	} else {
    845       1.31   mycroft 		nd_bsw4 (DD_CSR, DMACSR_SETSUPDATE | DMACSR_SETENABLE | dmadir);
    846        1.1       dbj 	}
    847        1.1       dbj }
    848       1.31   mycroft 
    849       1.31   mycroft /* This routine is used for debugging */
    850       1.31   mycroft void
    851       1.37       chs nextdma_print(struct nextdma_softc *nsc)
    852       1.31   mycroft {
    853       1.31   mycroft 	u_long dd_csr;
    854       1.31   mycroft 	u_long dd_next;
    855       1.31   mycroft 	u_long dd_next_initbuf;
    856       1.31   mycroft 	u_long dd_limit;
    857       1.31   mycroft 	u_long dd_start;
    858       1.31   mycroft 	u_long dd_stop;
    859       1.31   mycroft 	u_long dd_saved_next;
    860       1.31   mycroft 	u_long dd_saved_limit;
    861       1.31   mycroft 	u_long dd_saved_start;
    862       1.31   mycroft 	u_long dd_saved_stop;
    863       1.31   mycroft 	char sbuf[256];
    864       1.31   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    865       1.31   mycroft 
    866       1.31   mycroft 	/* Read all of the registers before we print anything out,
    867       1.31   mycroft 	 * in case something changes
    868       1.31   mycroft 	 */
    869       1.31   mycroft 	dd_csr          = nd_bsr4 (DD_CSR);
    870       1.31   mycroft 	dd_next         = nd_bsr4 (DD_NEXT);
    871       1.31   mycroft 	dd_next_initbuf = nd_bsr4 (DD_NEXT_INITBUF);
    872       1.31   mycroft 	dd_limit        = nd_bsr4 (DD_LIMIT);
    873       1.31   mycroft 	dd_start        = nd_bsr4 (DD_START);
    874       1.31   mycroft 	dd_stop         = nd_bsr4 (DD_STOP);
    875       1.31   mycroft 	dd_saved_next   = nd_bsr4 (DD_SAVED_NEXT);
    876       1.31   mycroft 	dd_saved_limit  = nd_bsr4 (DD_SAVED_LIMIT);
    877       1.31   mycroft 	dd_saved_start  = nd_bsr4 (DD_SAVED_START);
    878       1.31   mycroft 	dd_saved_stop   = nd_bsr4 (DD_SAVED_STOP);
    879       1.31   mycroft 
    880       1.43  christos 	snprintb(sbuf, sizeof(sbuf), NEXT_INTR_BITS,
    881       1.43  christos 	    *(volatile u_long *)IIOV(NEXT_P_INTRSTAT));
    882  1.49.10.1  pgoyette 	printf("NDMAP: *intrstat = %s\n", sbuf);
    883       1.31   mycroft 
    884       1.43  christos 	snprintb(sbuf, sizeof(sbuf), NEXT_INTR_BITS,
    885       1.43  christos 	    *(volatile u_long *)IIOV(NEXT_P_INTRMASK));
    886  1.49.10.1  pgoyette 	printf("NDMAP: *intrmask = %s\n", sbuf);
    887       1.31   mycroft 
    888       1.31   mycroft 	/* NDMAP is Next DMA Print (really!) */
    889       1.31   mycroft 
    890       1.31   mycroft 	if (stat->nd_map) {
    891       1.31   mycroft 		int i;
    892       1.31   mycroft 
    893       1.31   mycroft 		printf("NDMAP: nd_map->dm_mapsize = %ld\n",
    894       1.31   mycroft 		       stat->nd_map->dm_mapsize);
    895       1.31   mycroft 		printf("NDMAP: nd_map->dm_nsegs = %d\n",
    896       1.31   mycroft 		       stat->nd_map->dm_nsegs);
    897       1.31   mycroft 		printf("NDMAP: nd_map->dm_xfer_len = %ld\n",
    898       1.31   mycroft 		       stat->nd_map->dm_xfer_len);
    899       1.31   mycroft 		printf("NDMAP: nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    900       1.31   mycroft 		       stat->nd_idx, stat->nd_map->dm_segs[stat->nd_idx].ds_addr);
    901       1.31   mycroft 		printf("NDMAP: nd_map->dm_segs[%d].ds_len = %ld\n",
    902       1.31   mycroft 		       stat->nd_idx, stat->nd_map->dm_segs[stat->nd_idx].ds_len);
    903       1.31   mycroft 
    904       1.31   mycroft 		printf("NDMAP: Entire map;\n");
    905       1.31   mycroft 		for(i=0;i<stat->nd_map->dm_nsegs;i++) {
    906       1.31   mycroft 			printf("NDMAP:   nd_map->dm_segs[%d].ds_addr = 0x%08lx\n",
    907       1.31   mycroft 			       i,stat->nd_map->dm_segs[i].ds_addr);
    908       1.31   mycroft 			printf("NDMAP:   nd_map->dm_segs[%d].ds_len = %ld\n",
    909       1.31   mycroft 			       i,stat->nd_map->dm_segs[i].ds_len);
    910       1.31   mycroft 		}
    911       1.31   mycroft 	} else {
    912       1.31   mycroft 		printf("NDMAP: nd_map = NULL\n");
    913       1.31   mycroft 	}
    914       1.31   mycroft 	if (stat->nd_map_cont) {
    915       1.31   mycroft 		printf("NDMAP: nd_map_cont->dm_mapsize = %ld\n",
    916       1.31   mycroft 		       stat->nd_map_cont->dm_mapsize);
    917       1.31   mycroft 		printf("NDMAP: nd_map_cont->dm_nsegs = %d\n",
    918       1.31   mycroft 		       stat->nd_map_cont->dm_nsegs);
    919       1.31   mycroft 		printf("NDMAP: nd_map_cont->dm_xfer_len = %ld\n",
    920       1.31   mycroft 		       stat->nd_map_cont->dm_xfer_len);
    921       1.31   mycroft 		printf("NDMAP: nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    922       1.31   mycroft 		       stat->nd_idx_cont,stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr);
    923       1.31   mycroft 		printf("NDMAP: nd_map_cont->dm_segs[%d].ds_len = %ld\n",
    924       1.31   mycroft 		       stat->nd_idx_cont,stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len);
    925       1.31   mycroft 		if (stat->nd_map_cont != stat->nd_map) {
    926       1.31   mycroft 			int i;
    927       1.31   mycroft 			printf("NDMAP: Entire map;\n");
    928       1.31   mycroft 			for(i=0;i<stat->nd_map_cont->dm_nsegs;i++) {
    929       1.31   mycroft 				printf("NDMAP:   nd_map_cont->dm_segs[%d].ds_addr = 0x%08lx\n",
    930       1.31   mycroft 				       i,stat->nd_map_cont->dm_segs[i].ds_addr);
    931       1.31   mycroft 				printf("NDMAP:   nd_map_cont->dm_segs[%d].ds_len = %ld\n",
    932       1.31   mycroft 				       i,stat->nd_map_cont->dm_segs[i].ds_len);
    933       1.31   mycroft 			}
    934       1.31   mycroft 		}
    935       1.31   mycroft 	} else {
    936       1.31   mycroft 		printf("NDMAP: nd_map_cont = NULL\n");
    937       1.31   mycroft 	}
    938       1.31   mycroft 
    939       1.43  christos 	snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, dd_csr);
    940  1.49.10.1  pgoyette 	printf("NDMAP: dd->dd_csr          = %s\n",   sbuf);
    941       1.31   mycroft 
    942       1.31   mycroft 	printf("NDMAP: dd->dd_saved_next   = 0x%08lx\n", dd_saved_next);
    943       1.31   mycroft 	printf("NDMAP: dd->dd_saved_limit  = 0x%08lx\n", dd_saved_limit);
    944       1.31   mycroft 	printf("NDMAP: dd->dd_saved_start  = 0x%08lx\n", dd_saved_start);
    945       1.31   mycroft 	printf("NDMAP: dd->dd_saved_stop   = 0x%08lx\n", dd_saved_stop);
    946       1.31   mycroft 	printf("NDMAP: dd->dd_next         = 0x%08lx\n", dd_next);
    947       1.31   mycroft 	printf("NDMAP: dd->dd_next_initbuf = 0x%08lx\n", dd_next_initbuf);
    948       1.31   mycroft 	printf("NDMAP: dd->dd_limit        = 0x%08lx\n", dd_limit);
    949       1.31   mycroft 	printf("NDMAP: dd->dd_start        = 0x%08lx\n", dd_start);
    950       1.31   mycroft 	printf("NDMAP: dd->dd_stop         = 0x%08lx\n", dd_stop);
    951       1.31   mycroft 
    952       1.43  christos 	snprintb(sbuf, sizeof(sbuf), NEXT_INTR_BITS,
    953       1.47       mrg 	    NEXT_I_BIT(nsc->sc_chan->nd_intr));
    954  1.49.10.1  pgoyette 	printf("NDMAP: interrupt ipl (%ld) intr(%s)\n",
    955       1.31   mycroft 			NEXT_I_IPL(nsc->sc_chan->nd_intr), sbuf);
    956       1.31   mycroft }
    957       1.31   mycroft 
    958       1.31   mycroft #if defined(ND_DEBUG)
    959       1.31   mycroft void
    960       1.31   mycroft nextdma_debug_initstate(struct nextdma_softc *nsc)
    961       1.31   mycroft {
    962       1.31   mycroft 	switch(nsc->sc_chan->nd_intr) {
    963       1.31   mycroft 	case NEXT_I_ENETR_DMA:
    964       1.31   mycroft 		memset(nextdma_debug_enetr_state,0,sizeof(nextdma_debug_enetr_state));
    965       1.31   mycroft 		break;
    966       1.31   mycroft 	case NEXT_I_SCSI_DMA:
    967       1.31   mycroft 		memset(nextdma_debug_scsi_state,0,sizeof(nextdma_debug_scsi_state));
    968       1.31   mycroft 		break;
    969       1.31   mycroft 	}
    970       1.31   mycroft }
    971       1.31   mycroft 
    972       1.31   mycroft void
    973       1.31   mycroft nextdma_debug_savestate(struct nextdma_softc *nsc, unsigned int state)
    974       1.31   mycroft {
    975       1.31   mycroft 	switch(nsc->sc_chan->nd_intr) {
    976       1.31   mycroft 	case NEXT_I_ENETR_DMA:
    977       1.31   mycroft 		nextdma_debug_enetr_state[nextdma_debug_enetr_idx++] = state;
    978       1.31   mycroft 		nextdma_debug_enetr_idx %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
    979       1.31   mycroft 		break;
    980       1.31   mycroft 	case NEXT_I_SCSI_DMA:
    981       1.31   mycroft 		nextdma_debug_scsi_state[nextdma_debug_scsi_idx++] = state;
    982       1.31   mycroft 		nextdma_debug_scsi_idx %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
    983       1.31   mycroft 		break;
    984       1.31   mycroft 	}
    985       1.31   mycroft }
    986       1.31   mycroft 
    987       1.31   mycroft void
    988       1.31   mycroft nextdma_debug_enetr_dumpstate(void)
    989       1.31   mycroft {
    990       1.31   mycroft 	int i;
    991       1.31   mycroft 	int s;
    992       1.31   mycroft 	s = spldma();
    993       1.31   mycroft 	i = nextdma_debug_enetr_idx;
    994       1.31   mycroft 	do {
    995       1.31   mycroft 		char sbuf[256];
    996       1.31   mycroft 		if (nextdma_debug_enetr_state[i]) {
    997       1.43  christos 			snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, nextdma_debug_enetr_state[i]);
    998  1.49.10.1  pgoyette 			printf("DMA: 0x%02x state %s\n",i,sbuf);
    999       1.31   mycroft 		}
   1000       1.31   mycroft 		i++;
   1001       1.31   mycroft 		i %= (sizeof(nextdma_debug_enetr_state)/sizeof(unsigned int));
   1002       1.31   mycroft 	} while (i != nextdma_debug_enetr_idx);
   1003       1.31   mycroft 	splx(s);
   1004       1.31   mycroft }
   1005       1.31   mycroft 
   1006       1.31   mycroft void
   1007       1.31   mycroft nextdma_debug_scsi_dumpstate(void)
   1008       1.31   mycroft {
   1009       1.31   mycroft 	int i;
   1010       1.31   mycroft 	int s;
   1011       1.31   mycroft 	s = spldma();
   1012       1.31   mycroft 	i = nextdma_debug_scsi_idx;
   1013       1.31   mycroft 	do {
   1014       1.31   mycroft 		char sbuf[256];
   1015       1.31   mycroft 		if (nextdma_debug_scsi_state[i]) {
   1016       1.43  christos 			snprintb(sbuf, sizeof(sbuf), DMACSR_BITS, nextdma_debug_scsi_state[i]);
   1017  1.49.10.1  pgoyette 			printf("DMA: 0x%02x state %s\n",i,sbuf);
   1018       1.31   mycroft 		}
   1019       1.31   mycroft 		i++;
   1020       1.31   mycroft 		i %= (sizeof(nextdma_debug_scsi_state)/sizeof(unsigned int));
   1021       1.31   mycroft 	} while (i != nextdma_debug_scsi_idx);
   1022       1.31   mycroft 	splx(s);
   1023       1.31   mycroft }
   1024       1.31   mycroft #endif
   1025       1.31   mycroft 
   1026