Home | History | Annotate | Line # | Download | only in dev
sc_mbmem.c revision 1.11.66.1
      1 /*	$NetBSD: sc_mbmem.c,v 1.11.66.1 2008/05/16 02:23:16 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, David Jones, Gordon W. Ross, and Matthew Fredette.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This file contains only the machine-dependent parts of the
     34  * Sun-2 SCSI driver.  (Autoconfig stuff and DMA functions.)
     35  * The machine-independent parts are in sunscpal.c
     36  *
     37  * Supported hardware includes:
     38  * Sun SCSI-2 on Multibus (Sun2/120,Sun2/170)
     39  * Sun SCSI-2 on VME (Sun2/50,Sun3/160,Sun3/260,others?)
     40  *
     41  * Credits, history:
     42  *
     43  * Matthew Fredette took revision 1.15 of si_vme.c, and then heavily
     44  * modified it to support the Sun sc.  The remaining credits
     45  * are from si_vme.c:
     46  *
     47  * David Jones wrote the initial version of this module, which
     48  * included support for the VME adapter only. (no reselection).
     49  *
     50  * Gordon Ross added support for the OBIO adapter, and re-worked
     51  * both the VME and OBIO code to support disconnect/reselect.
     52  * (Required figuring out the hardware "features" noted above.)
     53  *
     54  * The autoconfiguration boilerplate came from Adam Glass.
     55  */
     56 
     57 /*****************************************************************
     58  * Multibus functions for sc
     59  ****************************************************************/
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: sc_mbmem.c,v 1.11.66.1 2008/05/16 02:23:16 yamt Exp $");
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/errno.h>
     67 #include <sys/kernel.h>
     68 #include <sys/malloc.h>
     69 #include <sys/device.h>
     70 #include <sys/buf.h>
     71 #include <sys/proc.h>
     72 #include <sys/user.h>
     73 
     74 #include <dev/scsipi/scsi_all.h>
     75 #include <dev/scsipi/scsipi_all.h>
     76 #include <dev/scsipi/scsipi_debug.h>
     77 #include <dev/scsipi/scsiconf.h>
     78 
     79 #include <machine/autoconf.h>
     80 #include <machine/bus.h>
     81 
     82 /* #define DEBUG XXX */
     83 
     84 #include <dev/ic/sunscpalreg.h>
     85 #include <dev/ic/sunscpalvar.h>
     86 
     87 /* Yes, we use a VME header file. */
     88 #include <dev/vme/screg.h>
     89 
     90 /*
     91  * Transfers smaller than this are done using PIO
     92  * (on assumption they're not worth DMA overhead)
     93  */
     94 #define	MIN_DMA_LEN 128
     95 
     96 /*
     97  * New-style autoconfig attachment
     98  */
     99 
    100 static int	sunsc_mbmem_match(struct device *, struct cfdata *, void *);
    101 static void	sunsc_mbmem_attach(struct device *, struct device *, void *);
    102 static int	sunsc_mbmem_intr(void *);
    103 
    104 CFATTACH_DECL(sc_mbmem, sizeof(struct sunscpal_softc),
    105     sunsc_mbmem_match, sunsc_mbmem_attach, NULL, NULL);
    106 
    107 /*
    108  * Options for parity, DMA, and interrupts.
    109  */
    110 int sunsc_mbmem_options = 0x00;
    111 
    112 static int
    113 sunsc_mbmem_match(struct device *parent, struct cfdata *cf, void *aux)
    114 {
    115 	struct mbmem_attach_args *mbma = aux;
    116 	bus_space_handle_t bh;
    117 	int matched;
    118 
    119 	/* No default Multibus address. */
    120 	if (mbma->mbma_paddr == -1)
    121 		return (0);
    122 
    123 	/* Make sure there is something there... */
    124 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
    125 			  0, &bh))
    126 		return (0);
    127 	matched = (bus_space_peek_2(mbma->mbma_bustag, bh, SCREG_ICR, NULL) == 0);
    128 	bus_space_unmap(mbma->mbma_bustag, bh, SCREG_BANK_SZ);
    129 	if (!matched)
    130 		return (0);
    131 
    132 	/* Default interrupt priority. */
    133 	if (mbma->mbma_pri == -1)
    134 		mbma->mbma_pri = 2;
    135 
    136 	return (1);
    137 }
    138 
    139 static void
    140 sunsc_mbmem_attach(struct device *parent, struct device *self, void *args)
    141 {
    142 	struct sunscpal_softc *sc = (void *) self;
    143 	struct cfdata *cf = device_cfdata(self);
    144 	struct mbmem_attach_args *mbma = args;
    145 	int i;
    146 
    147 	/* Map in the device. */
    148 	sc->sunscpal_regt = mbma->mbma_bustag;
    149 	sc->sunscpal_dmat = mbma->mbma_dmatag;
    150 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
    151 			  0, &sc->sunscpal_regh))
    152 		panic("sunsc_mbmem_attach: can't map");
    153 
    154 	/* Device register offsets. */
    155 	sc->sunscpal_data = SCREG_DATA;
    156 	sc->sunscpal_cmd_stat = SCREG_CMD_STAT;
    157 	sc->sunscpal_icr = SCREG_ICR;
    158 	sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H;
    159 	sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L;
    160 	sc->sunscpal_dma_count = SCREG_DMA_COUNT;
    161 	sc->sunscpal_intvec = SCREG_INTVEC;
    162 
    163 	/* Allocate DMA handles. */
    164 	i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle);
    165 	sc->sc_dma_handles = (sunscpal_dma_handle_t)
    166 		malloc(i, M_DEVBUF, M_WAITOK);
    167 	if (sc->sc_dma_handles == NULL)
    168 		panic("sc: DMA handles malloc failed");
    169 	for (i = 0; i < SUNSCPAL_OPENINGS; i++)
    170 		if (bus_dmamap_create(sc->sunscpal_dmat, SUNSCPAL_MAX_DMA_LEN,
    171 				      1, SUNSCPAL_MAX_DMA_LEN,
    172 				      0, BUS_DMA_WAITOK, &sc->sc_dma_handles[i].
    173 dh_dmamap) != 0)
    174 			panic("sc: DMA map create failed");
    175 
    176 	/* Miscellaneous. */
    177 	sc->sc_min_dma_len = MIN_DMA_LEN;
    178 	sc->sc_rev = SUNSCPAL_VARIANT_501_1006;
    179 
    180 	/* Attach interrupt handler. */
    181 	bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_BIO, 0,
    182 			   sunsc_mbmem_intr, sc);
    183 
    184 	/* Do the common attach stuff. */
    185 	sunscpal_attach(sc, (cf->cf_flags ? cf->cf_flags : sunsc_mbmem_options));
    186 }
    187 
    188 static int
    189 sunsc_mbmem_intr(void *arg)
    190 {
    191 	struct sunscpal_softc *sc = arg;
    192 	int claimed;
    193 
    194 	claimed = sunscpal_intr(sc);
    195 #ifdef	DEBUG
    196 	if (!claimed) {
    197 		printf("sunsc_intr: spurious from SBC\n");
    198 	}
    199 #endif
    200 	/* Yes, we DID cause this interrupt. */
    201 	claimed = 1;
    202 
    203 	return (claimed);
    204 }
    205