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