1 /* $NetBSD: ahsc.c,v 1.29 2002/01/28 09:56:51 aymeric 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 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: ahsc.c,v 1.29 2002/01/28 09:56:51 aymeric Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/device.h> 46 #include <dev/scsipi/scsi_all.h> 47 #include <dev/scsipi/scsipi_all.h> 48 #include <dev/scsipi/scsiconf.h> 49 #include <amiga/amiga/custom.h> 50 #include <amiga/amiga/cc.h> 51 #include <amiga/amiga/cfdev.h> 52 #include <amiga/amiga/device.h> 53 #include <amiga/amiga/isr.h> 54 #include <amiga/dev/dmavar.h> 55 #include <amiga/dev/sbicreg.h> 56 #include <amiga/dev/sbicvar.h> 57 #include <amiga/dev/ahscreg.h> 58 #include <amiga/dev/zbusvar.h> 59 60 #include <machine/cpu.h> 61 62 void ahscattach(struct device *, struct device *, void *); 63 int ahscmatch(struct device *, struct cfdata *, void *); 64 65 void ahsc_enintr(struct sbic_softc *); 66 void ahsc_dmastop(struct sbic_softc *); 67 int ahsc_dmanext(struct sbic_softc *); 68 int ahsc_dmaintr(void *); 69 int ahsc_dmago(struct sbic_softc *, char *, int, int); 70 71 #ifdef DEBUG 72 void ahsc_dump(void); 73 #endif 74 75 #ifdef DEBUG 76 int ahsc_dmadebug = 0; 77 #endif 78 79 struct cfattach ahsc_ca = { 80 sizeof(struct sbic_softc), ahscmatch, ahscattach 81 }; 82 83 /* 84 * if we are an A3000 we are here. 85 */ 86 int 87 ahscmatch(struct device *pdp, struct cfdata *cfp, void *auxp) 88 { 89 char *mbusstr; 90 91 mbusstr = auxp; 92 if (is_a3000() && matchname(auxp, "ahsc")) 93 return(1); 94 return(0); 95 } 96 97 void 98 ahscattach(struct device *pdp, struct device *dp, void *auxp) 99 { 100 volatile struct sdmac *rp; 101 struct sbic_softc *sc = (struct sbic_softc *)dp; 102 struct cfdev *cdp, *ecdp; 103 struct scsipi_adapter *adapt = &sc->sc_adapter; 104 struct scsipi_channel *chan = &sc->sc_channel; 105 106 ecdp = &cfdev[ncfdev]; 107 108 for (cdp = cfdev; cdp < ecdp; cdp++) { 109 if (cdp->rom.manid == 8738 && 110 cdp->rom.prodid == 35) 111 break; 112 } 113 114 sc->sc_cregs = rp = ztwomap(0xdd0000); 115 /* 116 * disable ints and reset bank register 117 */ 118 rp->CNTR = CNTR_PDMD; 119 rp->DAWR = DAWR_AHSC; 120 sc->sc_enintr = ahsc_enintr; 121 sc->sc_dmago = ahsc_dmago; 122 sc->sc_dmanext = ahsc_dmanext; 123 sc->sc_dmastop = ahsc_dmastop; 124 sc->sc_dmacmd = 0; 125 126 /* 127 * eveything is a valid dma address 128 */ 129 sc->sc_dmamask = 0; 130 131 if (cdp < ecdp) { 132 sc->sc_sbic.sbic_asr_p = ((vu_char *)rp + 0x43); 133 sc->sc_sbic.sbic_value_p = ((vu_char *)rp + 0x47); 134 printf(": modified for Apollo cpu board\n"); 135 } else { 136 sc->sc_sbic.sbic_asr_p = ((vu_char *)rp + 0x41); 137 sc->sc_sbic.sbic_value_p = ((vu_char *)rp + 0x43); 138 printf("\n"); 139 } 140 141 sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143; 142 143 /* 144 * Fill in the scsipi_adapter. 145 */ 146 memset(adapt, 0, sizeof(*adapt)); 147 adapt->adapt_dev = &sc->sc_dev; 148 adapt->adapt_nchannels = 1; 149 adapt->adapt_openings = 7; 150 adapt->adapt_max_periph = 1; 151 adapt->adapt_request = sbic_scsipi_request; 152 adapt->adapt_minphys = sbic_minphys; 153 154 /* 155 * Fill in the scsipi_channel. 156 */ 157 memset(chan, 0, sizeof(*chan)); 158 chan->chan_adapter = adapt; 159 chan->chan_bustype = &scsi_bustype; 160 chan->chan_channel = 0; 161 chan->chan_ntargets = 8; 162 chan->chan_nluns = 8; 163 chan->chan_id = 7; 164 165 sbicinit(sc); 166 167 sc->sc_isr.isr_intr = ahsc_dmaintr; 168 sc->sc_isr.isr_arg = sc; 169 sc->sc_isr.isr_ipl = 2; 170 add_isr (&sc->sc_isr); 171 172 /* 173 * attach all scsi units on us 174 */ 175 config_found(dp, chan, scsiprint); 176 } 177 178 void 179 ahsc_enintr(struct sbic_softc *dev) 180 { 181 volatile struct sdmac *sdp; 182 183 sdp = dev->sc_cregs; 184 185 dev->sc_flags |= SBICF_INTR; 186 sdp->CNTR = CNTR_PDMD | CNTR_INTEN; 187 } 188 189 int 190 ahsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags) 191 { 192 volatile struct sdmac *sdp; 193 194 sdp = dev->sc_cregs; 195 /* 196 * Set up the command word based on flags 197 */ 198 dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN; 199 if ((flags & DMAGO_READ) == 0) 200 dev->sc_dmacmd |= CNTR_DDIR; 201 #ifdef DEBUG 202 if (ahsc_dmadebug & DDB_IO) 203 printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd); 204 #endif 205 206 dev->sc_flags |= SBICF_INTR; 207 sdp->CNTR = dev->sc_dmacmd; 208 sdp->ACR = (u_int) dev->sc_cur->dc_addr; 209 sdp->ST_DMA = 1; 210 211 return(dev->sc_tcnt); 212 } 213 214 void 215 ahsc_dmastop(struct sbic_softc *dev) 216 { 217 volatile struct sdmac *sdp; 218 int s; 219 220 sdp = dev->sc_cregs; 221 222 #ifdef DEBUG 223 if (ahsc_dmadebug & DDB_FOLLOW) 224 printf("ahsc_dmastop()\n"); 225 #endif 226 if (dev->sc_dmacmd) { 227 s = splbio(); 228 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 229 /* 230 * only FLUSH if terminal count not enabled, 231 * and reading from peripheral 232 */ 233 sdp->FLUSH = 1; 234 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 235 ; 236 } 237 /* 238 * clear possible interrupt and stop dma 239 */ 240 sdp->CINT = 1; 241 sdp->SP_DMA = 1; 242 dev->sc_dmacmd = 0; 243 splx(s); 244 } 245 } 246 247 int 248 ahsc_dmaintr(void *arg) 249 { 250 struct sbic_softc *dev = arg; 251 volatile struct sdmac *sdp; 252 int stat, found; 253 254 sdp = dev->sc_cregs; 255 stat = sdp->ISTR; 256 257 if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0) 258 return (0); 259 260 #ifdef DEBUG 261 if (ahsc_dmadebug & DDB_FOLLOW) 262 printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat); 263 #endif 264 265 /* 266 * both, SCSI and DMA interrupts arrive here. I chose 267 * arbitrarily that DMA interrupts should have higher 268 * precedence than SCSI interrupts. 269 */ 270 found = 0; 271 if (stat & ISTR_E_INT) { 272 ++found; 273 274 sdp->CINT = 1; /* clear possible interrupt */ 275 276 /* 277 * check for SCSI ints in the same go and 278 * eventually save an interrupt 279 */ 280 } 281 282 if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS) 283 found += sbicintr(dev); 284 return(found); 285 } 286 287 288 int 289 ahsc_dmanext(struct sbic_softc *dev) 290 { 291 volatile struct sdmac *sdp; 292 293 sdp = dev->sc_cregs; 294 295 if (dev->sc_cur > dev->sc_last) { 296 /* shouldn't happen !! */ 297 printf("ahsc_dmanext at end !!!\n"); 298 ahsc_dmastop(dev); 299 return(0); 300 } 301 if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) { 302 /* 303 * only FLUSH if terminal count not enabled, 304 * and reading from peripheral 305 */ 306 sdp->FLUSH = 1; 307 while ((sdp->ISTR & ISTR_FE_FLG) == 0) 308 ; 309 } 310 /* 311 * clear possible interrupt and stop dma 312 */ 313 sdp->CINT = 1; /* clear possible interrupt */ 314 sdp->SP_DMA = 1; /* stop dma */ 315 sdp->CNTR = dev->sc_dmacmd; 316 sdp->ACR = (u_int)dev->sc_cur->dc_addr; 317 sdp->ST_DMA = 1; 318 319 dev->sc_tcnt = dev->sc_cur->dc_count << 1; 320 return(dev->sc_tcnt); 321 } 322 323 #ifdef DEBUG 324 void 325 ahsc_dump(void) 326 { 327 extern struct cfdriver ahsc_cd; 328 int i; 329 330 for (i = 0; i < ahsc_cd.cd_ndevs; ++i) 331 if (ahsc_cd.cd_devs[i]) 332 sbic_dump(ahsc_cd.cd_devs[i]); 333 } 334 #endif 335