1 /* $NetBSD: atzsc.c,v 1.8 1994/12/01 17:24:52 chopps Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Christian E. Hopps 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 * @(#)dma.c 37 */ 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/device.h> 42 #include <scsi/scsi_all.h> 43 #include <scsi/scsiconf.h> 44 #include <amiga/amiga/custom.h> 45 #include <amiga/amiga/cc.h> 46 #include <amiga/amiga/device.h> 47 #include <amiga/dev/dmavar.h> 48 #include <amiga/dev/sbicreg.h> 49 #include <amiga/dev/sbicvar.h> 50 #include <amiga/dev/atzscreg.h> 51 #include <amiga/dev/ztwobusvar.h> 52 53 int atzscprint __P((void *auxp, char *)); 54 void atzscattach __P((struct device *, struct device *, void *)); 55 int atzscmatch __P((struct device *, struct cfdata *, void *)); 56 57 void atzsc_dmafree __P((struct sbic_softc *)); 58 void atzsc_dmastop __P((struct sbic_softc *)); 59 int atzsc_dmanext __P((struct sbic_softc *)); 60 int atzsc_dmaintr __P((void)); 61 int atzsc_dmago __P((struct sbic_softc *, char *, int, int)); 62 63 struct scsi_adapter atzsc_scsiswitch = { 64 sbic_scsicmd, 65 sbic_minphys, 66 0, /* no lun support */ 67 0, /* no lun support */ 68 sbic_adinfo, 69 "atzsc", 70 }; 71 72 struct scsi_device atzsc_scsidev = { 73 NULL, /* use default error handler */ 74 NULL, /* do not have a start functio */ 75 NULL, /* have no async handler */ 76 NULL, /* Use default done routine */ 77 "atzsc", 78 0, 79 }; 80 81 82 #ifdef DEBUG 83 void atzsc_dmatimeout __P((void *)); 84 int atzsc_dmadebug = 0; 85 #endif 86 87 struct cfdriver atzsccd = { 88 NULL, "atzsc", (cfmatch_t)atzscmatch, atzscattach, 89 DV_DULL, sizeof(struct sbic_softc), NULL, 0 }; 90 91 /* 92 * if we are an A3000 we are here. 93 */ 94 int 95 atzscmatch(pdp, cdp, auxp) 96 struct device *pdp; 97 struct cfdata *cdp; 98 void *auxp; 99 { 100 struct ztwobus_args *zap; 101 102 zap = auxp; 103 104 /* 105 * Check manufacturer and product id. 106 * I was informed that older boards can be 2 also. 107 */ 108 if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2)) 109 return(1); 110 else 111 return(0); 112 } 113 114 void 115 atzscattach(pdp, dp, auxp) 116 struct device *pdp, *dp; 117 void *auxp; 118 { 119 volatile struct sdmac *rp; 120 struct sbic_softc *sc; 121 struct ztwobus_args *zap; 122 123 zap = auxp; 124 125 sc = (struct sbic_softc *)dp; 126 sc->sc_cregs = rp = zap->va; 127 /* 128 * disable ints and reset bank register 129 */ 130 rp->CNTR = CNTR_PDMD; 131 rp->DAWR = DAWR_ATZSC; 132 sc->sc_dmafree = atzsc_dmafree; 133 sc->sc_dmago = atzsc_dmago; 134 sc->sc_dmanext = atzsc_dmanext; 135 sc->sc_dmastop = atzsc_dmastop; 136 sc->sc_dmacmd = 0; 137 138 #ifdef DEBUG 139 /* make sure timeout is really not needed */ 140 timeout(atzsc_dmatimeout, 0, 30 * hz); 141 #endif 142 /* 143 * only 24 bit mem. 144 */ 145 sc->sc_flags |= SBICF_BADDMA; 146 sc->sc_dmamask = ~0x00ffffff; 147 /* 148 * If the users kva space is not ztwo try and allocate a bounce buffer. 149 * XXX this needs to change if we move to multiple memory segments. 150 */ 151 if (kvtop(sc) & sc->sc_dmamask) { 152 sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS); 153 if (isztwomem(sc->sc_dmabuffer)) 154 printf(" bounce pa 0x%x", ztwopa(sc->sc_dmabuffer)); 155 else if (sc->sc_dmabuffer) 156 printf(" bounce pa 0x%x", 157 PREP_DMA_MEM(sc->sc_dmabuffer)); 158 } 159 sc->sc_sbicp = (sbic_regmap_p) ((int)rp + 0x91); 160 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77; 161 162 printf(": dmamask 0x%x\n", ~sc->sc_dmamask); 163 164 sbicreset(sc); 165 166 sc->sc_link.adapter_softc = sc; 167 sc->sc_link.adapter_targ = 7; 168 sc->sc_link.adapter = &atzsc_scsiswitch; 169 sc->sc_link.device = &atzsc_scsidev; 170 TAILQ_INIT(&sc->sc_xslist); 171 172 custom.intreq = INTF_PORTS; 173 custom.intena = INTF_SETCLR | INTF_PORTS; 174 175 /* 176 * attach all scsi units on us 177 */ 178 config_found(dp, &sc->sc_link, atzscprint); 179 } 180 181 /* 182 * print diag if pnp is NULL else just extra 183 */ 184 int 185 atzscprint(auxp, pnp) 186 void *auxp; 187 char *pnp; 188 { 189 if (pnp == NULL) 190 return(UNCONF); 191 return(QUIET); 192 } 193 194 195 void 196 atzsc_dmafree(dev) 197 struct sbic_softc *dev; 198 { 199 volatile struct sdmac *sdp; 200 int s; 201 202 sdp = dev->sc_cregs; 203 204 s = splbio(); 205 #ifdef DEBUG 206 dev->sc_dmatimo = 0; 207 #endif 208 if (dev->sc_dmacmd) { 209 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 210 /* 211 * only FLUSH if terminal count not enabled, 212 * and reading from peripheral 213 */ 214 sdp->FLUSH = 1; 215 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 216 ; 217 } 218 /* 219 * clear possible interrupt and stop dma 220 */ 221 sdp->CINT = 1; 222 sdp->SP_DMA = 1; 223 dev->sc_dmacmd = 0; 224 } 225 /* 226 * disable interrupts 227 */ 228 sdp->CNTR = CNTR_PDMD; /* disable interrupts from dma/scsi */ 229 dev->sc_flags &= ~SBICF_INTR; 230 splx(s); 231 } 232 233 int 234 atzsc_dmago(dev, addr, count, flags) 235 struct sbic_softc *dev; 236 char *addr; 237 int count, flags; 238 { 239 volatile struct sdmac *sdp; 240 241 sdp = dev->sc_cregs; 242 /* 243 * Set up the command word based on flags 244 */ 245 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN; 246 if ((flags & DMAGO_READ) == 0) 247 dev->sc_dmacmd |= CNTR_DDIR; 248 #ifdef DEBUG 249 if (atzsc_dmadebug & DDB_IO) 250 printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd); 251 dev->sc_dmatimo = 1; 252 #endif 253 254 dev->sc_flags |= SBICF_INTR; 255 sdp->CNTR = dev->sc_dmacmd; 256 sdp->ACR = (u_int) dev->sc_cur->dc_addr; 257 sdp->ST_DMA = 1; 258 259 return(dev->sc_tcnt); 260 } 261 262 void 263 atzsc_dmastop(dev) 264 struct sbic_softc *dev; 265 { 266 volatile struct sdmac *sdp; 267 int s; 268 269 sdp = dev->sc_cregs; 270 271 #ifdef DEBUG 272 if (atzsc_dmadebug & DDB_FOLLOW) 273 printf("atzsc_dmastop()\n"); 274 dev->sc_dmatimo = 0; 275 #endif 276 if (dev->sc_dmacmd) { 277 s = splbio(); 278 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 279 /* 280 * only FLUSH if terminal count not enabled, 281 * and reading from peripheral 282 */ 283 sdp->FLUSH = 1; 284 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 285 ; 286 } 287 /* 288 * clear possible interrupt and stop dma 289 */ 290 sdp->CINT = 1; 291 sdp->SP_DMA = 1; 292 dev->sc_dmacmd = 0; 293 splx(s); 294 } 295 } 296 297 int 298 atzsc_dmaintr() 299 { 300 volatile struct sdmac *sdp; 301 struct sbic_softc *dev; 302 int i, stat, found; 303 304 found = 0; 305 for (i = 0; i < atzsccd.cd_ndevs; i++) { 306 dev = atzsccd.cd_devs[i]; 307 if (dev == NULL) 308 continue; 309 sdp = dev->sc_cregs; 310 stat = sdp->ISTR; 311 312 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0) 313 continue; 314 315 #ifdef DEBUG 316 if (atzsc_dmadebug & DDB_FOLLOW) 317 printf("atzsc_dmaintr (%d, 0x%x)\n", i, stat); 318 #endif 319 320 /* 321 * both, SCSI and DMA interrupts arrive here. I chose 322 * arbitrarily that DMA interrupts should have higher 323 * precedence than SCSI interrupts. 324 */ 325 if (stat & ISTR_E_INT) { 326 found++; 327 328 sdp->CINT = 1; /* clear possible interrupt */ 329 330 /* 331 * check for SCSI ints in the same go and 332 * eventually save an interrupt 333 */ 334 } 335 336 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS) 337 found += sbicintr(dev); 338 } 339 return(found); 340 } 341 342 343 int 344 atzsc_dmanext(dev) 345 struct sbic_softc *dev; 346 { 347 volatile struct sdmac *sdp; 348 int i, stat; 349 350 sdp = dev->sc_cregs; 351 352 if (dev->sc_cur > dev->sc_last) { 353 /* shouldn't happen !! */ 354 printf("atzsc_dmanext at end !!!\n"); 355 atzsc_dmastop(dev); 356 return(0); 357 } 358 #ifdef DEBUG 359 dev->sc_dmatimo = 1; 360 #endif 361 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 362 /* 363 * only FLUSH if terminal count not enabled, 364 * and reading from peripheral 365 */ 366 sdp->FLUSH = 1; 367 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 368 ; 369 } 370 /* 371 * clear possible interrupt and stop dma 372 */ 373 sdp->CINT = 1; /* clear possible interrupt */ 374 sdp->SP_DMA = 1; /* stop dma */ 375 sdp->CNTR = dev->sc_dmacmd; 376 sdp->ACR = (u_int)dev->sc_cur->dc_addr; 377 sdp->ST_DMA = 1; 378 379 dev->sc_tcnt = dev->sc_cur->dc_count << 1; 380 return(dev->sc_tcnt); 381 } 382 383 #ifdef DEBUG 384 /*ARGSUSED*/ 385 void 386 atzsc_dmatimeout(arg) 387 void *arg; 388 { 389 struct sbic_softc *dev; 390 int i, s; 391 392 for (i = 0; i < atzsccd.cd_ndevs; i++) { 393 dev = atzsccd.cd_devs[i]; 394 if (dev == NULL) 395 continue; 396 397 s = splbio(); 398 if (dev->sc_dmatimo) { 399 if (dev->sc_dmatimo > 1) 400 printf("atzsc_dma%d: timeout #%d\n", 401 dev->sc_dev.dv_unit, dev->sc_dmatimo - 1); 402 dev->sc_dmatimo++; 403 } 404 splx(s); 405 } 406 timeout(atzsc_dmatimeout, 0, 30 * hz); 407 } 408 #endif 409