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