1 /* $NetBSD: ahsc.c,v 1.34 2004/02/13 11:36:09 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)dma.c 32 */ 33 34 /* 35 * Copyright (c) 1994 Christian E. Hopps 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)dma.c 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: ahsc.c,v 1.34 2004/02/13 11:36:09 wiz Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/kernel.h> 74 #include <sys/device.h> 75 #include <dev/scsipi/scsi_all.h> 76 #include <dev/scsipi/scsipi_all.h> 77 #include <dev/scsipi/scsiconf.h> 78 #include <amiga/amiga/custom.h> 79 #include <amiga/amiga/cc.h> 80 #include <amiga/amiga/cfdev.h> 81 #include <amiga/amiga/device.h> 82 #include <amiga/amiga/isr.h> 83 #include <amiga/dev/dmavar.h> 84 #include <amiga/dev/sbicreg.h> 85 #include <amiga/dev/sbicvar.h> 86 #include <amiga/dev/ahscreg.h> 87 #include <amiga/dev/zbusvar.h> 88 89 #include <machine/cpu.h> 90 91 void ahscattach(struct device *, struct device *, void *); 92 int ahscmatch(struct device *, struct cfdata *, void *); 93 94 void ahsc_enintr(struct sbic_softc *); 95 void ahsc_dmastop(struct sbic_softc *); 96 int ahsc_dmanext(struct sbic_softc *); 97 int ahsc_dmaintr(void *); 98 int ahsc_dmago(struct sbic_softc *, char *, int, int); 99 100 #ifdef DEBUG 101 void ahsc_dump(void); 102 #endif 103 104 #ifdef DEBUG 105 int ahsc_dmadebug = 0; 106 #endif 107 108 CFATTACH_DECL(ahsc, sizeof(struct sbic_softc), 109 ahscmatch, ahscattach, NULL, NULL); 110 111 /* 112 * if we are an A3000 we are here. 113 */ 114 int 115 ahscmatch(struct device *pdp, struct cfdata *cfp, void *auxp) 116 { 117 char *mbusstr; 118 119 mbusstr = auxp; 120 if (is_a3000() && matchname(auxp, "ahsc")) 121 return(1); 122 return(0); 123 } 124 125 void 126 ahscattach(struct device *pdp, struct device *dp, void *auxp) 127 { 128 volatile struct sdmac *rp; 129 struct sbic_softc *sc = (struct sbic_softc *)dp; 130 struct cfdev *cdp, *ecdp; 131 struct scsipi_adapter *adapt = &sc->sc_adapter; 132 struct scsipi_channel *chan = &sc->sc_channel; 133 134 ecdp = &cfdev[ncfdev]; 135 136 for (cdp = cfdev; cdp < ecdp; cdp++) { 137 if (cdp->rom.manid == 8738 && 138 cdp->rom.prodid == 35) 139 break; 140 } 141 142 sc->sc_cregs = rp = ztwomap(0xdd0000); 143 /* 144 * disable ints and reset bank register 145 */ 146 rp->CNTR = CNTR_PDMD; 147 rp->DAWR = DAWR_AHSC; 148 sc->sc_enintr = ahsc_enintr; 149 sc->sc_dmago = ahsc_dmago; 150 sc->sc_dmanext = ahsc_dmanext; 151 sc->sc_dmastop = ahsc_dmastop; 152 sc->sc_dmacmd = 0; 153 154 /* 155 * everything is a valid DMA address 156 */ 157 sc->sc_dmamask = 0; 158 159 if (cdp < ecdp) { 160 sc->sc_sbic.sbic_asr_p = ((vu_char *)rp + 0x43); 161 sc->sc_sbic.sbic_value_p = ((vu_char *)rp + 0x47); 162 printf(": modified for Apollo CPU board\n"); 163 } else { 164 sc->sc_sbic.sbic_asr_p = ((vu_char *)rp + 0x41); 165 sc->sc_sbic.sbic_value_p = ((vu_char *)rp + 0x43); 166 printf("\n"); 167 } 168 169 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143; 170 171 /* 172 * Fill in the scsipi_adapter. 173 */ 174 memset(adapt, 0, sizeof(*adapt)); 175 adapt->adapt_dev = &sc->sc_dev; 176 adapt->adapt_nchannels = 1; 177 adapt->adapt_openings = 7; 178 adapt->adapt_max_periph = 1; 179 adapt->adapt_request = sbic_scsipi_request; 180 adapt->adapt_minphys = sbic_minphys; 181 182 /* 183 * Fill in the scsipi_channel. 184 */ 185 memset(chan, 0, sizeof(*chan)); 186 chan->chan_adapter = adapt; 187 chan->chan_bustype = &scsi_bustype; 188 chan->chan_channel = 0; 189 chan->chan_ntargets = 8; 190 chan->chan_nluns = 8; 191 chan->chan_id = 7; 192 193 sbicinit(sc); 194 195 sc->sc_isr.isr_intr = ahsc_dmaintr; 196 sc->sc_isr.isr_arg = sc; 197 sc->sc_isr.isr_ipl = 2; 198 add_isr (&sc->sc_isr); 199 200 /* 201 * attach all scsi units on us 202 */ 203 config_found(dp, chan, scsiprint); 204 } 205 206 void 207 ahsc_enintr(struct sbic_softc *dev) 208 { 209 volatile struct sdmac *sdp; 210 211 sdp = dev->sc_cregs; 212 213 dev->sc_flags |= SBICF_INTR; 214 sdp->CNTR = CNTR_PDMD | CNTR_INTEN; 215 } 216 217 int 218 ahsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags) 219 { 220 volatile struct sdmac *sdp; 221 222 sdp = dev->sc_cregs; 223 /* 224 * Set up the command word based on flags 225 */ 226 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN; 227 if ((flags & DMAGO_READ) == 0) 228 dev->sc_dmacmd |= CNTR_DDIR; 229 #ifdef DEBUG 230 if (ahsc_dmadebug & DDB_IO) 231 printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd); 232 #endif 233 234 dev->sc_flags |= SBICF_INTR; 235 sdp->CNTR = dev->sc_dmacmd; 236 sdp->ACR = (u_int) dev->sc_cur->dc_addr; 237 sdp->ST_DMA = 1; 238 239 return(dev->sc_tcnt); 240 } 241 242 void 243 ahsc_dmastop(struct sbic_softc *dev) 244 { 245 volatile struct sdmac *sdp; 246 int s; 247 248 sdp = dev->sc_cregs; 249 250 #ifdef DEBUG 251 if (ahsc_dmadebug & DDB_FOLLOW) 252 printf("ahsc_dmastop()\n"); 253 #endif 254 if (dev->sc_dmacmd) { 255 s = splbio(); 256 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 257 /* 258 * only FLUSH if terminal count not enabled, 259 * and reading from peripheral 260 */ 261 sdp->FLUSH = 1; 262 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 263 ; 264 } 265 /* 266 * clear possible interrupt and stop DMA 267 */ 268 sdp->CINT = 1; 269 sdp->SP_DMA = 1; 270 dev->sc_dmacmd = 0; 271 splx(s); 272 } 273 } 274 275 int 276 ahsc_dmaintr(void *arg) 277 { 278 struct sbic_softc *dev = arg; 279 volatile struct sdmac *sdp; 280 int stat, found; 281 282 sdp = dev->sc_cregs; 283 stat = sdp->ISTR; 284 285 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0) 286 return (0); 287 288 #ifdef DEBUG 289 if (ahsc_dmadebug & DDB_FOLLOW) 290 printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat); 291 #endif 292 293 /* 294 * both, SCSI and DMA interrupts arrive here. I chose 295 * arbitrarily that DMA interrupts should have higher 296 * precedence than SCSI interrupts. 297 */ 298 found = 0; 299 if (stat & ISTR_E_INT) { 300 ++found; 301 302 sdp->CINT = 1; /* clear possible interrupt */ 303 304 /* 305 * check for SCSI ints in the same go and 306 * eventually save an interrupt 307 */ 308 } 309 310 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS) 311 found += sbicintr(dev); 312 return(found); 313 } 314 315 316 int 317 ahsc_dmanext(struct sbic_softc *dev) 318 { 319 volatile struct sdmac *sdp; 320 321 sdp = dev->sc_cregs; 322 323 if (dev->sc_cur > dev->sc_last) { 324 /* shouldn't happen !! */ 325 printf("ahsc_dmanext at end !!!\n"); 326 ahsc_dmastop(dev); 327 return(0); 328 } 329 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 330 /* 331 * only FLUSH if terminal count not enabled, 332 * and reading from peripheral 333 */ 334 sdp->FLUSH = 1; 335 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 336 ; 337 } 338 /* 339 * clear possible interrupt and stop DMA 340 */ 341 sdp->CINT = 1; /* clear possible interrupt */ 342 sdp->SP_DMA = 1; /* stop DMA */ 343 sdp->CNTR = dev->sc_dmacmd; 344 sdp->ACR = (u_int)dev->sc_cur->dc_addr; 345 sdp->ST_DMA = 1; 346 347 dev->sc_tcnt = dev->sc_cur->dc_count << 1; 348 return(dev->sc_tcnt); 349 } 350 351 #ifdef DEBUG 352 void 353 ahsc_dump(void) 354 { 355 extern struct cfdriver ahsc_cd; 356 int i; 357 358 for (i = 0; i < ahsc_cd.cd_ndevs; ++i) 359 if (ahsc_cd.cd_devs[i]) 360 sbic_dump(ahsc_cd.cd_devs[i]); 361 } 362 #endif 363