Home | History | Annotate | Line # | Download | only in hpc
wdsc.c revision 1.27
      1 /*	$NetBSD: wdsc.c,v 1.27 2009/01/27 11:26:15 tsutsui 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.27 2009/01/27 11:26:15 tsutsui 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 #include <machine/machtype.h>
     56 #include <machine/sysconf.h>
     57 
     58 #include <sgimips/hpc/hpcvar.h>
     59 #include <sgimips/hpc/hpcreg.h>
     60 #include <sgimips/hpc/hpcdma.h>
     61 
     62 #include <dev/ic/wd33c93reg.h>
     63 #include <dev/ic/wd33c93var.h>
     64 
     65 #include <opt_kgdb.h>
     66 #include <sys/kgdb.h>
     67 
     68 struct wdsc_softc {
     69 	struct wd33c93_softc	sc_wd33c93; /* Must be first */
     70 	struct evcnt		sc_intrcnt; /* Interrupt counter */
     71 	bus_dma_tag_t		sc_dmat;
     72 	bus_dmamap_t		sc_dmamap;
     73 	int			sc_flags;
     74 #define	WDSC_DMA_ACTIVE			0x1
     75 #define	WDSC_DMA_MAPLOADED		0x2
     76 	struct hpc_dma_softc	sc_hpcdma;
     77 };
     78 
     79 
     80 void	wdsc_attach	(device_t, device_t, void *);
     81 int	wdsc_match	(device_t, cfdata_t, void *);
     82 
     83 CFATTACH_DECL_NEW(wdsc, sizeof(struct wdsc_softc),
     84     wdsc_match, wdsc_attach, NULL, NULL);
     85 
     86 int	wdsc_dmasetup	(struct wd33c93_softc *, void ** ,size_t *,
     87 				int, size_t *);
     88 int	wdsc_dmago	(struct wd33c93_softc *);
     89 void	wdsc_dmastop	(struct wd33c93_softc *);
     90 void	wdsc_reset	(struct wd33c93_softc *);
     91 int	wdsc_dmaintr	(void *);
     92 int	wdsc_scsiintr	(void *);
     93 
     94 /*
     95  * Match for SCSI devices on the onboard and GIO32 adapter WD33C93 chips
     96  */
     97 int
     98 wdsc_match(device_t parent, cfdata_t cf, void *auxp)
     99 {
    100 	struct hpc_attach_args *haa = auxp;
    101 
    102 	if (strcmp(haa->ha_name, cf->cf_name) == 0) {
    103 		uint32_t reset, asr, reg;
    104 
    105 		reset = MIPS_PHYS_TO_KSEG1(haa->ha_sh + haa->ha_dmaoff +
    106 		    haa->hpc_regs->scsi0_ctl);
    107 		asr = MIPS_PHYS_TO_KSEG1(haa->ha_sh + haa->ha_devoff);
    108 
    109 		/* XXX: hpc1 offset due to SGIMIPS_BUS_SPACE_HPC brain damage */
    110 		asr = (asr + 3) & ~0x3;
    111 
    112 		if (platform.badaddr((void *)reset, sizeof(reset)))
    113 			return (0);
    114 
    115 		*(volatile uint32_t *)reset = haa->hpc_regs->scsi_dmactl_reset;
    116 		delay(1000);
    117 		*(volatile uint32_t *)reset = 0x0;
    118 
    119 		if (platform.badaddr((void *)asr, sizeof(asr)))
    120 			return (0);
    121 
    122 		reg = *(volatile uint32_t *)asr;
    123 		if (haa->hpc_regs->revision == 3) {
    124 			if ((reg & 0xff) == SBIC_ASR_INT)
    125 				return (1);
    126 		} else {
    127 			if (((reg >> 8) & 0xff) == SBIC_ASR_INT)
    128 				return (1);
    129 		}
    130 	}
    131 
    132 	return (0);
    133 }
    134 
    135 /*
    136  * Attach the wdsc driver
    137  */
    138 void
    139 wdsc_attach(device_t parent, device_t self, void *aux)
    140 {
    141 	struct wdsc_softc *wsc = device_private(self);
    142 	struct wd33c93_softc *sc = &wsc->sc_wd33c93;
    143 	struct hpc_attach_args *haa = aux;
    144 	int err;
    145 
    146 	sc->sc_dev = self;
    147 	sc->sc_regt = haa->ha_st;
    148 	wsc->sc_dmat = haa->ha_dmat;
    149 
    150 	wsc->sc_hpcdma.hpc = haa->hpc_regs;
    151 
    152 	if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
    153 					haa->ha_devoff,
    154 					wsc->sc_hpcdma.hpc->scsi0_devregs_size,
    155 					&sc->sc_regh)) != 0) {
    156 		printf(": unable to map regs, err=%d\n", err);
    157 		return;
    158 	}
    159 
    160 	if (bus_dmamap_create(wsc->sc_dmat,
    161 			      wsc->sc_hpcdma.hpc->scsi_max_xfer,
    162 			      wsc->sc_hpcdma.hpc->scsi_dma_segs,
    163 			      wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
    164 			      wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
    165 			      BUS_DMA_WAITOK,
    166 			      &wsc->sc_dmamap) != 0) {
    167 		printf(": failed to create dmamap\n");
    168 		return;
    169 	}
    170 
    171 	sc->sc_dmasetup = wdsc_dmasetup;
    172 	sc->sc_dmago    = wdsc_dmago;
    173 	sc->sc_dmastop  = wdsc_dmastop;
    174 	sc->sc_reset	= wdsc_reset;
    175 
    176 	sc->sc_adapter.adapt_request = wd33c93_scsi_request;
    177 	sc->sc_adapter.adapt_minphys = minphys;
    178 
    179 	sc->sc_id = 0;					/* Host ID = 0 */
    180 	sc->sc_clkfreq = 200;				/* 20MHz */
    181 	sc->sc_dmamode = SBIC_CTL_BURST_DMA;
    182 
    183 	evcnt_attach_dynamic(&wsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    184 	    device_xname(sc->sc_dev), "intr");
    185 
    186 	if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
    187 	     wdsc_scsiintr, wsc)) == NULL) {
    188 		printf(": unable to establish interrupt!\n");
    189 		return;
    190 	}
    191 
    192 	hpcdma_init(haa, &wsc->sc_hpcdma, wsc->sc_hpcdma.hpc->scsi_dma_segs);
    193 	wd33c93_attach(sc);
    194 	return;
    195 }
    196 
    197 /*
    198  * Prime the hardware for a DMA transfer
    199  *
    200  * Requires splbio() interrupts to be disabled by the caller
    201  */
    202 int
    203 wdsc_dmasetup(struct wd33c93_softc *sc, void **addr, size_t *len, int datain, size_t *dmasize)
    204 {
    205 	struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
    206 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    207 	int count, err;
    208 	void *vaddr;
    209 
    210 	KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
    211 
    212 	vaddr = *addr;
    213 	count = dsc->sc_dlen = *len;
    214 	if (count) {
    215 		KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED) == 0);
    216 
    217 		/* Build list of physical addresses for this transfer */
    218 		if ((err=bus_dmamap_load(wsc->sc_dmat, wsc->sc_dmamap,
    219 				vaddr, count,
    220 				NULL /* kernel address */,
    221 				BUS_DMA_NOWAIT)) != 0)
    222 			panic("%s: bus_dmamap_load err=%d",
    223 			    device_xname(sc->sc_dev), err);
    224 
    225 		hpcdma_sglist_create(dsc, wsc->sc_dmamap);
    226 		wsc->sc_flags |= WDSC_DMA_MAPLOADED;
    227 
    228 		if (datain) {
    229 			dsc->sc_dmacmd =
    230 			    wsc->sc_hpcdma.hpc->scsi_dma_datain_cmd;
    231 			dsc->sc_flags |= HPCDMA_READ;
    232 		} else {
    233 			dsc->sc_dmacmd =
    234 			    wsc->sc_hpcdma.hpc->scsi_dma_dataout_cmd;
    235 			dsc->sc_flags &= ~HPCDMA_READ;
    236 		}
    237 	}
    238 	return(count);
    239 }
    240 
    241 /*
    242  * Prime the hardware for the next DMA transfer
    243  */
    244 int
    245 wdsc_dmago(struct wd33c93_softc *sc)
    246 {
    247 	struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
    248 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    249 
    250 	if (dsc->sc_dlen == 0)
    251 		return(0);
    252 
    253 	KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
    254 	KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED));
    255 
    256 	wsc->sc_flags |= WDSC_DMA_ACTIVE;
    257 
    258 	bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
    259 	    		wsc->sc_dmamap->dm_mapsize,
    260 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    261 
    262 	hpcdma_cntl(dsc, dsc->sc_dmacmd);	/* Thunderbirds are go! */
    263 
    264 	return(wsc->sc_dmamap->dm_mapsize);
    265 }
    266 
    267 /*
    268  * Stop DMA and unload active DMA maps
    269  */
    270 void
    271 wdsc_dmastop(struct wd33c93_softc *sc)
    272 {
    273 	struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
    274 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    275 
    276 	if (wsc->sc_flags & WDSC_DMA_ACTIVE) {
    277 		if (dsc->sc_flags & HPCDMA_READ)
    278 			hpcdma_flush(dsc);
    279 		hpcdma_cntl(dsc, 0);	/* Stop DMA */
    280 		bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap, 0,
    281 		    wsc->sc_dmamap->dm_mapsize,
    282 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    283 	}
    284 	if (wsc->sc_flags & WDSC_DMA_MAPLOADED)
    285 		bus_dmamap_unload(wsc->sc_dmat, wsc->sc_dmamap);
    286 	wsc->sc_flags &= ~(WDSC_DMA_ACTIVE | WDSC_DMA_MAPLOADED);
    287 }
    288 
    289 /*
    290  * Reset the controller.
    291  */
    292 void
    293 wdsc_reset(struct wd33c93_softc *sc)
    294 {
    295 	struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
    296 	struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
    297 
    298 	hpcdma_reset(dsc);
    299 }
    300 
    301 /*
    302  * WD33c93 SCSI controller interrupt
    303  */
    304 int
    305 wdsc_scsiintr(void *arg)
    306 {
    307 	struct wdsc_softc *wsc = arg;
    308 	struct wd33c93_softc *sc = &wsc->sc_wd33c93;
    309 	int found;
    310 
    311 	found = wd33c93_intr(sc);
    312 	if (found)
    313 		wsc->sc_intrcnt.ev_count++;
    314 	return(found);
    315 }
    316