Home | History | Annotate | Line # | Download | only in dev
wdsc.c revision 1.16
      1 /*	$NetBSD: wdsc.c,v 1.16 2000/02/26 14:31:35 scw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Steve Woodford
      5  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *  This product includes software developed by the University of
     19  *  California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *  @(#)wdsc.c
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 
     44 #include <dev/scsipi/scsi_all.h>
     45 #include <dev/scsipi/scsipi_all.h>
     46 #include <dev/scsipi/scsiconf.h>
     47 
     48 #include <machine/cpu.h>
     49 #include <machine/autoconf.h>
     50 
     51 #include <mvme68k/dev/dmavar.h>
     52 #include <mvme68k/dev/pccreg.h>
     53 #include <mvme68k/dev/pccvar.h>
     54 #include <mvme68k/dev/sbicreg.h>
     55 #include <mvme68k/dev/sbicvar.h>
     56 #include <mvme68k/dev/wdscreg.h>
     57 
     58 void    wdsc_pcc_attach __P((struct device *, struct device *, void *));
     59 int     wdsc_pcc_match  __P((struct device *, struct cfdata *, void *));
     60 
     61 void    wdsc_enintr     __P((struct sbic_softc *));
     62 int     wdsc_dmago      __P((struct sbic_softc *, char *, int, int));
     63 int     wdsc_dmanext    __P((struct sbic_softc *));
     64 void    wdsc_dmastop    __P((struct sbic_softc *));
     65 int     wdsc_dmaintr    __P((void *));
     66 int     wdsc_scsiintr   __P((void *));
     67 
     68 struct scsipi_device wdsc_scsidev = {
     69     NULL,       /* use default error handler */
     70     NULL,       /* do not have a start functio */
     71     NULL,       /* have no async handler */
     72     NULL,       /* Use default done routine */
     73 };
     74 
     75 struct cfattach wdsc_pcc_ca = {
     76 	sizeof(struct sbic_softc), wdsc_pcc_match, wdsc_pcc_attach
     77 };
     78 
     79 extern struct cfdriver wdsc_cd;
     80 
     81 
     82 /*
     83  * Match for SCSI devices on the onboard WD33C93 chip
     84  */
     85 int
     86 wdsc_pcc_match(pdp, cf, auxp)
     87     struct device *pdp;
     88 	struct cfdata *cf;
     89     void *auxp;
     90 {
     91     struct pcc_attach_args *pa = auxp;
     92 
     93     if (strcmp(pa->pa_name, wdsc_cd.cd_name))
     94 	return (0);
     95 
     96     pa->pa_ipl = cf->pcccf_ipl;
     97     return (1);
     98 }
     99 
    100 /*
    101  * Attach the wdsc driver
    102  */
    103 void
    104 wdsc_pcc_attach(pdp, dp, auxp)
    105     struct device *pdp, *dp;
    106     void *auxp;
    107 {
    108     struct sbic_softc   *sc = (struct sbic_softc *)dp;
    109     struct pcc_attach_args *pa = auxp;
    110     static int          attached = 0;
    111     int tmp;
    112 
    113     if ( attached )
    114         panic("wdsc: Driver already attached!");
    115 
    116     attached = 1;
    117 
    118     sc->sc_enintr  = wdsc_enintr;
    119     sc->sc_dmago   = wdsc_dmago;
    120     sc->sc_dmanext = wdsc_dmanext;
    121     sc->sc_dmastop = wdsc_dmastop;
    122     sc->sc_dmacmd  = 0;
    123 
    124     sc->sc_adapter.scsipi_cmd = sbic_scsicmd;
    125     sc->sc_adapter.scsipi_minphys = sbic_minphys;
    126 
    127     sc->sc_link.scsipi_scsi.channel        = SCSI_CHANNEL_ONLY_ONE;
    128     sc->sc_link.adapter_softc  = sc;
    129     sc->sc_link.scsipi_scsi.adapter_target = 7;
    130     sc->sc_link.adapter        = &sc->sc_adapter;
    131     sc->sc_link.device         = &wdsc_scsidev;
    132     sc->sc_link.openings       = 2;
    133     sc->sc_link.scsipi_scsi.max_target     = 7;
    134     sc->sc_link.scsipi_scsi.max_lun = 7;
    135     sc->sc_link.type = BUS_SCSI;
    136 
    137     printf(": WD33C93 SCSI, target %d\n",
    138 		sc->sc_link.scsipi_scsi.adapter_target);
    139 
    140     sc->sc_cregs = (volatile void *)sys_pcc;
    141     sc->sc_sbicp = (sbic_regmap_p) PCC_VADDR(pa->pa_offset);
    142 
    143     /*
    144      * Eveything is a valid dma address.
    145      */
    146     sc->sc_dmamask = 0;
    147 
    148     /*
    149      * The onboard WD33C93 of the '147 is usually clocked at 10MHz...
    150      * (We use 10 times this for accuracy in later calculations)
    151      */
    152     sc->sc_clkfreq = 100;
    153 
    154     /*
    155      * Initialise the hardware
    156      */
    157     sbicinit(sc);
    158 
    159     /*
    160      * Fix up the interrupts
    161      */
    162     sc->sc_ipl = pa->pa_ipl & PCC_IMASK;
    163 
    164     sys_pcc->scsi_int = sc->sc_ipl | PCC_ICLEAR;
    165     sys_pcc->dma_int  = sc->sc_ipl | PCC_ICLEAR;
    166     sys_pcc->dma_csr  = 0;
    167 
    168     pccintr_establish(PCCV_SCSID, wdsc_dmaintr,  sc->sc_ipl, sc);
    169     pccintr_establish(PCCV_SCSIP, wdsc_scsiintr, sc->sc_ipl, sc);
    170 
    171     sys_pcc->scsi_int = sc->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    172 
    173     /*
    174      * Attach all scsi units on us, watching for boot device
    175      * (see dk_establish).
    176      */
    177     tmp = bootpart;
    178     if (PCC_PADDR(pa->pa_offset) != bootaddr)
    179 	bootpart = -1;		/* invalid flag to dk_establish */
    180     (void)config_found(dp, &sc->sc_link, scsiprint);
    181     bootpart = tmp;		/* restore old value */
    182 }
    183 
    184 /*
    185  * Enable DMA interrupts
    186  */
    187 void
    188 wdsc_enintr(dev)
    189     struct sbic_softc *dev;
    190 {
    191     volatile struct pcc *pc = dev->sc_cregs;
    192 
    193     dev->sc_flags |= SBICF_INTR;
    194 
    195     pc->dma_int = dev->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    196 }
    197 
    198 /*
    199  * Prime the hardware for a DMA transfer
    200  */
    201 int
    202 wdsc_dmago(dev, addr, count, flags)
    203     struct sbic_softc *dev;
    204     char *addr;
    205     int count, flags;
    206 {
    207     volatile struct pcc *pc = dev->sc_cregs;
    208 
    209     /*
    210      * Set up the command word based on flags
    211      */
    212     if ( (flags & DMAGO_READ) == 0 )
    213         dev->sc_dmacmd = DMAC_CSR_ENABLE | DMAC_CSR_WRITE;
    214     else
    215         dev->sc_dmacmd = DMAC_CSR_ENABLE;
    216 
    217     dev->sc_flags |= SBICF_INTR;
    218     dev->sc_tcnt   = dev->sc_cur->dc_count << 1;
    219 
    220     /*
    221      * Prime the hardware.
    222      * Note, it's probably not necessary to do this here, since dmanext
    223      * is called just prior to the actual transfer.
    224      */
    225     pc->dma_csr   = 0;
    226     pc->dma_int   = dev->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    227     pc->dma_daddr = (unsigned long)dev->sc_cur->dc_addr;
    228     pc->dma_bcnt  = (unsigned long)dev->sc_tcnt | (1 << 24);
    229     pc->dma_csr   = dev->sc_dmacmd;
    230 
    231     return(dev->sc_tcnt);
    232 }
    233 
    234 /*
    235  * Prime the hardware for the next DMA transfer
    236  */
    237 int
    238 wdsc_dmanext(dev)
    239     struct sbic_softc *dev;
    240 {
    241     volatile struct pcc *pc = dev->sc_cregs;
    242 
    243     if ( dev->sc_cur > dev->sc_last ) {
    244         /*
    245          * Shouldn't happen !!
    246          */
    247         printf("wdsc_dmanext at end !!!\n");
    248         wdsc_dmastop(dev);
    249         return(0);
    250     }
    251 
    252     dev->sc_tcnt = dev->sc_cur->dc_count << 1;
    253 
    254     /*
    255      * Load the next DMA address
    256      */
    257     pc->dma_csr   = 0;
    258     pc->dma_int   = dev->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    259     pc->dma_daddr = (unsigned long)dev->sc_cur->dc_addr;
    260     pc->dma_bcnt  = (unsigned long)dev->sc_tcnt | (1 << 24);
    261     pc->dma_csr   = dev->sc_dmacmd;
    262 
    263     return(dev->sc_tcnt);
    264 }
    265 
    266 /*
    267  * Stop DMA, and disable interrupts
    268  */
    269 void
    270 wdsc_dmastop(dev)
    271     struct sbic_softc *dev;
    272 {
    273     volatile struct pcc *pc = dev->sc_cregs;
    274     int                 s;
    275 
    276     s = splbio();
    277 
    278     pc->dma_csr    = 0;
    279     pc->dma_int    = dev->sc_ipl | PCC_ICLEAR;
    280 
    281     splx(s);
    282 }
    283 
    284 /*
    285  * Come here following a DMA interrupt
    286  */
    287 int
    288 wdsc_dmaintr(arg)
    289     void *arg;
    290 {
    291     struct sbic_softc *dev = arg;
    292     volatile struct pcc *pc = dev->sc_cregs;
    293     int                 found = 0;
    294 
    295     /*
    296      * Really a DMA interrupt?
    297      */
    298     if ( (pc->dma_int & 0x80) == 0 )
    299         return(0);
    300 
    301     /*
    302      * Was it a completion interrupt?
    303      * XXXSCW Note: Support for other DMA interrupts is required, eg. buserr
    304      */
    305     if ( pc->dma_csr & DMAC_CSR_DONE ) {
    306         ++found;
    307 
    308         pc->dma_int = dev->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    309     }
    310 
    311     return(found);
    312 }
    313 
    314 /*
    315  * Come here for SCSI interrupts
    316  */
    317 int
    318 wdsc_scsiintr(arg)
    319     void *arg;
    320 {
    321     struct sbic_softc *dev = arg;
    322     volatile struct pcc *pc = dev->sc_cregs;
    323     int                 found;
    324 
    325     /*
    326      * Really a SCSI interrupt?
    327      */
    328     if ( (pc->scsi_int & 0x80) == 0 )
    329         return(0);
    330 
    331     /*
    332      * Go handle it
    333      */
    334     found = sbicintr(dev);
    335 
    336     /*
    337      * Acknowledge and clear the interrupt
    338      */
    339     pc->scsi_int = dev->sc_ipl | PCC_IENABLE | PCC_ICLEAR;
    340 
    341     return(found);
    342 }
    343