Home | History | Annotate | Line # | Download | only in vsa
asc_vsbus.c revision 1.1
      1 /*	$NetBSD: asc_vsbus.c,v 1.1 2000/03/04 00:24:06 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 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.
      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 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     40 
     41 __KERNEL_RCSID(0, "$NetBSD: asc_vsbus.c,v 1.1 2000/03/04 00:24:06 matt Exp $");
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/errno.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/device.h>
     50 #include <sys/buf.h>
     51 #include <sys/proc.h>
     52 #include <sys/user.h>
     53 #include <sys/reboot.h>
     54 #include <sys/queue.h>
     55 
     56 #include <dev/scsipi/scsi_all.h>
     57 #include <dev/scsipi/scsipi_all.h>
     58 #include <dev/scsipi/scsiconf.h>
     59 #include <dev/scsipi/scsi_message.h>
     60 
     61 #include <machine/bus.h>
     62 
     63 #include <dev/ic/ncr53c9xreg.h>
     64 #include <dev/ic/ncr53c9xvar.h>
     65 
     66 #include <machine/cpu.h>
     67 #include <machine/sid.h>
     68 #include <machine/rpb.h>
     69 #include <machine/scb.h>
     70 #include <machine/vsbus.h>
     71 
     72 struct asc_vsbus_softc {
     73 	struct ncr53c9x_softc sc_ncr53c9x;	/* Must be first */
     74 	bus_space_tag_t sc_bst;			/* bus space tag */
     75 	bus_space_handle_t sc_bsh;		/* bus space handle */
     76 	bus_dma_tag_t sc_dmat;			/* bus dma tag */
     77 	bus_dmamap_t sc_dmamap;
     78 	caddr_t *sc_dmaaddr;
     79 	size_t *sc_dmalen;
     80 	size_t sc_dmasize;
     81 	unsigned int sc_flags;
     82 #define	ASC_DMAACTIVE		0x0001
     83 #define	ASC_ISWRITE		0x0002
     84 #define	ASC_INTR		0x0004
     85 };
     86 
     87 #define	ASC_REG_ADR		0x0000
     88 #define	ASC_REG_DIR		0x000C
     89 #define	ASC_REG_NCR		0x0080
     90 #define	ASC_REG_END		0x00B0
     91 
     92 #define	ASC_DIR_TO_MEMORY	0x0000
     93 #define	ASC_DIR_FROM_MEMORY	0x0001
     94 
     95 #define	ASC_MAXXFERSIZE		65536
     96 #define	ASC_FREQUENCY		20000000
     97 
     98 static int asc_vsbus_match __P((struct device *, struct cfdata *, void *));
     99 static void asc_vsbus_attach __P((struct device *, struct device *, void *));
    100 static void ascintr __P((void *));
    101 
    102 struct cfattach asc_vsbus_ca = {
    103 	sizeof(struct asc_vsbus_softc), asc_vsbus_match, asc_vsbus_attach
    104 };
    105 
    106 static struct scsipi_device asc_vsbus_dev = {
    107 	NULL,			/* Use the default error handler */
    108 	NULL,			/* have a queue, served by this */
    109 	NULL,			/* have no async handler */
    110 	NULL,			/* use the default done handler */
    111 };
    112 
    113 /*
    114  * Functions and the switch for the MI code
    115  */
    116 static u_char	asc_vsbus_read_reg __P((struct ncr53c9x_softc *, int));
    117 static void	asc_vsbus_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    118 static int	asc_vsbus_dma_isintr __P((struct ncr53c9x_softc *));
    119 static void	asc_vsbus_dma_reset __P((struct ncr53c9x_softc *));
    120 static int	asc_vsbus_dma_intr __P((struct ncr53c9x_softc *));
    121 static int	asc_vsbus_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    122 		    size_t *, int, size_t *));
    123 static void	asc_vsbus_dma_go __P((struct ncr53c9x_softc *));
    124 static void	asc_vsbus_dma_stop __P((struct ncr53c9x_softc *));
    125 static int	asc_vsbus_dma_isactive __P((struct ncr53c9x_softc *));
    126 static void	asc_vsbus_clear_latched_intr __P((struct ncr53c9x_softc *));
    127 
    128 static struct ncr53c9x_glue asc_vsbus_glue = {
    129 	asc_vsbus_read_reg,
    130 	asc_vsbus_write_reg,
    131 	asc_vsbus_dma_isintr,
    132 	asc_vsbus_dma_reset,
    133 	asc_vsbus_dma_intr,
    134 	asc_vsbus_dma_setup,
    135 	asc_vsbus_dma_go,
    136 	asc_vsbus_dma_stop,
    137 	asc_vsbus_dma_isactive,
    138 	asc_vsbus_clear_latched_intr,
    139 };
    140 
    141 static int
    142 asc_vsbus_match( struct device *parent, struct cfdata *cf, void *aux)
    143 {
    144 	struct vsbus_attach_args *va = aux;
    145 	int dummy;
    146 	volatile int *ncr_regs;
    147 
    148 	if (vax_boardtype != VAX_BTYP_46
    149 	   && vax_boardtype != VAX_BTYP_48
    150 	   && vax_boardtype != VAX_BTYP_49)
    151 		return 0;
    152 
    153 	printf("asc_vsbus_match\n");
    154 
    155 	ncr_regs = (volatile int *) va->va_addr;
    156 
    157 	/*  *** need to generate an interrupt here
    158 	 * From trial and error, I've determined that an INT is generated
    159 	 * only when the following sequence of events occurs:
    160 	 *   1) The interrupt status register (0x05) must be read.
    161 	 *   2) SCSI bus reset interrupt must be enabled
    162 	 *   3) SCSI bus reset command must be sent
    163 	 *   4) NOP command must be sent
    164 	 */
    165 
    166         dummy = ncr_regs[NCR_INTR]; /* clear status register */
    167         ncr_regs[NCR_CFG1] = 0x07; /* we're ID 7, turn on INT for SCSI reset */
    168         ncr_regs[NCR_CMD] = NCRCMD_RSTSCSI; /* send the reset */
    169         ncr_regs[NCR_CMD] = NCRCMD_NOP; /* send a NOP */
    170 	DELAY(10000);
    171 
    172 	return (ncr_regs[NCR_INTR] & NCRINTR_FC) != 0;
    173 }
    174 
    175 
    176 /*
    177  * Attach this instance, and then all the sub-devices
    178  */
    179 static void
    180 asc_vsbus_attach(struct device *parent, struct device *self, void *aux)
    181 {
    182 	struct vsbus_attach_args *va = aux;
    183 	struct asc_vsbus_softc *asc = (void *)self;
    184 	struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
    185 	int error;
    186 
    187 	/*
    188 	 * Set up glue for MI code early; we use some of it here.
    189 	 */
    190 	sc->sc_glue = &asc_vsbus_glue;
    191 
    192 	asc->sc_bst = va->va_iot;
    193 	asc->sc_dmat = va->va_dmat;
    194 
    195 	error = bus_space_map(asc->sc_bst, va->va_paddr - ASC_REG_NCR,
    196 	    ASC_REG_END, 0, &asc->sc_bsh);
    197 	if (error) {
    198 		printf(": failed to map registers: error=%d\n", error);
    199 		return;
    200 	}
    201 
    202 	sc->sc_id = 7;	/* XXX need to get this from VMB */
    203 	sc->sc_freq = ASC_FREQUENCY;
    204 
    205 	/* gimme Mhz */
    206 	sc->sc_freq /= 1000000;
    207 
    208 	scb_vecalloc(va->va_cvec, ascintr, asc, SCB_ISTACK);
    209 
    210 	/*
    211 	 * XXX More of this should be in ncr53c9x_attach(), but
    212 	 * XXX should we really poke around the chip that much in
    213 	 * XXX the MI code?  Think about this more...
    214 	 */
    215 
    216 	/*
    217 	 * Set up static configuration info.
    218 	 */
    219 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    220 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    221 	sc->sc_cfg3 = NCRCFG3_CDB;
    222 	sc->sc_rev = NCR_VARIANT_NCR53C94;
    223 
    224 	/*
    225 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    226 	 * XXX but it appears to have some dependency on what sort
    227 	 * XXX of DMA we're hooked up to, etc.
    228 	 */
    229 
    230 	/*
    231 	 * This is the value used to start sync negotiations
    232 	 * Note that the NCR register "SYNCTP" is programmed
    233 	 * in "clocks per byte", and has a minimum value of 4.
    234 	 * The SCSI period used in negotiation is one-fourth
    235 	 * of the time (in nanoseconds) needed to transfer one byte.
    236 	 * Since the chip's clock is given in MHz, we have the following
    237 	 * formula: 4 * period = (1000 / freq) * 4
    238 	 */
    239 	sc->sc_minsync = (1000 / sc->sc_freq);
    240 	sc->sc_maxxfer = 64 * 1024;
    241 
    242 	/* Do the common parts of attachment. */
    243 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    244 	sc->sc_adapter.scsipi_minphys = minphys;
    245 	ncr53c9x_attach(sc, &asc_vsbus_dev);
    246 
    247 	/*
    248 	 * Register this device as boot device if we booted from it.
    249 	 * This will fail if there are more than one le in a machine,
    250 	 * fortunately there may be only one.
    251 	 */
    252 	if (B_TYPE(bootdev) == BDEV_SD)
    253 		booted_from = self;
    254 }
    255 
    256 static void
    257 ascintr(void *arg)
    258 {
    259 	struct asc_vsbus_softc *asc = arg;
    260 	asc->sc_flags |= ASC_INTR;
    261 	ncr53c9x_intr(&asc->sc_ncr53c9x);
    262 	asc->sc_flags &= ~ASC_INTR;
    263 }
    264 
    265 /*
    266  * Glue functions.
    267  */
    268 
    269 static u_char
    270 asc_vsbus_read_reg(struct ncr53c9x_softc *sc, int reg)
    271 {
    272 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    273 	u_char v;
    274 
    275 	v = bus_space_read_4(asc->sc_bst, asc->sc_bsh,
    276 	    ASC_REG_NCR + reg * sizeof(u_int32_t)) & 0xff;
    277 
    278 	return (v);
    279 }
    280 
    281 static void
    282 asc_vsbus_write_reg(sc, reg, val)
    283 	struct ncr53c9x_softc *sc;
    284 	int reg;
    285 	u_char val;
    286 {
    287 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    288 
    289 	bus_space_write_4(asc->sc_bst, asc->sc_bsh,
    290 	    ASC_REG_NCR + reg * sizeof(u_int32_t), val);
    291 }
    292 
    293 static int
    294 asc_vsbus_dma_isintr(sc)
    295 	struct ncr53c9x_softc *sc;
    296 {
    297 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    298 	int isintr = (asc->sc_flags & ASC_INTR);
    299 	asc->sc_flags &= ~ASC_INTR;
    300 	return isintr;		/* this is the only reason */
    301 }
    302 
    303 static void
    304 asc_vsbus_dma_reset(sc)
    305 	struct ncr53c9x_softc *sc;
    306 {
    307 #if 0
    308 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    309 #endif
    310 
    311 	/* nothing to do */
    312 }
    313 
    314 static int
    315 asc_vsbus_dma_intr(sc)
    316 	struct ncr53c9x_softc *sc;
    317 {
    318 #if 0
    319 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    320 #endif
    321 
    322 	return 0;
    323 }
    324 
    325 static int
    326 asc_vsbus_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
    327 		    int datain, size_t *dmasize)
    328 {
    329 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    330 
    331 	asc->sc_dmaaddr = addr;
    332 	asc->sc_dmalen = len;
    333 	if (datain) {
    334 		asc->sc_flags |= ASC_ISWRITE;
    335 	} else {
    336 		asc->sc_flags &= ~ASC_ISWRITE;
    337 	}
    338 
    339         NCR_DMA(("%s: start %d@%p,%d\n", sc->sc_dev.dv_xname,
    340                 (int)*asc->sc_dmalen, *asc->sc_dmaaddr, (asc->sc_flags & ASC_ISWRITE)));
    341 	*dmasize = asc->sc_dmasize = min(*dmasize, ASC_MAXXFERSIZE);
    342 
    343 	if (asc->sc_dmasize) {
    344 		if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
    345 				*asc->sc_dmaaddr, asc->sc_dmasize,
    346 				NULL /* kernel address */,
    347 				BUS_DMA_NOWAIT))
    348 			panic("%s: cannot load dma map", sc->sc_dev.dv_xname);
    349 		bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    350 				0, asc->sc_dmasize, datain
    351 					? BUS_DMASYNC_PREREAD
    352 					: BUS_DMASYNC_PREWRITE);
    353 		bus_space_write_4(asc->sc_bst, asc->sc_bsh, ASC_REG_ADR,
    354 				  asc->sc_dmamap->dm_segs[0].ds_addr);
    355 		bus_space_write_4(asc->sc_bst, asc->sc_bsh, ASC_REG_DIR,
    356 				  datain ? 0 : 1);
    357 	}
    358 
    359 
    360 	return 0;
    361 }
    362 
    363 static void
    364 asc_vsbus_dma_go(struct ncr53c9x_softc *sc)
    365 {
    366 #if 0
    367 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    368 #endif
    369 
    370 	/* Nothing to do */
    371 }
    372 
    373 static void
    374 asc_vsbus_dma_stop(struct ncr53c9x_softc *sc)
    375 {
    376 #if 0
    377 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    378 #endif
    379 
    380 	/*
    381 	 * XXX STOP DMA HERE!
    382 	 */
    383 }
    384 
    385 static int
    386 asc_vsbus_dma_isactive(struct ncr53c9x_softc *sc)
    387 {
    388 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    389 
    390 	return (asc->sc_flags & ASC_DMAACTIVE) != 0;
    391 }
    392 
    393 static void
    394 asc_vsbus_clear_latched_intr(struct ncr53c9x_softc *sc)
    395 {
    396 #if 0
    397 	struct asc_vsbus_softc *asc = (struct asc_vsbus_softc *)sc;
    398 #endif
    399 }
    400