1 /* $NetBSD: atzsc.c,v 1.28 1998/12/05 19:43:34 mjacob 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 <dev/scsipi/scsi_all.h> 43 #include <dev/scsipi/scsipi_all.h> 44 #include <dev/scsipi/scsiconf.h> 45 #include <amiga/amiga/custom.h> 46 #include <amiga/amiga/cc.h> 47 #include <amiga/amiga/device.h> 48 #include <amiga/amiga/isr.h> 49 #include <amiga/dev/dmavar.h> 50 #include <amiga/dev/sbicreg.h> 51 #include <amiga/dev/sbicvar.h> 52 #include <amiga/dev/atzscreg.h> 53 #include <amiga/dev/zbusvar.h> 54 55 void atzscattach __P((struct device *, struct device *, void *)); 56 int atzscmatch __P((struct device *, struct cfdata *, void *)); 57 58 void atzsc_enintr __P((struct sbic_softc *)); 59 void atzsc_dmastop __P((struct sbic_softc *)); 60 int atzsc_dmanext __P((struct sbic_softc *)); 61 int atzsc_dmaintr __P((void *)); 62 int atzsc_dmago __P((struct sbic_softc *, char *, int, int)); 63 64 #ifdef DEBUG 65 void atzsc_dump __P((void)); 66 #endif 67 68 struct scsipi_device atzsc_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 76 #ifdef DEBUG 77 int atzsc_dmadebug = 0; 78 #endif 79 80 struct cfattach atzsc_ca = { 81 sizeof(struct sbic_softc), atzscmatch, atzscattach 82 }; 83 84 /* 85 * if we are an A3000 we are here. 86 */ 87 int 88 atzscmatch(pdp, cfp, auxp) 89 struct device *pdp; 90 struct cfdata *cfp; 91 void *auxp; 92 { 93 struct zbus_args *zap; 94 95 zap = auxp; 96 97 /* 98 * Check manufacturer and product id. 99 * I was informed that older boards can be 2 also. 100 */ 101 if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2)) 102 return(1); 103 else 104 return(0); 105 } 106 107 void 108 atzscattach(pdp, dp, auxp) 109 struct device *pdp, *dp; 110 void *auxp; 111 { 112 volatile struct sdmac *rp; 113 struct sbic_softc *sc; 114 struct zbus_args *zap; 115 116 zap = auxp; 117 118 sc = (struct sbic_softc *)dp; 119 sc->sc_cregs = rp = zap->va; 120 /* 121 * disable ints and reset bank register 122 */ 123 rp->CNTR = CNTR_PDMD; 124 rp->DAWR = DAWR_ATZSC; 125 sc->sc_enintr = atzsc_enintr; 126 sc->sc_dmago = atzsc_dmago; 127 sc->sc_dmanext = atzsc_dmanext; 128 sc->sc_dmastop = atzsc_dmastop; 129 sc->sc_dmacmd = 0; 130 131 /* 132 * only 24 bit mem. 133 */ 134 sc->sc_flags |= SBICF_BADDMA; 135 sc->sc_dmamask = ~0x00ffffff; 136 #if 0 137 /* 138 * If the users kva space is not ztwo try and allocate a bounce buffer. 139 * XXX this needs to change if we move to multiple memory segments. 140 */ 141 if (kvtop(sc) & sc->sc_dmamask) { 142 sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS * 8); /* XXX */ 143 if (isztwomem(sc->sc_dmabuffer)) 144 printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer)); 145 else if (sc->sc_dmabuffer) 146 printf(" bounce pa 0x%x", 147 PREP_DMA_MEM(sc->sc_dmabuffer)); 148 } 149 #endif 150 sc->sc_sbic.sbic_asr_p = (volatile unsigned char *)rp + 0x91; 151 sc->sc_sbic.sbic_value_p = (volatile unsigned char *)rp + 0x93; 152 153 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77; 154 155 printf(": dmamask 0x%lx\n", ~sc->sc_dmamask); 156 157 sc->sc_adapter.scsipi_cmd = sbic_scsicmd; 158 sc->sc_adapter.scsipi_minphys = sbic_minphys; 159 160 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE; 161 sc->sc_link.adapter_softc = sc; 162 sc->sc_link.scsipi_scsi.adapter_target = 7; 163 sc->sc_link.adapter = &sc->sc_adapter; 164 sc->sc_link.device = &atzsc_scsidev; 165 sc->sc_link.openings = 2; 166 sc->sc_link.scsipi_scsi.max_target = 7; 167 sc->sc_link.scsipi_scsi.max_lun = 7; 168 sc->sc_link.type = BUS_SCSI; 169 170 sbicinit(sc); 171 172 sc->sc_isr.isr_intr = atzsc_dmaintr; 173 sc->sc_isr.isr_arg = sc; 174 sc->sc_isr.isr_ipl = 2; 175 add_isr (&sc->sc_isr); 176 177 /* 178 * attach all scsi units on us 179 */ 180 config_found(dp, &sc->sc_link, scsiprint); 181 } 182 183 void 184 atzsc_enintr(dev) 185 struct sbic_softc *dev; 186 { 187 volatile struct sdmac *sdp; 188 189 sdp = dev->sc_cregs; 190 191 dev->sc_flags |= SBICF_INTR; 192 sdp->CNTR = CNTR_PDMD | CNTR_INTEN; 193 } 194 195 int 196 atzsc_dmago(dev, addr, count, flags) 197 struct sbic_softc *dev; 198 char *addr; 199 int count, flags; 200 { 201 volatile struct sdmac *sdp; 202 203 sdp = dev->sc_cregs; 204 /* 205 * Set up the command word based on flags 206 */ 207 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN; 208 if ((flags & DMAGO_READ) == 0) 209 dev->sc_dmacmd |= CNTR_DDIR; 210 #ifdef DEBUG 211 if (atzsc_dmadebug & DDB_IO) 212 printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd); 213 #endif 214 215 dev->sc_flags |= SBICF_INTR; 216 sdp->CNTR = dev->sc_dmacmd; 217 sdp->ACR = (u_int) dev->sc_cur->dc_addr; 218 sdp->ST_DMA = 1; 219 220 return(dev->sc_tcnt); 221 } 222 223 void 224 atzsc_dmastop(dev) 225 struct sbic_softc *dev; 226 { 227 volatile struct sdmac *sdp; 228 int s; 229 230 sdp = dev->sc_cregs; 231 232 #ifdef DEBUG 233 if (atzsc_dmadebug & DDB_FOLLOW) 234 printf("atzsc_dmastop()\n"); 235 #endif 236 if (dev->sc_dmacmd) { 237 s = splbio(); 238 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 239 /* 240 * only FLUSH if terminal count not enabled, 241 * and reading from peripheral 242 */ 243 sdp->FLUSH = 1; 244 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 245 ; 246 } 247 /* 248 * clear possible interrupt and stop dma 249 */ 250 sdp->CINT = 1; 251 sdp->SP_DMA = 1; 252 dev->sc_dmacmd = 0; 253 splx(s); 254 } 255 } 256 257 int 258 atzsc_dmaintr(arg) 259 void *arg; 260 { 261 struct sbic_softc *dev = arg; 262 volatile struct sdmac *sdp; 263 int stat, found; 264 265 sdp = dev->sc_cregs; 266 stat = sdp->ISTR; 267 268 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0) 269 return (0); 270 271 #ifdef DEBUG 272 if (atzsc_dmadebug & DDB_FOLLOW) 273 printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat); 274 #endif 275 276 /* 277 * both, SCSI and DMA interrupts arrive here. I chose 278 * arbitrarily that DMA interrupts should have higher 279 * precedence than SCSI interrupts. 280 */ 281 found = 0; 282 if (stat & ISTR_E_INT) { 283 found++; 284 285 sdp->CINT = 1; /* clear possible interrupt */ 286 287 /* 288 * check for SCSI ints in the same go and 289 * eventually save an interrupt 290 */ 291 } 292 293 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS) 294 found += sbicintr(dev); 295 return(found); 296 } 297 298 299 int 300 atzsc_dmanext(dev) 301 struct sbic_softc *dev; 302 { 303 volatile struct sdmac *sdp; 304 305 sdp = dev->sc_cregs; 306 307 if (dev->sc_cur > dev->sc_last) { 308 /* shouldn't happen !! */ 309 printf("atzsc_dmanext at end !!!\n"); 310 atzsc_dmastop(dev); 311 return(0); 312 } 313 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 314 /* 315 * only FLUSH if terminal count not enabled, 316 * and reading from peripheral 317 */ 318 sdp->FLUSH = 1; 319 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 320 ; 321 } 322 /* 323 * clear possible interrupt and stop dma 324 */ 325 sdp->CINT = 1; /* clear possible interrupt */ 326 sdp->SP_DMA = 1; /* stop dma */ 327 sdp->CNTR = dev->sc_dmacmd; 328 sdp->ACR = (u_int)dev->sc_cur->dc_addr; 329 sdp->ST_DMA = 1; 330 331 dev->sc_tcnt = dev->sc_cur->dc_count << 1; 332 return(dev->sc_tcnt); 333 } 334 335 #ifdef DEBUG 336 void 337 atzsc_dump() 338 { 339 extern struct cfdriver atzsc_cd; 340 int i; 341 342 for (i = 0; i < atzsc_cd.cd_ndevs; ++i) 343 if (atzsc_cd.cd_devs[i]) 344 sbic_dump(atzsc_cd.cd_devs[i]); 345 } 346 #endif 347