Home | History | Annotate | Line # | Download | only in dev
      1  1.24   dyoung /*	$NetBSD: esp_obio.c,v 1.24 2011/07/01 18:50:41 dyoung Exp $	*/
      2   1.1       pk 
      3   1.1       pk /*-
      4   1.1       pk  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1       pk  * All rights reserved.
      6   1.1       pk  *
      7   1.1       pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       pk  * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
      9   1.1       pk  * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
     10   1.1       pk  *
     11   1.1       pk  * Redistribution and use in source and binary forms, with or without
     12   1.1       pk  * modification, are permitted provided that the following conditions
     13   1.1       pk  * are met:
     14   1.1       pk  * 1. Redistributions of source code must retain the above copyright
     15   1.1       pk  *    notice, this list of conditions and the following disclaimer.
     16   1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       pk  *    documentation and/or other materials provided with the distribution.
     19   1.1       pk  *
     20   1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1       pk  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1       pk  */
     32  1.16    lukem 
     33  1.16    lukem #include <sys/cdefs.h>
     34  1.24   dyoung __KERNEL_RCSID(0, "$NetBSD: esp_obio.c,v 1.24 2011/07/01 18:50:41 dyoung Exp $");
     35   1.1       pk 
     36   1.1       pk #include <sys/types.h>
     37   1.1       pk #include <sys/param.h>
     38   1.1       pk #include <sys/systm.h>
     39   1.1       pk #include <sys/kernel.h>
     40   1.1       pk #include <sys/errno.h>
     41   1.1       pk #include <sys/device.h>
     42   1.1       pk #include <sys/buf.h>
     43   1.1       pk 
     44   1.1       pk #include <dev/scsipi/scsi_all.h>
     45   1.1       pk #include <dev/scsipi/scsipi_all.h>
     46   1.1       pk #include <dev/scsipi/scsiconf.h>
     47   1.1       pk #include <dev/scsipi/scsi_message.h>
     48   1.1       pk 
     49  1.24   dyoung #include <sys/bus.h>
     50   1.1       pk #include <machine/autoconf.h>
     51   1.8       pk #include <machine/intr.h>
     52   1.1       pk 
     53   1.1       pk #include <dev/ic/lsi64854reg.h>
     54   1.1       pk #include <dev/ic/lsi64854var.h>
     55   1.1       pk 
     56   1.1       pk #include <dev/ic/ncr53c9xreg.h>
     57   1.1       pk #include <dev/ic/ncr53c9xvar.h>
     58   1.1       pk 
     59   1.1       pk #include <dev/sbus/sbusvar.h>
     60   1.1       pk 
     61   1.1       pk struct esp_softc {
     62   1.1       pk 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
     63   1.1       pk 	bus_space_tag_t		sc_bustag;
     64   1.1       pk 	bus_dma_tag_t		sc_dmatag;
     65   1.1       pk 	bus_space_handle_t	sc_reg;		/* the registers */
     66   1.1       pk 	struct lsi64854_softc	*sc_dma;	/* pointer to my dma */
     67   1.1       pk };
     68   1.1       pk 
     69   1.1       pk 
     70  1.22  tsutsui int	espmatch_obio(device_t, cfdata_t, void *);
     71  1.22  tsutsui void	espattach_obio(device_t, device_t, void *);
     72   1.1       pk 
     73   1.1       pk /* Linkup to the rest of the kernel */
     74  1.22  tsutsui CFATTACH_DECL_NEW(esp_obio, sizeof(struct esp_softc),
     75  1.14  thorpej     espmatch_obio, espattach_obio, NULL, NULL);
     76   1.1       pk 
     77   1.1       pk /*
     78   1.1       pk  * Functions and the switch for the MI code.
     79   1.1       pk  */
     80  1.22  tsutsui static uint8_t	esp_read_reg(struct ncr53c9x_softc *, int);
     81  1.22  tsutsui static void	esp_write_reg(struct ncr53c9x_softc *, int, uint8_t);
     82  1.17      uwe static int	esp_dma_isintr(struct ncr53c9x_softc *);
     83  1.17      uwe static void	esp_dma_reset(struct ncr53c9x_softc *);
     84  1.17      uwe static int	esp_dma_intr(struct ncr53c9x_softc *);
     85  1.22  tsutsui static int	esp_dma_setup(struct ncr53c9x_softc *, uint8_t **,
     86  1.17      uwe 				    size_t *, int, size_t *);
     87  1.17      uwe static void	esp_dma_go(struct ncr53c9x_softc *);
     88  1.17      uwe static void	esp_dma_stop(struct ncr53c9x_softc *);
     89  1.17      uwe static int	esp_dma_isactive(struct ncr53c9x_softc *);
     90   1.1       pk 
     91   1.1       pk static struct ncr53c9x_glue esp_obio_glue = {
     92   1.1       pk 	esp_read_reg,
     93   1.1       pk 	esp_write_reg,
     94   1.1       pk 	esp_dma_isintr,
     95   1.1       pk 	esp_dma_reset,
     96   1.1       pk 	esp_dma_intr,
     97   1.1       pk 	esp_dma_setup,
     98   1.1       pk 	esp_dma_go,
     99   1.1       pk 	esp_dma_stop,
    100   1.1       pk 	esp_dma_isactive,
    101   1.1       pk 	NULL,			/* gl_clear_latched_intr */
    102   1.1       pk };
    103   1.1       pk 
    104   1.1       pk int
    105  1.22  tsutsui espmatch_obio(device_t parent, cfdata_t cf, void *aux)
    106   1.1       pk {
    107   1.1       pk 	union obio_attach_args *uoba = aux;
    108   1.1       pk 	struct obio4_attach_args *oba;
    109   1.1       pk 
    110   1.1       pk 	if (uoba->uoba_isobio4 == 0)
    111  1.22  tsutsui 		return 0;
    112   1.1       pk 
    113   1.1       pk 	oba = &uoba->uoba_oba4;
    114  1.22  tsutsui 	return bus_space_probe(oba->oba_bustag, oba->oba_paddr,
    115   1.1       pk 				1,	/* probe size */
    116   1.1       pk 				0,	/* offset */
    117   1.1       pk 				0,	/* flags */
    118  1.22  tsutsui 				NULL, NULL);
    119   1.1       pk }
    120   1.1       pk 
    121   1.1       pk void
    122  1.22  tsutsui espattach_obio(device_t parent, device_t self, void *aux)
    123   1.1       pk {
    124  1.22  tsutsui 	struct esp_softc *esc = device_private(self);
    125  1.22  tsutsui 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    126   1.1       pk 	union obio_attach_args *uoba = aux;
    127   1.1       pk 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
    128  1.22  tsutsui 	device_t dma_dev;
    129  1.22  tsutsui 
    130  1.22  tsutsui 	sc->sc_dev = self;
    131   1.1       pk 
    132   1.1       pk 	esc->sc_bustag = oba->oba_bustag;
    133   1.1       pk 	esc->sc_dmatag = oba->oba_dmatag;
    134   1.1       pk 
    135   1.1       pk 	sc->sc_id = 7;
    136   1.1       pk 	sc->sc_freq = 24000000;
    137   1.1       pk 
    138   1.1       pk 	/*
    139  1.21    joerg 	 * Find the DMA by poking around the dma device structures and
    140  1.21    joerg 	 * set the reverse pointer.
    141   1.1       pk 	 */
    142  1.21    joerg 	dma_dev = device_find_by_driver_unit("dma", device_unit(self));
    143  1.21    joerg 	if (dma_dev == NULL)
    144  1.21    joerg 		panic("%s: no corresponding DMA device", device_xname(self));
    145  1.21    joerg 	esc->sc_dma = device_private(dma_dev);
    146  1.21    joerg 	esc->sc_dma->sc_client = sc;
    147   1.1       pk 
    148  1.11       pk 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
    149  1.11       pk 			  16,	/* size (of ncr53c9xreg) */
    150  1.11       pk 			  BUS_SPACE_MAP_LINEAR,
    151  1.11       pk 			  &esc->sc_reg) != 0) {
    152  1.22  tsutsui 		aprint_error(": cannot map registers\n");
    153   1.1       pk 		return;
    154   1.1       pk 	}
    155   1.1       pk 
    156   1.1       pk 	/*
    157   1.1       pk 	 * Set up glue for MI code early; we use some of it here.
    158   1.1       pk 	 */
    159   1.1       pk 	sc->sc_glue = &esp_obio_glue;
    160   1.1       pk 
    161  1.18    lukem 	/* gimme MHz */
    162   1.1       pk 	sc->sc_freq /= 1000000;
    163   1.1       pk 
    164   1.1       pk 	/*
    165   1.1       pk 	 * XXX More of this should be in ncr53c9x_attach(), but
    166   1.1       pk 	 * XXX should we really poke around the chip that much in
    167   1.1       pk 	 * XXX the MI code?  Think about this more...
    168   1.1       pk 	 */
    169   1.1       pk 
    170   1.1       pk 	/*
    171   1.1       pk 	 * It is necessary to try to load the 2nd config register here,
    172   1.1       pk 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    173   1.1       pk 	 * will not set up the defaults correctly.
    174   1.1       pk 	 */
    175   1.1       pk 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    176   1.1       pk 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    177   1.1       pk 	sc->sc_cfg3 = NCRCFG3_CDB;
    178   1.1       pk 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    179   1.1       pk 
    180   1.1       pk 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    181   1.1       pk 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    182   1.1       pk 		sc->sc_rev = NCR_VARIANT_ESP100;
    183   1.1       pk 	} else {
    184   1.1       pk 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    185   1.1       pk 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    186   1.1       pk 		sc->sc_cfg3 = 0;
    187   1.1       pk 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    188   1.1       pk 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    189   1.1       pk 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    190   1.1       pk 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    191   1.1       pk 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    192   1.1       pk 			sc->sc_rev = NCR_VARIANT_ESP100A;
    193   1.1       pk 		} else {
    194   1.1       pk 			/* NCRCFG2_FE enables > 64K transfers */
    195   1.1       pk 			sc->sc_cfg2 |= NCRCFG2_FE;
    196   1.1       pk 			sc->sc_cfg3 = 0;
    197   1.1       pk 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    198   1.1       pk 			sc->sc_rev = NCR_VARIANT_ESP200;
    199   1.1       pk 		}
    200   1.1       pk 	}
    201   1.1       pk 
    202   1.1       pk 	/*
    203   1.1       pk 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    204   1.1       pk 	 * XXX but it appears to have some dependency on what sort
    205   1.1       pk 	 * XXX of DMA we're hooked up to, etc.
    206   1.1       pk 	 */
    207   1.1       pk 
    208   1.1       pk 	/*
    209   1.1       pk 	 * This is the value used to start sync negotiations
    210   1.1       pk 	 * Note that the NCR register "SYNCTP" is programmed
    211   1.1       pk 	 * in "clocks per byte", and has a minimum value of 4.
    212   1.1       pk 	 * The SCSI period used in negotiation is one-fourth
    213   1.1       pk 	 * of the time (in nanoseconds) needed to transfer one byte.
    214   1.1       pk 	 * Since the chip's clock is given in MHz, we have the following
    215   1.1       pk 	 * formula: 4 * period = (1000 / freq) * 4
    216   1.1       pk 	 */
    217   1.1       pk 	sc->sc_minsync = 1000 / sc->sc_freq;
    218   1.1       pk 
    219   1.1       pk 	/*
    220   1.1       pk 	 * Alas, we must now modify the value a bit, because it's
    221  1.17      uwe 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    222  1.17      uwe 	 * in config register 3...
    223   1.1       pk 	 */
    224   1.1       pk 	switch (sc->sc_rev) {
    225   1.1       pk 	case NCR_VARIANT_ESP100:
    226   1.1       pk 		sc->sc_maxxfer = 64 * 1024;
    227   1.1       pk 		sc->sc_minsync = 0;	/* No synch on old chip? */
    228   1.1       pk 		break;
    229   1.1       pk 
    230   1.1       pk 	case NCR_VARIANT_ESP100A:
    231   1.1       pk 		sc->sc_maxxfer = 64 * 1024;
    232   1.1       pk 		/* Min clocks/byte is 5 */
    233   1.1       pk 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    234   1.1       pk 		break;
    235   1.1       pk 
    236   1.1       pk 	case NCR_VARIANT_ESP200:
    237   1.1       pk 		sc->sc_maxxfer = 16 * 1024 * 1024;
    238   1.1       pk 		/* XXX - do actually set FAST* bits */
    239   1.1       pk 		break;
    240   1.1       pk 	}
    241   1.1       pk 
    242   1.1       pk 	/* Establish interrupt channel */
    243  1.15       pk 	bus_intr_establish(esc->sc_bustag, oba->oba_pri, IPL_BIO,
    244  1.22  tsutsui 	    ncr53c9x_intr, sc);
    245   1.1       pk 
    246   1.1       pk 	/* register interrupt stats */
    247   1.6      cgd 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    248  1.22  tsutsui 	    device_xname(self), "intr");
    249   1.1       pk 
    250   1.1       pk 	/* Do the common parts of attachment. */
    251  1.10   bouyer 	sc->sc_adapter.adapt_minphys = minphys;
    252  1.10   bouyer 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    253  1.10   bouyer 	ncr53c9x_attach(sc);
    254   1.9   petrov 	sc->sc_features |= NCR_F_DMASELECT;
    255   1.1       pk }
    256   1.1       pk 
    257   1.1       pk /*
    258   1.1       pk  * Glue functions.
    259   1.1       pk  */
    260   1.1       pk 
    261  1.22  tsutsui static uint8_t
    262  1.17      uwe esp_read_reg(struct ncr53c9x_softc *sc, int reg)
    263   1.1       pk {
    264   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    265   1.1       pk 
    266  1.22  tsutsui 	return bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4);
    267   1.1       pk }
    268   1.1       pk 
    269  1.17      uwe static void
    270  1.22  tsutsui esp_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t v)
    271   1.1       pk {
    272   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    273   1.1       pk 
    274   1.1       pk 	bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
    275   1.1       pk }
    276   1.1       pk 
    277  1.17      uwe static int
    278  1.17      uwe esp_dma_isintr(struct ncr53c9x_softc *sc)
    279   1.1       pk {
    280   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    281   1.1       pk 
    282  1.22  tsutsui 	return DMA_ISINTR(esc->sc_dma);
    283   1.1       pk }
    284   1.1       pk 
    285  1.17      uwe static void
    286  1.17      uwe esp_dma_reset(struct ncr53c9x_softc *sc)
    287   1.1       pk {
    288   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    289   1.1       pk 
    290   1.1       pk 	DMA_RESET(esc->sc_dma);
    291   1.1       pk }
    292   1.1       pk 
    293  1.17      uwe static int
    294  1.17      uwe esp_dma_intr(struct ncr53c9x_softc *sc)
    295   1.1       pk {
    296   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    297   1.1       pk 
    298  1.22  tsutsui 	return DMA_INTR(esc->sc_dma);
    299   1.1       pk }
    300   1.1       pk 
    301  1.17      uwe static int
    302  1.22  tsutsui esp_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    303  1.22  tsutsui     int datain, size_t *dmasize)
    304   1.1       pk {
    305   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    306   1.1       pk 
    307  1.22  tsutsui 	return DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize);
    308   1.1       pk }
    309   1.1       pk 
    310  1.17      uwe static void
    311  1.17      uwe esp_dma_go(struct ncr53c9x_softc *sc)
    312   1.1       pk {
    313   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    314   1.1       pk 
    315   1.1       pk 	DMA_GO(esc->sc_dma);
    316   1.1       pk }
    317   1.1       pk 
    318  1.17      uwe static void
    319  1.17      uwe esp_dma_stop(struct ncr53c9x_softc *sc)
    320   1.1       pk {
    321   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    322  1.17      uwe 	uint32_t csr;
    323   1.1       pk 
    324   1.1       pk 	csr = L64854_GCSR(esc->sc_dma);
    325   1.1       pk 	csr &= ~D_EN_DMA;
    326   1.1       pk 	L64854_SCSR(esc->sc_dma, csr);
    327   1.1       pk }
    328   1.1       pk 
    329  1.17      uwe static int
    330  1.17      uwe esp_dma_isactive(struct ncr53c9x_softc *sc)
    331   1.1       pk {
    332   1.1       pk 	struct esp_softc *esc = (struct esp_softc *)sc;
    333   1.1       pk 
    334  1.22  tsutsui 	return DMA_ISACTIVE(esc->sc_dma);
    335   1.1       pk }
    336