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