Home | History | Annotate | Line # | Download | only in dev
ahsc.c revision 1.1
      1 /*
      2  * Copyright (c) 1994 Christian E. Hopps
      3  * Copyright (c) 1982, 1990 The Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)dma.c
     35  *	$Id: ahsc.c,v 1.1 1994/05/08 05:52:54 chopps Exp $
     36  */
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <scsi/scsi_all.h>
     42 #include <scsi/scsiconf.h>
     43 #include <amiga/amiga/custom.h>
     44 #include <amiga/amiga/cc.h>
     45 #include <amiga/amiga/device.h>
     46 #include <amiga/dev/dmavar.h>
     47 #include <amiga/dev/sbicreg.h>
     48 #include <amiga/dev/sbicvar.h>
     49 #include <amiga/dev/ahscreg.h>
     50 #include <amiga/dev/ztwobusvar.h>
     51 
     52 int ahscprint __P((void *auxp, char *));
     53 void ahscattach __P((struct device *, struct device *, void *));
     54 int ahscmatch __P((struct device *, struct cfdata *, void *));
     55 
     56 void ahsc_dmafree __P((struct sbic_softc *));
     57 void ahsc_dmastop __P((struct sbic_softc *));
     58 int ahsc_dmanext __P((struct sbic_softc *));
     59 int ahsc_dmaintr __P((void));
     60 int ahsc_dmago __P((struct sbic_softc *, char *, int, int));
     61 
     62 struct scsi_adapter ahsc_scsiswitch = {
     63 	sbic_scsicmd,
     64 	sbic_minphys,
     65 	0,			/* no lun support */
     66 	0,			/* no lun support */
     67 	sbic_adinfo,
     68 	"ahsc",
     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 	"ahsc",
     77 	0,
     78 };
     79 
     80 
     81 #ifdef DEBUG
     82 void	ahsc_dmatimeout __P((void *));
     83 int	ahsc_dmadebug = 0;
     84 #endif
     85 
     86 struct cfdriver ahsccd = {
     87 	NULL, "ahsc", ahscmatch, ahscattach,
     88 	DV_DULL, sizeof(struct sbic_softc), NULL, 0 };
     89 
     90 /*
     91  * if we are an A3000 we are here.
     92  */
     93 int
     94 ahscmatch(pdp, cdp, auxp)
     95 	struct device *pdp;
     96 	struct cfdata *cdp;
     97 	void *auxp;
     98 {
     99 	char *mbusstr;
    100 
    101 	mbusstr = auxp;
    102 	if (is_a3000() && matchname(auxp, "ahsc"))
    103 		return(1);
    104 	return(0);
    105 }
    106 
    107 void
    108 ahscattach(pdp, dp, auxp)
    109 	struct device *pdp, *dp;
    110 	void *auxp;
    111 {
    112 	volatile struct sdmac *rp;
    113 	struct sbic_softc *sc;
    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(ahsc_dmatimeout, 0, 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 	sbicreset(sc);
    140 
    141 	sc->sc_link.adapter_softc = sc;
    142 	sc->sc_link.adapter_targ = 7;
    143 	sc->sc_link.adapter = &ahsc_scsiswitch;
    144 	sc->sc_link.device = &ahsc_scsidev;
    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(i);
    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