Home | History | Annotate | Line # | Download | only in hpc
wdsc.c revision 1.12
      1 /*	$NetBSD: wdsc.c,v 1.12 2003/12/16 11:59:04 sekiya Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wayne Knowles
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wayne Knowles
      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>
     40 __KERNEL_RCSID(0, "$NetBSD: wdsc.c,v 1.12 2003/12/16 11:59:04 sekiya Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/buf.h>
     47 
     48 #include <dev/scsipi/scsi_all.h>
     49 #include <dev/scsipi/scsipi_all.h>
     50 #include <dev/scsipi/scsiconf.h>
     51 
     52 #include <machine/cpu.h>
     53 #include <machine/bus.h>
     54 #include <machine/autoconf.h>
     55 
     56 #include <sgimips/hpc/hpcvar.h>
     57 #include <sgimips/hpc/hpcreg.h>
     58 #include <sgimips/hpc/hpcdma.h>
     59 
     60 #include <sgimips/hpc/sbicreg.h>
     61 #include <sgimips/hpc/sbicvar.h>
     62 
     63 #include <opt_kgdb.h>
     64 #include <sys/kgdb.h>
     65 
     66 struct wdsc_softc {
     67 	struct wd33c93_softc	sc_wd33c93; /* Must be first */
     68 	struct evcnt		sc_intrcnt; /* Interrupt counter */
     69 	bus_dma_tag_t		sc_dmat;
     70 	bus_dmamap_t		sc_dmamap;
     71 	int			sc_flags;
     72 #define	WDSC_DMA_ACTIVE			0x1
     73 #define	WDSC_DMA_MAPLOADED		0x2
     74 	struct hpc_dma_softc	sc_hpcdma;
     75 };
     76 
     77 
     78 void	wdsc_attach	__P((struct device *, struct device *, void *));
     79 int	wdsc_match	__P((struct device *, struct cfdata *, void *));
     80 
     81 CFATTACH_DECL(wdsc, sizeof(struct wdsc_softc),
     82     wdsc_match, wdsc_attach, NULL, NULL);
     83 
     84 int	wdsc_dmasetup	__P((struct wd33c93_softc *, caddr_t *,size_t *,
     85 				int, size_t *));
     86 int	wdsc_dmago	__P((struct wd33c93_softc *));
     87 void	wdsc_dmastop	__P((struct wd33c93_softc *));
     88 void	wdsc_reset	__P((struct wd33c93_softc *));
     89 int	wdsc_dmaintr	__P((void *));
     90 int	wdsc_scsiintr	__P((void *));
     91 
     92 #define MAX_SCSI_XFER	(512*1024)
     93 #define MAX_SEG_SZ	8192
     94 #define	MAX_DMA_SZ	MAX_SCSI_XFER
     95 #define	DMA_SEGS	(MAX_DMA_SZ/MAX_SEG_SZ)
     96 
     97 /*
     98  * Match for SCSI devices on the onboard WD33C93 chip
     99  */
    100 int
    101 wdsc_match(pdp, cf, auxp)
    102 	struct device *pdp;
    103 	struct cfdata *cf;
    104 	void *auxp;
    105 {
    106 	struct hpc_attach_args *haa = auxp;
    107 
    108 	if (strcmp(haa->ha_name, cf->cf_name) == 0)
    109 		return (1);
    110 
    111 	return (0);
    112 }
    113 
    114 /*
    115  * Attach the wdsc driver
    116  */
    117 void
    118 wdsc_attach(pdp, dp, auxp)
    119 	struct device *pdp, *dp;
    120 	void *auxp;
    121 {
    122 	struct wd33c93_softc *sc = (void *)dp;
    123 	struct wdsc_softc *wsc = (void *)dp;
    124 	struct hpc_attach_args *haa = auxp;
    125 	int err;
    126 
    127 	sc->sc_regt = haa->ha_st;
    128 	wsc->sc_dmat = haa->ha_dmat;
    129 
    130 	wsc->sc_hpcdma.hpc = haa->hpc_regs;
    131 
    132 	if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
    133 					haa->ha_devoff,
    134 					wsc->sc_hpcdma.hpc->scsi0_devregs_size,
    135 					&sc->sc_regh)) != 0) {
    136 		printf(": unable to map regs, err=%d\n", err);
    137 		return;
    138 	}
    139 
    140 	if (bus_dmamap_create(wsc->sc_dmat, MAX_DMA_SZ,
    141 			      wsc->sc_hpcdma.hpc->scsi_dma_segs,
    142 			      wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
    143 			      wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
    144 			      BUS_DMA_WAITOK,
    145 			      &wsc->sc_dmamap) != 0) {
    146 		printf(": failed to create dmamap\n");
    147 		return;
    148 	}
    149 
    150 	sc->sc_dmasetup = wdsc_dmasetup;
    151 	sc->sc_dmago    = wdsc_dmago;
    152 	sc->sc_dmastop  = wdsc_dmastop;
    153 	sc->sc_reset	= wdsc_reset;
    154 
    155 	sc->sc_adapter.adapt_request = wd33c93_scsi_request;
    156 	sc->sc_adapter.adapt_minphys = minphys;
    157 
    158 	sc->sc_id = 0;					/* Host ID = 0 */
    159 	sc->sc_clkfreq = wsc->sc_hpcdma.hpc->clk_freq;
    160 
    161 	evcnt_attach_dynamic(&wsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    162 			     sc->sc_dev.dv_xname, "intr");
    163 
    164 	if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
    165 	     wdsc_scsiintr, sc)) == NULL) {
    166 		printf(": unable to establish interrupt!\n");
    167 		return;
    168 	}
    169 
    170 	hpcdma_init(haa, &wsc->sc_hpcdma, DMA_SEGS);
    171 	wd33c93_attach(sc);
    172 	return;
    173 }
    174 
    175 /*
    176  * Prime the hardware for a DMA transfer
    177  *
    178  * Requires splbio() interrupts to be disabled by the caller
    179  */
    180 int
    181 wdsc_dmasetup(dev, addr, len, datain, dmasize)
    182 	struct wd33c93_softc *dev;
    183 	caddr_t *addr;
    184 	size_t *len;
    185 	int datain;
    186 	size_t *dmasize;
    187 {
    188 	struct wdsc_softc *wsc = (void *)dev;
    189 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    190 	int count, err;
    191 	void *vaddr;
    192 
    193 	KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
    194 
    195 	vaddr = *addr;
    196 	count = dsc->sc_dlen = *len;
    197 	if (count) {
    198 		KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED) == 0);
    199 
    200 		/* Build list of physical addresses for this transfer */
    201 		if ((err=bus_dmamap_load(wsc->sc_dmat, wsc->sc_dmamap,
    202 				vaddr, count,
    203 				NULL /* kernel address */,
    204 				BUS_DMA_NOWAIT)) != 0)
    205 			panic("%s: bus_dmamap_load err=%d",
    206 			      dev->sc_dev.dv_xname, err);
    207 
    208 		hpcdma_sglist_create(dsc, wsc->sc_dmamap);
    209 		wsc->sc_flags |= WDSC_DMA_MAPLOADED;
    210 
    211 		if (datain) {
    212 			dsc->sc_dmacmd = wsc->sc_hpcdma.hpc->dma_datain_cmd;
    213 			dsc->sc_flags |= HPCDMA_READ;
    214 		} else {
    215 			dsc->sc_dmacmd = wsc->sc_hpcdma.hpc->dma_dataout_cmd;
    216 			dsc->sc_flags &= ~HPCDMA_READ;
    217 		}
    218 	}
    219 	return(count);
    220 }
    221 
    222 /*
    223  * Prime the hardware for the next DMA transfer
    224  */
    225 int
    226 wdsc_dmago(dev)
    227 	struct wd33c93_softc *dev;
    228 {
    229 	struct wdsc_softc *wsc = (void *)dev;
    230 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    231 
    232 	if (dsc->sc_dlen == 0)
    233 		return(0);
    234 
    235 	KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
    236 	KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED));
    237 
    238 	wsc->sc_flags |= WDSC_DMA_ACTIVE;
    239 
    240 	bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
    241 	    		wsc->sc_dmamap->dm_mapsize,
    242 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    243 
    244 	hpcdma_cntl(dsc, dsc->sc_dmacmd);	/* Thunderbirds are go! */
    245 
    246 	return(wsc->sc_dmamap->dm_mapsize);
    247 }
    248 
    249 /*
    250  * Stop DMA and unload active DMA maps
    251  */
    252 void
    253 wdsc_dmastop(dev)
    254 	struct wd33c93_softc *dev;
    255 {
    256 	struct wdsc_softc *wsc = (void *)dev;
    257 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    258 
    259 	if (wsc->sc_flags & WDSC_DMA_ACTIVE) {
    260 		if (dsc->sc_flags & HPCDMA_READ)
    261 			hpcdma_flush(dsc);
    262 		hpcdma_cntl(dsc, 0);	/* Stop DMA */
    263 		bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
    264 		    wsc->sc_dmamap->dm_mapsize,
    265 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    266 	}
    267 	if (wsc->sc_flags & WDSC_DMA_MAPLOADED)
    268 		bus_dmamap_unload(wsc->sc_dmat, wsc->sc_dmamap);
    269 	wsc->sc_flags &= ~(WDSC_DMA_ACTIVE | WDSC_DMA_MAPLOADED);
    270 }
    271 
    272 /*
    273  * Reset the controller.
    274  */
    275 void
    276 wdsc_reset(dev)
    277 	struct wd33c93_softc *dev;
    278 {
    279 	struct wdsc_softc *wsc = (void *)dev;
    280 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    281 
    282 	hpcdma_reset(dsc);
    283 }
    284 
    285 /*
    286  * WD33c93 SCSI controller interrupt
    287  */
    288 int
    289 wdsc_scsiintr(arg)
    290 	void *arg;
    291 {
    292 	struct wd33c93_softc *dev = arg;
    293 	struct wdsc_softc *wsc = arg;
    294 	int found;
    295 
    296 	found = wd33c93_intr(dev);
    297 	if (found)
    298 		wsc->sc_intrcnt.ev_count++;
    299 	return(found);
    300 }
    301