Home | History | Annotate | Line # | Download | only in hpc
wdsc.c revision 1.1
      1  1.1  wdk /*	$NetBSD: wdsc.c,v 1.1 2001/08/19 03:16:22 wdk Exp $	*/
      2  1.1  wdk 
      3  1.1  wdk /*
      4  1.1  wdk  * Copyright (c) 2001 Wayne Knowles
      5  1.1  wdk  * All rights reserved.
      6  1.1  wdk  *
      7  1.1  wdk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  wdk  * by Wayne Knowles
      9  1.1  wdk  *
     10  1.1  wdk  * Redistribution and use in source and binary forms, with or without
     11  1.1  wdk  * modification, are permitted provided that the following conditions
     12  1.1  wdk  * are met:
     13  1.1  wdk  * 1. Redistributions of source code must retain the above copyright
     14  1.1  wdk  *    notice, this list of conditions and the following disclaimer.
     15  1.1  wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  wdk  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  wdk  *    documentation and/or other materials provided with the distribution.
     18  1.1  wdk  * 3. All advertising materials mentioning features or use of this software
     19  1.1  wdk  *    must display the following acknowledgement:
     20  1.1  wdk  *        This product includes software developed by the NetBSD
     21  1.1  wdk  *        Foundation, Inc. and its contributors.
     22  1.1  wdk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  wdk  *    contributors may be used to endorse or promote products derived
     24  1.1  wdk  *    from this software without specific prior written permission.
     25  1.1  wdk  *
     26  1.1  wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  wdk  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  wdk  */
     38  1.1  wdk 
     39  1.1  wdk /*
     40  1.1  wdk  * TODO:
     41  1.1  wdk  *
     42  1.1  wdk  *    Support for 2nd SCSI controller
     43  1.1  wdk  *    evcnt(9) hooks
     44  1.1  wdk  *    remove struct dma_chain
     45  1.1  wdk  *    dma{setup,stop,go} API similar to NCR93c9x MI driver
     46  1.1  wdk  *    improve hpcdma functions
     47  1.1  wdk  *    cleanup softc stuff
     48  1.1  wdk  */
     49  1.1  wdk 
     50  1.1  wdk #include <sys/param.h>
     51  1.1  wdk #include <sys/systm.h>
     52  1.1  wdk #include <sys/kernel.h>
     53  1.1  wdk #include <sys/device.h>
     54  1.1  wdk #include <sys/buf.h>
     55  1.1  wdk 
     56  1.1  wdk #include <dev/scsipi/scsi_all.h>
     57  1.1  wdk #include <dev/scsipi/scsipi_all.h>
     58  1.1  wdk #include <dev/scsipi/scsiconf.h>
     59  1.1  wdk 
     60  1.1  wdk #include <machine/cpu.h>
     61  1.1  wdk #include <machine/bus.h>
     62  1.1  wdk #include <machine/autoconf.h>
     63  1.1  wdk 
     64  1.1  wdk #include <sgimips/hpc/hpcvar.h>
     65  1.1  wdk #include <sgimips/hpc/hpcreg.h>
     66  1.1  wdk #include <sgimips/hpc/hpcdma.h>
     67  1.1  wdk 
     68  1.1  wdk #include <sgimips/hpc/sbicreg.h>
     69  1.1  wdk #include <sgimips/hpc/sbicvar.h>
     70  1.1  wdk 
     71  1.1  wdk struct wdsc_softc {
     72  1.1  wdk 	struct sbic_softc	sc_sbic; /* Must be first */
     73  1.1  wdk 
     74  1.1  wdk 	struct hpc_dma_softc	sc_hpcdma;
     75  1.1  wdk };
     76  1.1  wdk 
     77  1.1  wdk 
     78  1.1  wdk void    wdsc_attach __P((struct device *, struct device *, void *));
     79  1.1  wdk int     wdsc_match  __P((struct device *, struct cfdata *, void *));
     80  1.1  wdk 
     81  1.1  wdk struct cfattach wdsc_ca = {
     82  1.1  wdk 	sizeof(struct wdsc_softc), wdsc_match, wdsc_attach
     83  1.1  wdk };
     84  1.1  wdk 
     85  1.1  wdk extern struct cfdriver wdsc_cd;
     86  1.1  wdk 
     87  1.1  wdk void    wdsc_enintr     __P((struct sbic_softc *));
     88  1.1  wdk int     wdsc_dmasetup   __P((struct sbic_softc *, bus_dmamap_t, int));
     89  1.1  wdk int     wdsc_dmago      __P((struct sbic_softc *));
     90  1.1  wdk void    wdsc_dmastop    __P((struct sbic_softc *));
     91  1.1  wdk int     wdsc_dmaintr    __P((void *));
     92  1.1  wdk int     wdsc_scsiintr   __P((void *));
     93  1.1  wdk 
     94  1.1  wdk #define MAX_SCSI_XFER   (512*1024)
     95  1.1  wdk #define MAX_SEG_SZ	8192
     96  1.1  wdk #define	MAX_DMA_SZ	MAX_SCSI_XFER
     97  1.1  wdk #define	DMA_SEGS	(MAX_DMA_SZ/MAX_SEG_SZ)
     98  1.1  wdk 
     99  1.1  wdk /*
    100  1.1  wdk  * Match for SCSI devices on the onboard WD33C93 chip
    101  1.1  wdk  */
    102  1.1  wdk int
    103  1.1  wdk wdsc_match(pdp, cf, auxp)
    104  1.1  wdk     struct device *pdp;
    105  1.1  wdk     struct cfdata *cf;
    106  1.1  wdk     void *auxp;
    107  1.1  wdk {
    108  1.1  wdk     struct hpc_attach_args *haa = auxp;
    109  1.1  wdk 
    110  1.1  wdk     if (strcmp(haa->ha_name, wdsc_cd.cd_name))
    111  1.1  wdk 	return (0);
    112  1.1  wdk     return (1);
    113  1.1  wdk }
    114  1.1  wdk 
    115  1.1  wdk /*
    116  1.1  wdk  * Attach the wdsc driver
    117  1.1  wdk  */
    118  1.1  wdk void
    119  1.1  wdk wdsc_attach(pdp, dp, auxp)
    120  1.1  wdk     struct device *pdp, *dp;
    121  1.1  wdk     void *auxp;
    122  1.1  wdk {
    123  1.1  wdk     struct sbic_softc *sc = (void *)dp;
    124  1.1  wdk     struct wdsc_softc *wdsc = (void *)dp;
    125  1.1  wdk     struct hpc_attach_args *haa = auxp;
    126  1.1  wdk     int err;
    127  1.1  wdk 
    128  1.1  wdk     sc->sc_regt = haa->ha_iot;
    129  1.1  wdk     sc->sc_dmat = haa->ha_dmat;
    130  1.1  wdk 
    131  1.1  wdk     if ((err = bus_space_subregion(haa->ha_iot, haa->ha_ioh,
    132  1.1  wdk 					HPC_SCSI0_DEVREGS,
    133  1.1  wdk 	     				HPC_SCSI0_DEVREGS_SIZE,
    134  1.1  wdk 	     				&sc->sc_regh)) != 0) {
    135  1.1  wdk 		printf(": unable to map WD33C93 regs, err=%d\n", err);
    136  1.1  wdk 		goto fail;
    137  1.1  wdk     }
    138  1.1  wdk 
    139  1.1  wdk     if (bus_dmamap_create(sc->sc_dmat, MAX_DMA_SZ,
    140  1.1  wdk 			      DMA_SEGS, MAX_SEG_SZ, MAX_SEG_SZ,
    141  1.1  wdk 			      BUS_DMA_WAITOK,
    142  1.1  wdk 			      &sc->sc_dmamap) != 0) {
    143  1.1  wdk 		printf(": failed to create dmamap\n");
    144  1.1  wdk 		goto fail;
    145  1.1  wdk     }
    146  1.1  wdk 
    147  1.1  wdk     hpcdma_init(haa, &wdsc->sc_hpcdma, DMA_SEGS);
    148  1.1  wdk 
    149  1.1  wdk     sc->sc_enintr   = wdsc_enintr;
    150  1.1  wdk     sc->sc_dmasetup = wdsc_dmasetup;
    151  1.1  wdk     sc->sc_dmago    = wdsc_dmago;
    152  1.1  wdk     sc->sc_dmastop  = wdsc_dmastop;
    153  1.1  wdk 
    154  1.1  wdk     sc->sc_adapter.adapt_dev = &sc->sc_dev;
    155  1.1  wdk     sc->sc_adapter.adapt_nchannels = 1;
    156  1.1  wdk     sc->sc_adapter.adapt_openings = 7;
    157  1.1  wdk     sc->sc_adapter.adapt_max_periph = 1;
    158  1.1  wdk     sc->sc_adapter.adapt_ioctl = NULL;
    159  1.1  wdk     sc->sc_adapter.adapt_minphys = minphys;
    160  1.1  wdk     sc->sc_adapter.adapt_request = sbic_scsi_request;
    161  1.1  wdk     sc->sc_channel.chan_adapter = &sc->sc_adapter;
    162  1.1  wdk     sc->sc_channel.chan_bustype = &scsi_bustype;
    163  1.1  wdk     sc->sc_channel.chan_channel = 0;
    164  1.1  wdk     sc->sc_channel.chan_ntargets = 8;
    165  1.1  wdk     sc->sc_channel.chan_nluns = 8;
    166  1.1  wdk     sc->sc_channel.chan_id = 7;
    167  1.1  wdk 
    168  1.1  wdk     printf(": WD33C93 SCSI, target %d\n", sc->sc_channel.chan_id);
    169  1.1  wdk 
    170  1.1  wdk     /*
    171  1.1  wdk      * Controller clock frequency * 10
    172  1.1  wdk      */
    173  1.1  wdk     sc->sc_clkfreq = 200;
    174  1.1  wdk 
    175  1.1  wdk     /*
    176  1.1  wdk      * Initialise the hardware
    177  1.1  wdk      */
    178  1.1  wdk     sbicinit(sc);
    179  1.1  wdk 
    180  1.1  wdk     /* XXX: 1 = IRQ_LOCAL0 + 1 */
    181  1.1  wdk     if ((cpu_intr_establish(1, IPL_BIO, wdsc_scsiintr, sc)) == NULL) {
    182  1.1  wdk 		printf(": unable to establish interrupt!\n");
    183  1.1  wdk 		goto fail;
    184  1.1  wdk     }
    185  1.1  wdk 
    186  1.1  wdk     (void)config_found(dp, &sc->sc_channel, scsiprint);
    187  1.1  wdk 
    188  1.1  wdk fail:
    189  1.1  wdk     return;
    190  1.1  wdk }
    191  1.1  wdk 
    192  1.1  wdk /*
    193  1.1  wdk  * Enable DMA interrupts
    194  1.1  wdk  */
    195  1.1  wdk void
    196  1.1  wdk wdsc_enintr(dev)
    197  1.1  wdk     struct sbic_softc *dev;
    198  1.1  wdk {
    199  1.1  wdk     dev->sc_flags |= SBICF_INTR;
    200  1.1  wdk }
    201  1.1  wdk 
    202  1.1  wdk /*
    203  1.1  wdk  * Prime the hardware for a DMA transfer
    204  1.1  wdk  */
    205  1.1  wdk int
    206  1.1  wdk wdsc_dmasetup(dev, dmamap, flags)
    207  1.1  wdk     struct sbic_softc *dev;
    208  1.1  wdk     bus_dmamap_t dmamap;
    209  1.1  wdk     int flags;
    210  1.1  wdk {
    211  1.1  wdk     struct hpc_dma_softc *dsc = &((struct wdsc_softc *)dev)->sc_hpcdma;
    212  1.1  wdk     struct sbic_acb *acb = dev->sc_nexus;
    213  1.1  wdk     int     s;
    214  1.1  wdk     int     count, err;
    215  1.1  wdk     void   *vaddr;
    216  1.1  wdk 
    217  1.1  wdk     KASSERT((dsc->sc_flags & HPC_DMA_ACTIVE) == 0);
    218  1.1  wdk 
    219  1.1  wdk     vaddr = acb->sc_kv.dc_addr;
    220  1.1  wdk     count = acb->sc_kv.dc_count;
    221  1.1  wdk 
    222  1.1  wdk     if (count) {
    223  1.1  wdk 	    s = splbio();
    224  1.1  wdk 
    225  1.1  wdk 	    /* have dmamap for the transfering addresses */
    226  1.1  wdk 	    if ((err=bus_dmamap_load(dev->sc_dmat, dev->sc_dmamap,
    227  1.1  wdk 				vaddr, count,
    228  1.1  wdk 				NULL /* kernel address */,
    229  1.1  wdk 				BUS_DMA_NOWAIT)) != 0)
    230  1.1  wdk 		    panic("%s: bus_dmamap_load err=%d",
    231  1.1  wdk 			  dev->sc_dev.dv_xname, err);
    232  1.1  wdk 
    233  1.1  wdk 	    dev->sc_flags |= SBICF_DMALOADED; /* XXX - Move to MD */
    234  1.1  wdk 	    dev->sc_flags |= SBICF_INTR;
    235  1.1  wdk 
    236  1.1  wdk 	    hpcdma_sglist_create(dsc, dev->sc_dmamap);
    237  1.1  wdk 
    238  1.1  wdk 	    dsc->sc_dmacmd = HPC_DMACTL_ACTIVE;	/* XXX - remove tests in MI */
    239  1.1  wdk 	    if (flags & ACB_DATAIN) {
    240  1.1  wdk 		    dsc->sc_flags  |= HPC_DMA_READ;
    241  1.1  wdk 	    } else {
    242  1.1  wdk 		    dsc->sc_dmacmd |= HPC_DMACTL_DIR;
    243  1.1  wdk 		    dsc->sc_flags  &= ~HPC_DMA_READ;
    244  1.1  wdk 	    }
    245  1.1  wdk 	    splx(s);
    246  1.1  wdk     }
    247  1.1  wdk     return(count);
    248  1.1  wdk }
    249  1.1  wdk 
    250  1.1  wdk /*
    251  1.1  wdk  * Prime the hardware for the next DMA transfer
    252  1.1  wdk  */
    253  1.1  wdk int
    254  1.1  wdk wdsc_dmago(dev)
    255  1.1  wdk     struct sbic_softc *dev;
    256  1.1  wdk {
    257  1.1  wdk     struct hpc_dma_softc *dsc = &((struct wdsc_softc *)dev)->sc_hpcdma;
    258  1.1  wdk 
    259  1.1  wdk     if (dev->sc_tcnt == 0) {
    260  1.1  wdk 	    return(0);
    261  1.1  wdk     }
    262  1.1  wdk 
    263  1.1  wdk     KASSERT((dsc->sc_flags & HPC_DMA_ACTIVE) == 0);
    264  1.1  wdk     dsc->sc_flags |= HPC_DMA_ACTIVE;
    265  1.1  wdk 
    266  1.1  wdk     bus_dmamap_sync(dev->sc_dmat, dev->sc_dmamap, 0, dev->sc_tcnt,
    267  1.1  wdk 		BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    268  1.1  wdk 
    269  1.1  wdk     hpcdma_cntl(dsc, dsc->sc_dmacmd);	/* Thunderbirds are go! */
    270  1.1  wdk 
    271  1.1  wdk     return(dev->sc_dmamap->dm_mapsize);
    272  1.1  wdk }
    273  1.1  wdk 
    274  1.1  wdk /*
    275  1.1  wdk  * Stop DMA, and disable interrupts
    276  1.1  wdk  */
    277  1.1  wdk void
    278  1.1  wdk wdsc_dmastop(dev)
    279  1.1  wdk     struct sbic_softc *dev;
    280  1.1  wdk {
    281  1.1  wdk     struct hpc_dma_softc *dsc = &((struct wdsc_softc *)dev)->sc_hpcdma;
    282  1.1  wdk 
    283  1.1  wdk     if (dsc->sc_flags & HPC_DMA_ACTIVE) {
    284  1.1  wdk 	    if (dsc->sc_flags & HPC_DMA_READ)
    285  1.1  wdk 		    hpcdma_flush(dsc);
    286  1.1  wdk 	    hpcdma_cntl(dsc, 0);	/* Stop DMA */
    287  1.1  wdk 	    bus_dmamap_sync(dev->sc_dmat, dev->sc_dmamap, 0,
    288  1.1  wdk 		dev->sc_dmamap->dm_mapsize,
    289  1.1  wdk 		BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    290  1.1  wdk 	    dsc->sc_flags &= ~HPC_DMA_ACTIVE;
    291  1.1  wdk     }
    292  1.1  wdk     if (dev->sc_flags & SBICF_DMALOADED)
    293  1.1  wdk 	    bus_dmamap_unload(dev->sc_dmat, dev->sc_dmamap);
    294  1.1  wdk }
    295  1.1  wdk 
    296  1.1  wdk /*
    297  1.1  wdk  * SCSI controller interrupt
    298  1.1  wdk  */
    299  1.1  wdk int
    300  1.1  wdk wdsc_scsiintr(arg)
    301  1.1  wdk     void *arg;
    302  1.1  wdk {
    303  1.1  wdk     struct sbic_softc *dev = arg;
    304  1.1  wdk     int found;
    305  1.1  wdk 
    306  1.1  wdk     found = sbicintr(dev);
    307  1.1  wdk     return(found);
    308  1.1  wdk }
    309