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