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