Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.27
      1 /*	$NetBSD: esp.c,v 1.27 2008/04/13 04:55:53 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jeremy Cooper and Gordon W. Ross
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * "Front end" glue for the ncr53c9x chip, formerly known as the
     41  * Emulex SCSI Processor (ESP) which is what we actually have.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: esp.c,v 1.27 2008/04/13 04:55:53 tsutsui Exp $");
     46 
     47 #include <sys/types.h>
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/errno.h>
     52 #include <sys/device.h>
     53 #include <sys/buf.h>
     54 
     55 #include <dev/scsipi/scsi_all.h>
     56 #include <dev/scsipi/scsipi_all.h>
     57 #include <dev/scsipi/scsiconf.h>
     58 #include <dev/scsipi/scsi_message.h>
     59 
     60 #include <machine/autoconf.h>
     61 #include <machine/bus.h>
     62 
     63 #include <dev/ic/ncr53c9xreg.h>
     64 #include <dev/ic/ncr53c9xvar.h>
     65 
     66 #include <sun3/dev/dmareg.h>
     67 #include <sun3/dev/dmavar.h>
     68 
     69 #define	ESP_REG_SIZE	(12*4)
     70 
     71 struct esp_softc {
     72 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
     73 	bus_space_tag_t sc_bst;			/* bus space tag */
     74 	bus_space_handle_t sc_bsh;		/* bus space handle */
     75 	struct dma_softc *sc_dma;		/* pointer to my dma */
     76 };
     77 
     78 static int	espmatch(device_t, cfdata_t, void *);
     79 static void	espattach(device_t, device_t, void *);
     80 
     81 CFATTACH_DECL_NEW(esp, sizeof(struct esp_softc),
     82     espmatch, espattach, NULL, NULL);
     83 
     84 /*
     85  * Functions and the switch for the MI code.
     86  */
     87 static uint8_t	esp_read_reg(struct ncr53c9x_softc *, int);
     88 static void	esp_write_reg(struct ncr53c9x_softc *, int, uint8_t);
     89 static int	esp_dma_isintr(struct ncr53c9x_softc *);
     90 static void	esp_dma_reset(struct ncr53c9x_softc *);
     91 static int	esp_dma_intr(struct ncr53c9x_softc *);
     92 static int	esp_dma_setup(struct ncr53c9x_softc *, uint8_t **, size_t *,
     93 		    int, size_t *);
     94 static void	esp_dma_go(struct ncr53c9x_softc *);
     95 static void	esp_dma_stop(struct ncr53c9x_softc *);
     96 static int	esp_dma_isactive(struct ncr53c9x_softc *);
     97 
     98 static struct ncr53c9x_glue esp_glue = {
     99 	esp_read_reg,
    100 	esp_write_reg,
    101 	esp_dma_isintr,
    102 	esp_dma_reset,
    103 	esp_dma_intr,
    104 	esp_dma_setup,
    105 	esp_dma_go,
    106 	esp_dma_stop,
    107 	esp_dma_isactive,
    108 	NULL,			/* gl_clear_latched_intr */
    109 };
    110 
    111 static int
    112 espmatch(device_t parent, struct cfdata *cf, void *aux)
    113 {
    114 	struct confargs *ca = aux;
    115 
    116 	/*
    117 	 * Check for the esp registers.
    118 	 */
    119 	if (bus_peek(ca->ca_bustype,
    120 	    ca->ca_paddr + (NCR_STAT * 4), 1) == -1)
    121 		return 0;
    122 
    123 	/* If default ipl, fill it in. */
    124 	if (ca->ca_intpri == -1)
    125 		ca->ca_intpri = 2;
    126 
    127 	return 1;
    128 }
    129 
    130 static void
    131 espattach(device_t parent, device_t self, void *aux)
    132 {
    133 	struct esp_softc *esc = device_private(self);
    134 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    135 	struct confargs *ca = aux;
    136 
    137 	/*
    138 	 * Set up glue for MI code early; we use some of it here.
    139 	 */
    140 	sc->sc_dev = self;
    141 	sc->sc_glue = &esp_glue;
    142 
    143 	/*
    144 	 * Map the ESP registers.
    145 	 */
    146 	esc->sc_bst = ca->ca_bustag;
    147 	if (bus_space_map(esc->sc_bst, ca->ca_paddr, ESP_REG_SIZE, 0,
    148 	    &esc->sc_bsh) != 0) {
    149 		aprint_error(": can't map register\n");
    150 		return;
    151 	}
    152 
    153 	/* Other settings */
    154 	sc->sc_id = 7;
    155 	sc->sc_freq = 20;	/* The 3/80 esp runs at 20 MHz */
    156 
    157 	/*
    158 	 * Hook up the DMA driver.
    159 	 */
    160 	esc->sc_dma = espdmafind(device_unit(self));
    161 	esc->sc_dma->sc_client = sc; /* Point back to us */
    162 
    163 	/*
    164 	 * XXX More of this should be in ncr53c9x_attach(), but
    165 	 * XXX should we really poke around the chip that much in
    166 	 * XXX the MI code?  Think about this more...
    167 	 */
    168 
    169 	/*
    170 	 * It is necessary to try to load the 2nd config register here,
    171 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    172 	 * will not set up the defaults correctly.
    173 	 */
    174 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    175 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    176 	sc->sc_cfg3 = NCRCFG3_CDB;
    177 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    178 
    179 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    180 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    181 		sc->sc_rev = NCR_VARIANT_ESP100;
    182 	} else {
    183 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    184 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    185 		sc->sc_cfg3 = 0;
    186 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    187 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    188 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    189 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    190 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    191 			sc->sc_rev = NCR_VARIANT_ESP100A;
    192 		} else {
    193 			/* NCRCFG2_FE enables > 64K transfers */
    194 			sc->sc_cfg2 |= NCRCFG2_FE;
    195 			sc->sc_cfg3 = 0;
    196 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    197 			sc->sc_rev = NCR_VARIANT_ESP200;
    198 		}
    199 	}
    200 
    201 	/*
    202 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    203 	 * XXX but it appears to have some dependency on what sort
    204 	 * XXX of DMA we're hooked up to, etc.
    205 	 */
    206 
    207 	/*
    208 	 * This is the value used to start sync negotiations
    209 	 * Note that the NCR register "SYNCTP" is programmed
    210 	 * in "clocks per byte", and has a minimum value of 4.
    211 	 * The SCSI period used in negotiation is one-fourth
    212 	 * of the time (in nanoseconds) needed to transfer one byte.
    213 	 * Since the chip's clock is given in MHz, we have the following
    214 	 * formula: 4 * period = (1000 / freq) * 4
    215 	 */
    216 	sc->sc_minsync = 1000 / sc->sc_freq;
    217 
    218 	/*
    219 	 * Alas, we must now modify the value a bit, because it's
    220 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    221 	 * in config register 3...
    222 	 */
    223 	switch (sc->sc_rev) {
    224 	case NCR_VARIANT_ESP100:
    225 		sc->sc_maxxfer = 64 * 1024;
    226 		sc->sc_minsync = 0;	/* No synch on old chip? */
    227 		break;
    228 
    229 	case NCR_VARIANT_ESP100A:
    230 		sc->sc_maxxfer = 64 * 1024;
    231 		/* Min clocks/byte is 5 */
    232 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    233 		break;
    234 
    235 	case NCR_VARIANT_ESP200:
    236 		sc->sc_maxxfer = 16 * 1024 * 1024;
    237 		/* XXX - do actually set FAST* bits */
    238 		break;
    239 	}
    240 
    241 	/* and the interuppts */
    242 	isr_add_autovect(ncr53c9x_intr, sc, ca->ca_intpri);
    243 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    244 	    device_xname(self), "intr");
    245 
    246 	/* Do the common parts of attachment. */
    247 	sc->sc_adapter.adapt_minphys = minphys;
    248 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    249 	ncr53c9x_attach(sc);
    250 
    251 	/* Turn on target selection using the `dma' method */
    252 	sc->sc_features |= NCR_F_DMASELECT;
    253 }
    254 
    255 
    256 /*
    257  * Glue functions.
    258  */
    259 
    260 uint8_t
    261 esp_read_reg(struct ncr53c9x_softc *sc, int reg)
    262 {
    263 	struct esp_softc *esc = (struct esp_softc *)sc;
    264 
    265 	return bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg * 4);
    266 }
    267 
    268 void
    269 esp_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
    270 {
    271 	struct esp_softc *esc = (struct esp_softc *)sc;
    272 
    273 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg * 4, val);
    274 }
    275 
    276 int
    277 esp_dma_isintr(struct ncr53c9x_softc *sc)
    278 {
    279 	struct esp_softc *esc = (struct esp_softc *)sc;
    280 
    281 	return DMA_ISINTR(esc->sc_dma);
    282 }
    283 
    284 void
    285 esp_dma_reset(struct ncr53c9x_softc *sc)
    286 {
    287 	struct esp_softc *esc = (struct esp_softc *)sc;
    288 
    289 	dma_reset(esc->sc_dma);
    290 }
    291 
    292 int
    293 esp_dma_intr(struct ncr53c9x_softc *sc)
    294 {
    295 	struct esp_softc *esc = (struct esp_softc *)sc;
    296 
    297 	return espdmaintr(esc->sc_dma);
    298 }
    299 
    300 int
    301 esp_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    302     int datain, size_t *dmasize)
    303 {
    304 	struct esp_softc *esc = (struct esp_softc *)sc;
    305 
    306 	return dma_setup(esc->sc_dma, addr, len, datain, dmasize);
    307 }
    308 
    309 void
    310 esp_dma_go(struct ncr53c9x_softc *sc)
    311 {
    312 	struct esp_softc *esc = (struct esp_softc *)sc;
    313 
    314 	DMA_GO(esc->sc_dma);
    315 }
    316 
    317 void
    318 esp_dma_stop(struct ncr53c9x_softc *sc)
    319 {
    320 	struct esp_softc *esc = (struct esp_softc *)sc;
    321 
    322 	DMA_STOP(esc->sc_dma);
    323 }
    324 
    325 int
    326 esp_dma_isactive(struct ncr53c9x_softc *sc)
    327 {
    328 	struct esp_softc *esc = (struct esp_softc *)sc;
    329 
    330 	return DMA_ISACTIVE(esc->sc_dma);
    331 }
    332