Home | History | Annotate | Line # | Download | only in dev
ahsc.c revision 1.9
      1 /*	$NetBSD: ahsc.c,v 1.9 1995/08/18 15:27:48 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/amiga/isr.h>
     48 #include <amiga/dev/dmavar.h>
     49 #include <amiga/dev/sbicreg.h>
     50 #include <amiga/dev/sbicvar.h>
     51 #include <amiga/dev/ahscreg.h>
     52 #include <amiga/dev/zbusvar.h>
     53 
     54 int ahscprint __P((void *auxp, char *));
     55 void ahscattach __P((struct device *, struct device *, void *));
     56 int ahscmatch __P((struct device *, struct cfdata *, void *));
     57 
     58 void ahsc_dmafree __P((struct sbic_softc *));
     59 void ahsc_dmastop __P((struct sbic_softc *));
     60 int ahsc_dmanext __P((struct sbic_softc *));
     61 int ahsc_dmaintr __P((struct sbic_softc *));
     62 int ahsc_dmago __P((struct sbic_softc *, char *, int, int));
     63 
     64 struct scsi_adapter ahsc_scsiswitch = {
     65 	sbic_scsicmd,
     66 	sbic_minphys,
     67 	0,			/* no lun support */
     68 	0,			/* no lun support */
     69 };
     70 
     71 struct scsi_device ahsc_scsidev = {
     72 	NULL,		/* use default error handler */
     73 	NULL,		/* do not have a start functio */
     74 	NULL,		/* have no async handler */
     75 	NULL,		/* Use default done routine */
     76 };
     77 
     78 
     79 #ifdef DEBUG
     80 void	ahsc_dmatimeout __P((struct sbic_softc *));
     81 int	ahsc_dmadebug = 0;
     82 #endif
     83 
     84 struct cfdriver ahsccd = {
     85 	NULL, "ahsc", (cfmatch_t)ahscmatch, ahscattach,
     86 	DV_DULL, sizeof(struct sbic_softc), NULL, 0 };
     87 
     88 /*
     89  * if we are an A3000 we are here.
     90  */
     91 int
     92 ahscmatch(pdp, cdp, auxp)
     93 	struct device *pdp;
     94 	struct cfdata *cdp;
     95 	void *auxp;
     96 {
     97 	char *mbusstr;
     98 
     99 	mbusstr = auxp;
    100 	if (is_a3000() && matchname(auxp, "ahsc"))
    101 		return(1);
    102 	return(0);
    103 }
    104 
    105 void
    106 ahscattach(pdp, dp, auxp)
    107 	struct device *pdp, *dp;
    108 	void *auxp;
    109 {
    110 	volatile struct sdmac *rp;
    111 	struct sbic_softc *sc;
    112 
    113 	printf("\n");
    114 
    115 	sc = (struct sbic_softc *)dp;
    116 	sc->sc_cregs = rp = ztwomap(0xdd0000);
    117 	/*
    118 	 * disable ints and reset bank register
    119 	 */
    120 	rp->CNTR = CNTR_PDMD;
    121 	rp->DAWR = DAWR_AHSC;
    122 	sc->sc_dmafree = ahsc_dmafree;
    123 	sc->sc_dmago = ahsc_dmago;
    124 	sc->sc_dmanext = ahsc_dmanext;
    125 	sc->sc_dmastop = ahsc_dmastop;
    126 	sc->sc_dmacmd = 0;
    127 
    128 #ifdef DEBUG
    129 	/* make sure timeout is really not needed */
    130 	timeout((void *)ahsc_dmatimeout, sc, 30 * hz);
    131 #endif
    132 	/*
    133 	 * eveything is a valid dma address
    134 	 */
    135 	sc->sc_dmamask = 0;
    136 	sc->sc_sbicp = (sbic_regmap_p) ((int)rp + 0x41);
    137 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143;
    138 
    139 	sc->sc_link.adapter_softc = sc;
    140 	sc->sc_link.adapter_target = 7;
    141 	sc->sc_link.adapter = &ahsc_scsiswitch;
    142 	sc->sc_link.device = &ahsc_scsidev;
    143 	sc->sc_link.openings = 2;
    144 
    145 	sbicinit(sc);
    146 
    147 	sc->sc_isr.isr_intr = ahsc_dmaintr;
    148 	sc->sc_isr.isr_arg = sc;
    149 	sc->sc_isr.isr_ipl = 2;
    150 	add_isr (&sc->sc_isr);
    151 
    152 	/*
    153 	 * attach all scsi units on us
    154 	 */
    155 	config_found(dp, &sc->sc_link, ahscprint);
    156 }
    157 
    158 /*
    159  * print diag if pnp is NULL else just extra
    160  */
    161 int
    162 ahscprint(auxp, pnp)
    163 	void *auxp;
    164 	char *pnp;
    165 {
    166 	if (pnp == NULL)
    167 		return(UNCONF);
    168 	return(QUIET);
    169 }
    170 
    171 
    172 void
    173 ahsc_dmafree(dev)
    174 	struct sbic_softc *dev;
    175 {
    176 	volatile struct sdmac *sdp;
    177 	int s;
    178 
    179 	sdp = dev->sc_cregs;
    180 
    181 	s = splbio();
    182 #ifdef DEBUG
    183 	dev->sc_dmatimo = 0;
    184 #endif
    185 	if (dev->sc_dmacmd) {
    186 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    187 			/*
    188 			 * only FLUSH if terminal count not enabled,
    189 			 * and reading from peripheral
    190 			 */
    191 			sdp->FLUSH = 1;
    192 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    193 				;
    194 		}
    195 		/*
    196 		 * clear possible interrupt and stop dma
    197 		 */
    198 		sdp->CINT = 1;
    199 		sdp->SP_DMA = 1;
    200 		dev->sc_dmacmd = 0;
    201 	}
    202 	/*
    203 	 * disable interrupts
    204 	 */
    205 	sdp->CNTR = CNTR_PDMD;	/* disable interrupts from dma/scsi */
    206 	dev->sc_flags &= ~SBICF_INTR;
    207 	splx(s);
    208 }
    209 
    210 int
    211 ahsc_dmago(dev, addr, count, flags)
    212 	struct sbic_softc *dev;
    213 	char *addr;
    214 	int count, flags;
    215 {
    216 	volatile struct sdmac *sdp;
    217 
    218 	sdp = dev->sc_cregs;
    219 	/*
    220 	 * Set up the command word based on flags
    221 	 */
    222 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
    223 	if ((flags & DMAGO_READ) == 0)
    224 		dev->sc_dmacmd |= CNTR_DDIR;
    225 #ifdef DEBUG
    226 	if (ahsc_dmadebug & DDB_IO)
    227 		printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd);
    228 	dev->sc_dmatimo = 1;
    229 #endif
    230 
    231 	dev->sc_flags |= SBICF_INTR;
    232 	sdp->CNTR = dev->sc_dmacmd;
    233 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    234 	sdp->ST_DMA = 1;
    235 
    236 	return(dev->sc_tcnt);
    237 }
    238 
    239 void
    240 ahsc_dmastop(dev)
    241 	struct sbic_softc *dev;
    242 {
    243 	volatile struct sdmac *sdp;
    244 	int s;
    245 
    246 	sdp = dev->sc_cregs;
    247 
    248 #ifdef DEBUG
    249 	if (ahsc_dmadebug & DDB_FOLLOW)
    250 		printf("ahsc_dmastop()\n");
    251 	dev->sc_dmatimo = 0;
    252 #endif
    253 	if (dev->sc_dmacmd) {
    254 		s = splbio();
    255 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    256 			/*
    257 			 * only FLUSH if terminal count not enabled,
    258 			 * and reading from peripheral
    259 			 */
    260 			sdp->FLUSH = 1;
    261 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    262 				;
    263 		}
    264 		/*
    265 		 * clear possible interrupt and stop dma
    266 		 */
    267 		sdp->CINT = 1;
    268 		sdp->SP_DMA = 1;
    269 		dev->sc_dmacmd = 0;
    270 		splx(s);
    271 	}
    272 }
    273 
    274 int
    275 ahsc_dmaintr(dev)
    276 	struct sbic_softc *dev;
    277 {
    278 	volatile struct sdmac *sdp;
    279 	int stat, found;
    280 
    281 	sdp = dev->sc_cregs;
    282 	stat = sdp->ISTR;
    283 
    284 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
    285 		return (0);
    286 
    287 #ifdef DEBUG
    288 	if (ahsc_dmadebug & DDB_FOLLOW)
    289 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
    290 #endif
    291 
    292 	/*
    293 	 * both, SCSI and DMA interrupts arrive here. I chose
    294 	 * arbitrarily that DMA interrupts should have higher
    295 	 * precedence than SCSI interrupts.
    296 	 */
    297 	found = 0;
    298 	if (stat & ISTR_E_INT) {
    299 		++found;
    300 
    301 		sdp->CINT = 1;	/* clear possible interrupt */
    302 
    303 		/*
    304 		 * check for SCSI ints in the same go and
    305 		 * eventually save an interrupt
    306 		 */
    307 	}
    308 
    309 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
    310 		found += sbicintr(dev);
    311 	return(found);
    312 }
    313 
    314 
    315 int
    316 ahsc_dmanext(dev)
    317 	struct sbic_softc *dev;
    318 {
    319 	volatile struct sdmac *sdp;
    320 	int i, stat;
    321 
    322 	sdp = dev->sc_cregs;
    323 
    324 	if (dev->sc_cur > dev->sc_last) {
    325 		/* shouldn't happen !! */
    326 		printf("ahsc_dmanext at end !!!\n");
    327 		ahsc_dmastop(dev);
    328 		return(0);
    329 	}
    330 #ifdef DEBUG
    331 	dev->sc_dmatimo = 1;
    332 #endif
    333 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    334 		  /*
    335 		   * only FLUSH if terminal count not enabled,
    336 		   * and reading from peripheral
    337 		   */
    338 		sdp->FLUSH = 1;
    339 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    340 			;
    341         }
    342 	/*
    343 	 * clear possible interrupt and stop dma
    344 	 */
    345 	sdp->CINT = 1;	/* clear possible interrupt */
    346 	sdp->SP_DMA = 1;	/* stop dma */
    347 	sdp->CNTR = dev->sc_dmacmd;
    348 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
    349 	sdp->ST_DMA = 1;
    350 
    351 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
    352 	return(dev->sc_tcnt);
    353 }
    354 
    355 #ifdef DEBUG
    356 /*ARGSUSED*/
    357 void
    358 ahsc_dmatimeout(sc)
    359 	struct sbic_softc *sc;
    360 {
    361 	int s;
    362 
    363 	s = splbio();
    364 	if (sc->sc_dmatimo) {
    365 		if (sc->sc_dmatimo > 1)
    366 			printf("%s: dma timeout #%d\n",
    367 			    sc->sc_dev.dv_xname, sc->sc_dmatimo - 1);
    368 		sc->sc_dmatimo++;
    369 	}
    370 	splx(s);
    371 	timeout((void *)ahsc_dmatimeout, sc, 30 * hz);
    372 }
    373 #endif
    374