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