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