Home | History | Annotate | Line # | Download | only in sbus
esp_sbus.c revision 1.45
      1 /*	$NetBSD: esp_sbus.c,v 1.45 2008/04/23 13:37:19 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: esp_sbus.c,v 1.45 2008/04/23 13:37:19 tsutsui Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/buf.h>
     47 #include <sys/malloc.h>
     48 
     49 #include <dev/scsipi/scsi_all.h>
     50 #include <dev/scsipi/scsipi_all.h>
     51 #include <dev/scsipi/scsiconf.h>
     52 #include <dev/scsipi/scsi_message.h>
     53 
     54 #include <sys/bus.h>
     55 #include <sys/intr.h>
     56 #include <machine/autoconf.h>
     57 
     58 #include <dev/ic/lsi64854reg.h>
     59 #include <dev/ic/lsi64854var.h>
     60 
     61 #include <dev/ic/ncr53c9xreg.h>
     62 #include <dev/ic/ncr53c9xvar.h>
     63 
     64 #include <dev/sbus/sbusvar.h>
     65 
     66 #include "opt_ddb.h"
     67 
     68 /* #define ESP_SBUS_DEBUG */
     69 
     70 struct esp_softc {
     71 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
     72 	struct sbusdev	sc_sd;			/* sbus device */
     73 
     74 	bus_space_tag_t	sc_bustag;
     75 	bus_dma_tag_t	sc_dmatag;
     76 
     77 	bus_space_handle_t sc_reg;		/* the registers */
     78 	struct lsi64854_softc *sc_dma;		/* pointer to my dma */
     79 
     80 	int	sc_pri;				/* SBUS priority */
     81 };
     82 
     83 int	espmatch_sbus(device_t, cfdata_t, void *);
     84 void	espattach_sbus(device_t, device_t, void *);
     85 void	espattach_dma(device_t, device_t, void *);
     86 
     87 static void	espattach(struct esp_softc *, struct ncr53c9x_glue *);
     88 
     89 CFATTACH_DECL_NEW(esp_sbus, sizeof(struct esp_softc),
     90     espmatch_sbus, espattach_sbus, NULL, NULL);
     91 
     92 CFATTACH_DECL_NEW(esp_dma, sizeof(struct esp_softc),
     93     espmatch_sbus, espattach_dma, NULL, NULL);
     94 
     95 /*
     96  * Functions and the switch for the MI code.
     97  */
     98 static uint8_t	esp_read_reg(struct ncr53c9x_softc *, int);
     99 static void	esp_write_reg(struct ncr53c9x_softc *, int, uint8_t);
    100 static uint8_t	esp_rdreg1(struct ncr53c9x_softc *, int);
    101 static void	esp_wrreg1(struct ncr53c9x_softc *, int, uint8_t);
    102 static int	esp_dma_isintr(struct ncr53c9x_softc *);
    103 static void	esp_dma_reset(struct ncr53c9x_softc *);
    104 static int	esp_dma_intr(struct ncr53c9x_softc *);
    105 static int	esp_dma_setup(struct ncr53c9x_softc *, uint8_t **,
    106 				    size_t *, int, size_t *);
    107 static void	esp_dma_go(struct ncr53c9x_softc *);
    108 static void	esp_dma_stop(struct ncr53c9x_softc *);
    109 static int	esp_dma_isactive(struct ncr53c9x_softc *);
    110 
    111 #ifdef DDB
    112 static void	esp_init_ddb_cmds(void);
    113 #endif
    114 
    115 static struct ncr53c9x_glue esp_sbus_glue = {
    116 	esp_read_reg,
    117 	esp_write_reg,
    118 	esp_dma_isintr,
    119 	esp_dma_reset,
    120 	esp_dma_intr,
    121 	esp_dma_setup,
    122 	esp_dma_go,
    123 	esp_dma_stop,
    124 	esp_dma_isactive,
    125 	NULL,			/* gl_clear_latched_intr */
    126 };
    127 
    128 static struct ncr53c9x_glue esp_sbus_glue1 = {
    129 	esp_rdreg1,
    130 	esp_wrreg1,
    131 	esp_dma_isintr,
    132 	esp_dma_reset,
    133 	esp_dma_intr,
    134 	esp_dma_setup,
    135 	esp_dma_go,
    136 	esp_dma_stop,
    137 	esp_dma_isactive,
    138 	NULL,			/* gl_clear_latched_intr */
    139 };
    140 
    141 int
    142 espmatch_sbus(struct device *parent, struct cfdata *cf, void *aux)
    143 {
    144 	int rv;
    145 	struct sbus_attach_args *sa = aux;
    146 
    147 	if (strcmp("SUNW,fas", sa->sa_name) == 0)
    148 	        return 1;
    149 
    150 	rv = (strcmp(cf->cf_name, sa->sa_name) == 0 ||
    151 	    strcmp("ptscII", sa->sa_name) == 0);
    152 	return rv;
    153 }
    154 
    155 void
    156 espattach_sbus(device_t parent, device_t self, void *aux)
    157 {
    158 	struct esp_softc *esc = device_private(self);
    159 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    160 	struct sbus_softc *sbsc = device_private(parent);
    161 	struct sbus_attach_args *sa = aux;
    162 	struct lsi64854_softc *lsc;
    163 	device_t dma_dev;
    164 	int burst, sbusburst;
    165 
    166 	sc->sc_dev = self;
    167 
    168 #ifdef DDB
    169 	esp_init_ddb_cmds();
    170 #endif
    171 
    172 	esc->sc_bustag = sa->sa_bustag;
    173 	esc->sc_dmatag = sa->sa_dmatag;
    174 
    175 	sc->sc_id = prom_getpropint(sa->sa_node, "initiator-id", 7);
    176 	sc->sc_freq = prom_getpropint(sa->sa_node, "clock-frequency", -1);
    177 	if (sc->sc_freq < 0)
    178 		sc->sc_freq = sbsc->sc_clockfreq;
    179 
    180 #ifdef ESP_SBUS_DEBUG
    181 	aprint_normal("\n");
    182 	aprint_normal_dev(self, "%s: sc_id %d, freq %d\n",
    183 	    __func__, sc->sc_id, sc->sc_freq);
    184 	aprint_normal("%s", device_xname(self));
    185 #endif
    186 
    187 	if (strcmp("SUNW,fas", sa->sa_name) == 0) {
    188 
    189 		/*
    190 		 * fas has 2 register spaces: dma(lsi64854) and
    191 		 *                            SCSI core (ncr53c9x)
    192 		 */
    193 		if (sa->sa_nreg != 2) {
    194 			aprint_error(": %d register spaces\n", sa->sa_nreg);
    195 			return;
    196 		}
    197 
    198 		/*
    199 		 * allocate space for dma, in SUNW,fas there are no separate
    200 		 * dma device
    201 		 */
    202 		lsc = malloc(sizeof(struct lsi64854_softc), M_DEVBUF, M_NOWAIT);
    203 
    204 		if (lsc == NULL) {
    205 			aprint_error(": out of memory (lsi64854_softc)\n");
    206 			return;
    207 		}
    208 		lsc->sc_dev = malloc(sizeof(struct device), M_DEVBUF,
    209 		    M_NOWAIT | M_ZERO);
    210 		if (lsc->sc_dev == NULL) {
    211 			aprint_error(": out of memory (device_t)\n");
    212 			free(lsc, M_DEVBUF);
    213 			return;
    214 		}
    215 		esc->sc_dma = lsc;
    216 
    217 		lsc->sc_bustag = sa->sa_bustag;
    218 		lsc->sc_dmatag = sa->sa_dmatag;
    219 
    220 		strlcpy(lsc->sc_dev->dv_xname, device_xname(sc->sc_dev),
    221 		    sizeof(lsc->sc_dev->dv_xname));
    222 
    223 		/* Map dma registers */
    224 		if (sa->sa_npromvaddrs) {
    225 			sbus_promaddr_to_handle(sa->sa_bustag,
    226 			    sa->sa_promvaddrs[0], &lsc->sc_regs);
    227 		} else {
    228 			if (sbus_bus_map(sa->sa_bustag,
    229 			    sa->sa_reg[0].oa_space,
    230 			    sa->sa_reg[0].oa_base,
    231 			    sa->sa_reg[0].oa_size,
    232 			    0, &lsc->sc_regs) != 0) {
    233 				aprint_error(": cannot map dma registers\n");
    234 				return;
    235 			}
    236 		}
    237 
    238 		/*
    239 		 * XXX is this common(from bpp.c), the same in dma_sbus...etc.
    240 		 *
    241 		 * Get transfer burst size from PROM and plug it into the
    242 		 * controller registers. This is needed on the Sun4m; do
    243 		 * others need it too?
    244 		 */
    245 		sbusburst = sbsc->sc_burst;
    246 		if (sbusburst == 0)
    247 			sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
    248 
    249 		burst = prom_getpropint(sa->sa_node, "burst-sizes", -1);
    250 
    251 #if ESP_SBUS_DEBUG
    252 		aprint_normal("%s: burst 0x%x, sbus 0x%x\n",
    253 		    __func__, burst, sbusburst);
    254 		aprint_normal("%s", device_xname(self));
    255 #endif
    256 
    257 		if (burst == -1)
    258 			/* take SBus burst sizes */
    259 			burst = sbusburst;
    260 
    261 		/* Clamp at parent's burst sizes */
    262 		burst &= sbusburst;
    263 		lsc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
    264 		    (burst & SBUS_BURST_16) ? 16 : 0;
    265 
    266 		lsc->sc_channel = L64854_CHANNEL_SCSI;
    267 		lsc->sc_client = sc;
    268 
    269 		lsi64854_attach(lsc);
    270 
    271 		/*
    272 		 * map SCSI core registers
    273 		 */
    274 		if (sa->sa_npromvaddrs > 1) {
    275 			sbus_promaddr_to_handle(sa->sa_bustag,
    276 			    sa->sa_promvaddrs[1], &esc->sc_reg);
    277 		} else {
    278 			if (sbus_bus_map(sa->sa_bustag,
    279 			    sa->sa_reg[1].oa_space,
    280 			    sa->sa_reg[1].oa_base,
    281 			    sa->sa_reg[1].oa_size,
    282 			    0, &esc->sc_reg) != 0) {
    283 				aprint_error(": cannot map "
    284 				    "scsi core registers\n");
    285 				return;
    286 			}
    287 		}
    288 
    289 		if (sa->sa_nintr == 0) {
    290 			aprint_error(": no interrupt property\n");
    291 			return;
    292 		}
    293 
    294 		esc->sc_pri = sa->sa_pri;
    295 
    296 		/* add me to the sbus structures */
    297 		esc->sc_sd.sd_reset = (void *)ncr53c9x_reset;
    298 		sbus_establish(&esc->sc_sd, self);
    299 
    300 		espattach(esc, &esp_sbus_glue);
    301 
    302 		return;
    303 	}
    304 
    305 	/*
    306 	 * Find the DMA by poking around the dma device structures
    307 	 *
    308 	 * What happens here is that if the dma driver has not been
    309 	 * configured, then this returns a NULL pointer. Then when the
    310 	 * dma actually gets configured, it does the opposing test, and
    311 	 * if the sc->sc_esp field in it's softc is NULL, then tries to
    312 	 * find the matching esp driver.
    313 	 */
    314 	dma_dev = device_find_by_driver_unit("dma", device_unit(self));
    315 	if (dma_dev == NULL) {
    316 		aprint_error(": no corresponding DMA device\n");
    317 		return;
    318 	}
    319 	esc->sc_dma = device_private(dma_dev);
    320 	esc->sc_dma->sc_client = sc;
    321 
    322 	/*
    323 	 * The `ESC' DMA chip must be reset before we can access
    324 	 * the esp registers.
    325 	 */
    326 	if (esc->sc_dma->sc_rev == DMAREV_ESC)
    327 		DMA_RESET(esc->sc_dma);
    328 
    329 	/*
    330 	 * Map my registers in, if they aren't already in virtual
    331 	 * address space.
    332 	 */
    333 	if (sa->sa_npromvaddrs) {
    334 		sbus_promaddr_to_handle(sa->sa_bustag,
    335 		    sa->sa_promvaddrs[0], &esc->sc_reg);
    336 	} else {
    337 		if (sbus_bus_map(sa->sa_bustag,
    338 		    sa->sa_slot, sa->sa_offset, sa->sa_size,
    339 		    0, &esc->sc_reg) != 0) {
    340 			aprint_error(": cannot map registers\n");
    341 			return;
    342 		}
    343 	}
    344 
    345 	if (sa->sa_nintr == 0) {
    346 		/*
    347 		 * No interrupt properties: we quit; this might
    348 		 * happen on e.g. a Sparc X terminal.
    349 		 */
    350 		aprint_error(": no interrupt property\n");
    351 		return;
    352 	}
    353 
    354 	esc->sc_pri = sa->sa_pri;
    355 
    356 	/* add me to the sbus structures */
    357 	esc->sc_sd.sd_reset = (void *)ncr53c9x_reset;
    358 	sbus_establish(&esc->sc_sd, self);
    359 
    360 	if (strcmp("ptscII", sa->sa_name) == 0) {
    361 		espattach(esc, &esp_sbus_glue1);
    362 	} else {
    363 		espattach(esc, &esp_sbus_glue);
    364 	}
    365 }
    366 
    367 void
    368 espattach_dma(device_t parent, device_t self, void *aux)
    369 {
    370 	struct esp_softc *esc = device_private(self);
    371 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    372 	struct sbus_attach_args *sa = aux;
    373 
    374 	if (strcmp("ptscII", sa->sa_name) == 0) {
    375 		return;
    376 	}
    377 
    378 	sc->sc_dev = self;
    379 
    380 	esc->sc_bustag = sa->sa_bustag;
    381 	esc->sc_dmatag = sa->sa_dmatag;
    382 
    383 	sc->sc_id = prom_getpropint(sa->sa_node, "initiator-id", 7);
    384 	sc->sc_freq = prom_getpropint(sa->sa_node, "clock-frequency", -1);
    385 
    386 	esc->sc_dma = device_private(parent);
    387 	esc->sc_dma->sc_client = sc;
    388 
    389 	/*
    390 	 * Map my registers in, if they aren't already in virtual
    391 	 * address space.
    392 	 */
    393 	if (sa->sa_npromvaddrs) {
    394 		sbus_promaddr_to_handle(sa->sa_bustag,
    395 		    sa->sa_promvaddrs[0], &esc->sc_reg);
    396 	} else {
    397 		if (sbus_bus_map(sa->sa_bustag,
    398 		    sa->sa_slot, sa->sa_offset, sa->sa_size,
    399 		    0, &esc->sc_reg) != 0) {
    400 			aprint_error(": cannot map registers\n");
    401 			return;
    402 		}
    403 	}
    404 
    405 	if (sa->sa_nintr == 0) {
    406 		/*
    407 		 * No interrupt properties: we quit; this might
    408 		 * happen on e.g. a Sparc X terminal.
    409 		 */
    410 		aprint_error(": no interrupt property\n");
    411 		return;
    412 	}
    413 
    414 	esc->sc_pri = sa->sa_pri;
    415 
    416 	/* Assume SBus is grandparent */
    417 	esc->sc_sd.sd_reset = (void *)ncr53c9x_reset;
    418 	sbus_establish(&esc->sc_sd, parent);
    419 
    420 	espattach(esc, &esp_sbus_glue);
    421 }
    422 
    423 
    424 /*
    425  * Attach this instance, and then all the sub-devices
    426  */
    427 void
    428 espattach(struct esp_softc *esc, struct ncr53c9x_glue *gluep)
    429 {
    430 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    431 	void *icookie;
    432 	unsigned int uid = 0;
    433 
    434 	/*
    435 	 * Set up glue for MI code early; we use some of it here.
    436 	 */
    437 	sc->sc_glue = gluep;
    438 
    439 	/* gimme MHz */
    440 	sc->sc_freq /= 1000000;
    441 
    442 	/*
    443 	 * XXX More of this should be in ncr53c9x_attach(), but
    444 	 * XXX should we really poke around the chip that much in
    445 	 * XXX the MI code?  Think about this more...
    446 	 */
    447 
    448 	/*
    449 	 * It is necessary to try to load the 2nd config register here,
    450 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    451 	 * will not set up the defaults correctly.
    452 	 */
    453 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    454 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    455 	sc->sc_cfg3 = NCRCFG3_CDB;
    456 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    457 
    458 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    459 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    460 		sc->sc_rev = NCR_VARIANT_ESP100;
    461 	} else {
    462 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    463 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    464 		sc->sc_cfg3 = 0;
    465 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    466 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    467 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    468 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    469 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    470 			sc->sc_rev = NCR_VARIANT_ESP100A;
    471 		} else {
    472 			/* NCRCFG2_FE enables > 64K transfers */
    473 			sc->sc_cfg2 |= NCRCFG2_FE;
    474 			sc->sc_cfg3 = 0;
    475 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    476 			sc->sc_rev = NCR_VARIANT_ESP200;
    477 
    478 			/*
    479 			 * XXX spec says it's valid after power up or
    480 			 * chip reset
    481 			 */
    482 			uid = NCR_READ_REG(sc, NCR_UID);
    483 			if (((uid & 0xf8) >> 3) == 0x0a) /* XXX */
    484 				sc->sc_rev = NCR_VARIANT_FAS366;
    485 		}
    486 	}
    487 
    488 #ifdef ESP_SBUS_DEBUG
    489 	aprint_debug("%s: revision %d, uid 0x%x\n", __func__, sc->sc_rev, uid);
    490 	aprint_normal("%s", device_xname(sc->sc_dev));
    491 #endif
    492 
    493 	/*
    494 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    495 	 * XXX but it appears to have some dependency on what sort
    496 	 * XXX of DMA we're hooked up to, etc.
    497 	 */
    498 
    499 	/*
    500 	 * This is the value used to start sync negotiations
    501 	 * Note that the NCR register "SYNCTP" is programmed
    502 	 * in "clocks per byte", and has a minimum value of 4.
    503 	 * The SCSI period used in negotiation is one-fourth
    504 	 * of the time (in nanoseconds) needed to transfer one byte.
    505 	 * Since the chip's clock is given in MHz, we have the following
    506 	 * formula: 4 * period = (1000 / freq) * 4
    507 	 */
    508 	sc->sc_minsync = 1000 / sc->sc_freq;
    509 
    510 	/*
    511 	 * Alas, we must now modify the value a bit, because it's
    512 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    513 	 * in config register 3...
    514 	 */
    515 	switch (sc->sc_rev) {
    516 	case NCR_VARIANT_ESP100:
    517 		sc->sc_maxxfer = 64 * 1024;
    518 		sc->sc_minsync = 0;	/* No synch on old chip? */
    519 		break;
    520 
    521 	case NCR_VARIANT_ESP100A:
    522 		sc->sc_maxxfer = 64 * 1024;
    523 		/* Min clocks/byte is 5 */
    524 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    525 		break;
    526 
    527 	case NCR_VARIANT_ESP200:
    528 	case NCR_VARIANT_FAS366:
    529 		sc->sc_maxxfer = 16 * 1024 * 1024;
    530 		/* XXX - do actually set FAST* bits */
    531 		break;
    532 	}
    533 
    534 	/* Establish interrupt channel */
    535 	icookie = bus_intr_establish(esc->sc_bustag, esc->sc_pri, IPL_BIO,
    536 	    ncr53c9x_intr, sc);
    537 
    538 	/* register interrupt stats */
    539 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    540 	    device_xname(sc->sc_dev), "intr");
    541 
    542 	/* Turn on target selection using the `dma' method */
    543 	if (sc->sc_rev != NCR_VARIANT_FAS366)
    544 		sc->sc_features |= NCR_F_DMASELECT;
    545 
    546 	/* Do the common parts of attachment. */
    547 	sc->sc_adapter.adapt_minphys = minphys;
    548 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    549 	ncr53c9x_attach(sc);
    550 }
    551 
    552 /*
    553  * Glue functions.
    554  */
    555 
    556 #ifdef ESP_SBUS_DEBUG
    557 int esp_sbus_debug = 0;
    558 
    559 static struct {
    560 	char *r_name;
    561 	int   r_flag;
    562 } esp__read_regnames [] = {
    563 	{ "TCL", 0},			/* 0/00 */
    564 	{ "TCM", 0},			/* 1/04 */
    565 	{ "FIFO", 0},			/* 2/08 */
    566 	{ "CMD", 0},			/* 3/0c */
    567 	{ "STAT", 0},			/* 4/10 */
    568 	{ "INTR", 0},			/* 5/14 */
    569 	{ "STEP", 0},			/* 6/18 */
    570 	{ "FFLAGS", 1},			/* 7/1c */
    571 	{ "CFG1", 1},			/* 8/20 */
    572 	{ "STAT2", 0},			/* 9/24 */
    573 	{ "CFG4", 1},			/* a/28 */
    574 	{ "CFG2", 1},			/* b/2c */
    575 	{ "CFG3", 1},			/* c/30 */
    576 	{ "-none", 1},			/* d/34 */
    577 	{ "TCH", 1},			/* e/38 */
    578 	{ "TCX", 1},			/* f/3c */
    579 };
    580 
    581 static struct {
    582 	char *r_name;
    583 	int   r_flag;
    584 } esp__write_regnames[] = {
    585 	{ "TCL", 1},			/* 0/00 */
    586 	{ "TCM", 1},			/* 1/04 */
    587 	{ "FIFO", 0},			/* 2/08 */
    588 	{ "CMD", 0},			/* 3/0c */
    589 	{ "SELID", 1},			/* 4/10 */
    590 	{ "TIMEOUT", 1},		/* 5/14 */
    591 	{ "SYNCTP", 1},			/* 6/18 */
    592 	{ "SYNCOFF", 1},		/* 7/1c */
    593 	{ "CFG1", 1},			/* 8/20 */
    594 	{ "CCF", 1},			/* 9/24 */
    595 	{ "TEST", 1},			/* a/28 */
    596 	{ "CFG2", 1},			/* b/2c */
    597 	{ "CFG3", 1},			/* c/30 */
    598 	{ "-none", 1},			/* d/34 */
    599 	{ "TCH", 1},			/* e/38 */
    600 	{ "TCX", 1},			/* f/3c */
    601 };
    602 #endif
    603 
    604 uint8_t
    605 esp_read_reg(struct ncr53c9x_softc *sc, int reg)
    606 {
    607 	struct esp_softc *esc = (struct esp_softc *)sc;
    608 	uint8_t v;
    609 
    610 	v = bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4);
    611 #ifdef ESP_SBUS_DEBUG
    612 	if (esp_sbus_debug && (reg < 0x10) && esp__read_regnames[reg].r_flag)
    613 		printf("RD:%x <%s> %x\n", reg * 4,
    614 		    ((unsigned int)reg < 0x10) ?
    615 		    esp__read_regnames[reg].r_name : "<***>", v);
    616 #endif
    617 	return v;
    618 }
    619 
    620 void
    621 esp_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t v)
    622 {
    623 	struct esp_softc *esc = (struct esp_softc *)sc;
    624 
    625 #ifdef ESP_SBUS_DEBUG
    626 	if (esp_sbus_debug && (reg < 0x10) && esp__write_regnames[reg].r_flag)
    627 		printf("WR:%x <%s> %x\n", reg * 4,
    628 		    ((unsigned int)reg < 0x10) ?
    629 		    esp__write_regnames[reg].r_name : "<***>", v);
    630 #endif
    631 	bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
    632 }
    633 
    634 uint8_t
    635 esp_rdreg1(struct ncr53c9x_softc *sc, int reg)
    636 {
    637 	struct esp_softc *esc = (struct esp_softc *)sc;
    638 
    639 	return bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg);
    640 }
    641 
    642 void
    643 esp_wrreg1(struct ncr53c9x_softc *sc, int reg, uint8_t v)
    644 {
    645 	struct esp_softc *esc = (struct esp_softc *)sc;
    646 
    647 	bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg, v);
    648 }
    649 
    650 int
    651 esp_dma_isintr(struct ncr53c9x_softc *sc)
    652 {
    653 	struct esp_softc *esc = (struct esp_softc *)sc;
    654 
    655 	return DMA_ISINTR(esc->sc_dma);
    656 }
    657 
    658 void
    659 esp_dma_reset(struct ncr53c9x_softc *sc)
    660 {
    661 	struct esp_softc *esc = (struct esp_softc *)sc;
    662 
    663 	DMA_RESET(esc->sc_dma);
    664 }
    665 
    666 int
    667 esp_dma_intr(struct ncr53c9x_softc *sc)
    668 {
    669 	struct esp_softc *esc = (struct esp_softc *)sc;
    670 
    671 	return DMA_INTR(esc->sc_dma);
    672 }
    673 
    674 int
    675 esp_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    676     int datain, size_t *dmasize)
    677 {
    678 	struct esp_softc *esc = (struct esp_softc *)sc;
    679 
    680 	return DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize);
    681 }
    682 
    683 void
    684 esp_dma_go(struct ncr53c9x_softc *sc)
    685 {
    686 	struct esp_softc *esc = (struct esp_softc *)sc;
    687 
    688 	DMA_GO(esc->sc_dma);
    689 }
    690 
    691 void
    692 esp_dma_stop(struct ncr53c9x_softc *sc)
    693 {
    694 	struct esp_softc *esc = (struct esp_softc *)sc;
    695 	uint32_t csr;
    696 
    697 	csr = L64854_GCSR(esc->sc_dma);
    698 	csr &= ~D_EN_DMA;
    699 	L64854_SCSR(esc->sc_dma, csr);
    700 }
    701 
    702 int
    703 esp_dma_isactive(struct ncr53c9x_softc *sc)
    704 {
    705 	struct esp_softc *esc = (struct esp_softc *)sc;
    706 
    707 	return DMA_ISACTIVE(esc->sc_dma);
    708 }
    709 
    710 #ifdef DDB
    711 #include <machine/db_machdep.h>
    712 #include <ddb/db_output.h>
    713 #include <ddb/db_command.h>
    714 
    715 void db_esp(db_expr_t, bool, db_expr_t, const char*);
    716 
    717 const struct db_command db_esp_command_table[] = {
    718 	{ DDB_ADD_CMD("esp",	db_esp,	0,
    719 	  "display status of all esp SCSI controllers and their devices",
    720 	  NULL, NULL) },
    721 	{ DDB_ADD_CMD(NULL,	NULL,	0, NULL, NULL, NULL) }
    722 };
    723 
    724 static void
    725 esp_init_ddb_cmds(void)
    726 {
    727 	static int db_cmds_initialized = 0;
    728 
    729 	if (db_cmds_initialized)
    730 		return;
    731 	db_cmds_initialized = 1;
    732 	(void)db_register_tbl(DDB_MACH_CMD, db_esp_command_table);
    733 }
    734 
    735 void
    736 db_esp(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    737 {
    738 	device_t dv;
    739 	struct ncr53c9x_softc *sc;
    740 	struct ncr53c9x_ecb *ecb;
    741 	struct ncr53c9x_linfo *li;
    742 	int u, t, i;
    743 
    744 	for (u = 0; u < 10; u++) {
    745 		dv = device_find_by_driver_unit("esp", u);
    746 		if (dv == NULL)
    747 			continue;
    748 		sc = device_private(dv);
    749 
    750 		db_printf("%s: nexus %p phase %x prev %x"
    751 		    " dp %p dleft %lx ify %x\n", device_xname(dv),
    752 		    sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
    753 		      sc->sc_dp, sc->sc_dleft, sc->sc_msgify);
    754 		db_printf("\tmsgout %x msgpriq %x msgin %x:%x:%x:%x:%x\n",
    755 		     sc->sc_msgout, sc->sc_msgpriq, sc->sc_imess[0],
    756 		     sc->sc_imess[1], sc->sc_imess[2], sc->sc_imess[3],
    757 		     sc->sc_imess[0]);
    758 		db_printf("ready: ");
    759 		for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
    760 		    ecb = TAILQ_NEXT(ecb, chain)) {
    761 			db_printf("ecb %p ", ecb);
    762 			if (ecb == TAILQ_NEXT(ecb, chain)) {
    763 				db_printf("\nWARNING: tailq loop on ecb %p",
    764 				    ecb);
    765 				break;
    766 			}
    767 		}
    768 		db_printf("\n");
    769 
    770 		for (t = 0; t < sc->sc_ntarg; t++) {
    771 			LIST_FOREACH(li, &sc->sc_tinfo[t].luns, link) {
    772 				db_printf("t%d lun %d untagged %p"
    773 				    " busy %d used %x\n",
    774 				    t, (int)li->lun, li->untagged, li->busy,
    775 				    li->used);
    776 				for (i = 0; i < 256; i++)
    777 					ecb = li->queued[i];
    778 					if (ecb != NULL) {
    779 						db_printf("ecb %p tag %x\n",
    780 						    ecb, i);
    781 					}
    782 			}
    783 		}
    784 	}
    785 }
    786 #endif
    787