Home | History | Annotate | Line # | Download | only in dev
gtsc.c revision 1.6
      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: gtsc.c,v 1.6 1994/10/06 19:06:41 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/gtscreg.h>
     50 #include <amiga/dev/ztwobusvar.h>
     51 #include <amiga/dev/gvpbusvar.h>
     52 
     53 void gtscattach __P((struct device *, struct device *, void *));
     54 int gtscmatch __P((struct device *, struct cfdata *, void *));
     55 int gtscprint __P((void *auxp, char *));
     56 
     57 void gtsc_dmafree __P((struct sbic_softc *));
     58 void gtsc_dmastop __P((struct sbic_softc *));
     59 int gtsc_dmanext __P((struct sbic_softc *));
     60 int gtsc_dmaintr __P((void));
     61 int gtsc_dmago __P((struct sbic_softc *, char *, int, int));
     62 
     63 struct scsi_adapter gtsc_scsiswitch = {
     64 	sbic_scsicmd,
     65 	sbic_minphys,
     66 	0,			/* no lun support */
     67 	0,			/* no lun support */
     68 	sbic_adinfo,
     69 	"gtsc",
     70 };
     71 
     72 struct scsi_device gtsc_scsidev = {
     73 	NULL,		/* use default error handler */
     74 	NULL,		/* have a queue served by this ??? */
     75 	NULL,		/* have no async handler ??? */
     76 	NULL,		/* Use default done routine */
     77 	"gtsc",
     78 	0,
     79 };
     80 
     81 int gtsc_maxdma = 0;	/* Maximum size per DMA transfer */
     82 int gtsc_dmamask = 0;
     83 int gtsc_dmabounce = 0;
     84 
     85 #ifdef DEBUG
     86 void gtsc_dmatimeout __P((void *));
     87 int gtsc_debug = 0;
     88 #endif
     89 
     90 struct cfdriver gtsccd = {
     91 	NULL, "gtsc", gtscmatch, gtscattach,
     92 	DV_DULL, sizeof(struct sbic_softc), NULL, 0 };
     93 
     94 int
     95 gtscmatch(pdp, cdp, auxp)
     96 	struct device *pdp;
     97 	struct cfdata *cdp;
     98 	void *auxp;
     99 {
    100 	struct gvpbus_args *gap;
    101 
    102 	gap = auxp;
    103 	if (gap->flags & GVP_SCSI)
    104 		return(1);
    105 	return(0);
    106 }
    107 
    108 /*
    109  * attach all devices on our board.
    110  */
    111 void
    112 gtscattach(pdp, dp, auxp)
    113 	struct device *pdp, *dp;
    114 	void *auxp;
    115 {
    116 	volatile struct sdmac *rp;
    117 	struct gvpbus_args *gap;
    118 	struct sbic_softc *sc;
    119 
    120 	gap = auxp;
    121 	sc = (struct sbic_softc *)dp;
    122 	sc->sc_cregs = rp = gap->zargs.va;
    123 
    124 	/*
    125 	 * disable ints and reset bank register
    126 	 */
    127 	rp->CNTR = 0;
    128 	if ((gap->flags & GVP_NOBANK) == 0)
    129 		rp->bank = 0;
    130 
    131 	sc->sc_dmago =  gtsc_dmago;
    132 	sc->sc_dmafree = gtsc_dmafree;
    133 	sc->sc_dmanext = gtsc_dmanext;
    134 	sc->sc_dmastop = gtsc_dmastop;
    135 	sc->sc_dmacmd = 0;
    136 
    137 #ifdef DEBUG
    138 	/* make sure timeout is really not needed */
    139 	timeout((void *)gtsc_dmatimeout, 0, 30 * hz);
    140 #endif
    141 
    142 	sc->sc_flags |= SBICF_BADDMA;
    143 	if (gtsc_dmamask)
    144 		sc->sc_dmamask = gtsc_dmamask;
    145 	else if (gap->flags & GVP_24BITDMA)
    146 		sc->sc_dmamask = ~0x00ffffff;
    147 	else if (gap->flags & GVP_25BITDMA)
    148 		sc->sc_dmamask = ~0x01ffffff;
    149 	else
    150 		sc->sc_dmamask = ~0x07ffffff;
    151 	printf(": dmamask 0x%x", ~sc->sc_dmamask);
    152 
    153 	if ((gap->flags & GVP_NOBANK) == 0)
    154 		sc->gtsc_bankmask = (~sc->sc_dmamask >> 18) & 0x01c0;
    155 
    156 
    157 	/*
    158 	 * if the user requests a bounce buffer or
    159 	 * the users kva space is not ztwo and dma needs it
    160 	 * try and allocate a bounce buffer.  If we allocate
    161 	 * one and it is in ztwo space leave maxdma to user
    162 	 * setting or default to MAXPHYS else the address must
    163 	 * be on the chip bus so decrease it to either the users
    164 	 * setting or 1024 bytes.
    165 	 *
    166 	 * XXX this needs to change if we move to multiple memory segments.
    167 	 */
    168 	if (gtsc_dmabounce || kvtop(sc) & sc->sc_dmamask) {
    169 		sc->sc_dmabuffer = (char *) alloc_z2mem(MAXPHYS);
    170 		if (isztwomem(sc->sc_dmabuffer))
    171 			printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer));
    172 		else if (gtsc_maxdma == 0) {
    173 			gtsc_maxdma = 1024;
    174 			printf(" bounce pa 0x%x",
    175 			    PREP_DMA_MEM(sc->sc_dmabuffer));
    176 		}
    177 	}
    178 	if (gtsc_maxdma == 0)
    179 		gtsc_maxdma = MAXPHYS;
    180 
    181 	printf(" maxdma %d\n", gtsc_maxdma);
    182 
    183 	sc->sc_sbicp = (sbic_regmap_p) ((int)rp + 0x61);
    184 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
    185 
    186 	sbicreset(sc);
    187 
    188 	sc->sc_link.adapter_softc = sc;
    189 	sc->sc_link.adapter_targ = 7;
    190 	sc->sc_link.adapter = &gtsc_scsiswitch;
    191 	sc->sc_link.device = &gtsc_scsidev;
    192 	TAILQ_INIT(&sc->sc_xslist);
    193 
    194 	custom.intreq = INTF_PORTS;
    195 	custom.intena = INTF_SETCLR | INTF_PORTS;
    196 
    197 	/*
    198 	 * attach all scsi units on us
    199 	 */
    200 	config_found(dp, &sc->sc_link, gtscprint);
    201 }
    202 
    203 /*
    204  * print diag if pnp is NULL else just extra
    205  */
    206 int
    207 gtscprint(auxp, pnp)
    208 	void *auxp;
    209 	char *pnp;
    210 {
    211 	if (pnp == NULL)
    212 		return(UNCONF);
    213 	return(QUIET);
    214 }
    215 
    216 void
    217 gtsc_dmafree(dev)
    218 	struct sbic_softc *dev;
    219 {
    220 	volatile struct sdmac *sdp;
    221 	int s;
    222 
    223 	sdp = dev->sc_cregs;
    224 
    225 	s = splbio();
    226 #ifdef DEBUG
    227 	dev->sc_dmatimo = 0;
    228 #endif
    229 	if (dev->sc_dmacmd) {
    230 		/*
    231 		 * clear possible interrupt and stop dma
    232 		 */
    233 		sdp->CNTR &= ~GVP_CNTR_INT_P;
    234 		sdp->SP_DMA = 1;
    235 		dev->sc_dmacmd = 0;
    236 	}
    237 #ifdef DEBUG
    238 	if (gtsc_debug & (DDB_IO | DDB_FOLLOW))
    239 		printf("gtsc_dmafree\n");
    240 #endif
    241 	/*
    242 	 * disable interrupts
    243 	 */
    244 	sdp->CNTR = 0;	/* disable interrupts from dma/sbic */
    245 	dev->sc_flags &= ~SBICF_INTR;
    246 	splx(s);
    247 }
    248 
    249 int
    250 gtsc_dmago(dev, addr, count, flags)
    251 	struct sbic_softc *dev;
    252 	char *addr;
    253 	int count, flags;
    254 {
    255 	volatile struct sdmac *sdp;
    256 
    257 	sdp = dev->sc_cregs;
    258 	/*
    259 	 * Set up the command word based on flags
    260 	 */
    261 	dev->sc_dmacmd = GVP_CNTR_INTEN;
    262 	if ((flags & DMAGO_READ) == 0)
    263 		dev->sc_dmacmd |= GVP_CNTR_DDIR;
    264 
    265 #ifdef DEBUG
    266 	if (gtsc_debug & DDB_IO)
    267 		printf("gtsc_dmago: cmd %x\n", dev->sc_dmacmd);
    268 	dev->sc_dmatimo = 1;
    269 #endif
    270 	dev->sc_flags |= SBICF_INTR;
    271 	sdp->CNTR = dev->sc_dmacmd;
    272 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    273 	if (dev->gtsc_bankmask)
    274 		sdp->bank =
    275 		    dev->gtsc_bankmask & (((u_int)dev->sc_cur->dc_addr) >> 18);
    276 	sdp->ST_DMA = 1;
    277 
    278 	/*
    279 	 * restrict transfer count to maximum
    280 	 */
    281 	if (dev->sc_tcnt > gtsc_maxdma)
    282 		dev->sc_tcnt = gtsc_maxdma;
    283 	return(dev->sc_tcnt);
    284 }
    285 
    286 void
    287 gtsc_dmastop(dev)
    288 	struct sbic_softc *dev;
    289 {
    290 	volatile struct sdmac *sdp;
    291 	int s;
    292 
    293 	sdp = dev->sc_cregs;
    294 
    295 #ifdef DEBUG
    296 	if (gtsc_debug & DDB_FOLLOW)
    297 		printf("gtsc_dmastop()\n");
    298 	dev->sc_dmatimo = 0;
    299 #endif
    300 	if (dev->sc_dmacmd) {
    301 		/*
    302 		 * clear possible interrupt and stop dma
    303 		 */
    304 		s = splbio();
    305 		sdp->CNTR &= ~GVP_CNTR_INT_P;
    306 		sdp->SP_DMA = 1;
    307 		dev->sc_dmacmd = 0;
    308 		splx(s);
    309 	}
    310 }
    311 
    312 int
    313 gtsc_dmaintr()
    314 {
    315 	volatile struct sdmac *sdp;
    316 	struct sbic_softc *dev;
    317 	int i, stat, found;
    318 
    319 	found = 0;
    320 	for (i = 0; i < gtsccd.cd_ndevs; i++) {
    321 		dev = gtsccd.cd_devs[i];
    322 		if (dev == NULL)
    323 			continue;
    324 		sdp = dev->sc_cregs;
    325 		stat = sdp->CNTR;
    326 		if ((stat & GVP_CNTR_INT_P) == 0)
    327 			continue;
    328 #ifdef DEBUG
    329 		if (gtsc_debug & DDB_FOLLOW)
    330 			printf("gtsc_dmaintr(%d, 0x%x) ", i, stat);
    331 #endif
    332 		if (dev->sc_flags & SBICF_INTR)
    333 			found += sbicintr(dev);
    334 	}
    335 	return(found);
    336 }
    337 
    338 
    339 int
    340 gtsc_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("gtsc_dmanext at end !!!\n");
    351 		gtsc_dmastop(dev);
    352 		return(0);
    353 	}
    354 #ifdef DEBUG
    355 	dev->sc_dmatimo = 1;
    356 #endif
    357 	/*
    358 	 * clear possible interrupt and stop dma
    359 	 */
    360 	sdp->CNTR &= ~GVP_CNTR_INT_P;
    361 	sdp->SP_DMA = 1;
    362 
    363 	sdp->CNTR = dev->sc_dmacmd;
    364 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    365 	if (dev->gtsc_bankmask)
    366 		sdp->bank =
    367 		    dev->gtsc_bankmask & ((u_int)dev->sc_cur->dc_addr >> 18);
    368 	sdp->ST_DMA = 1;
    369 
    370 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
    371 	if (dev->sc_tcnt > gtsc_maxdma)
    372 		dev->sc_tcnt = gtsc_maxdma;
    373 #ifdef DEBUG
    374 	if (gtsc_debug & DDB_FOLLOW)
    375 		printf("gtsc_dmanext ret: %d\n", dev->sc_tcnt);
    376 #endif
    377 	return(dev->sc_tcnt);
    378 }
    379 
    380 #ifdef DEBUG
    381 void
    382 gtsc_dmatimeout(arg)
    383 	void *arg;
    384 {
    385 	struct sbic_softc *dev;
    386 	int i, s;
    387 
    388 	for (i = 0; i < gtsccd.cd_ndevs; i++) {
    389 		dev = gtsccd.cd_devs[i];
    390 		if (dev == NULL)
    391 			continue;
    392 		s = splbio();
    393 		if (dev->sc_dmatimo) {
    394 			if (dev->sc_dmatimo > 1)
    395 				printf("gtsc_dma%d: timeout #%d\n",
    396 				    dev->sc_dev.dv_unit, dev->sc_dmatimo - 1);
    397 			dev->sc_dmatimo++;
    398 		}
    399 		splx(s);
    400 	}
    401 	timeout(gtsc_dmatimeout, 0, 30 * hz);
    402 }
    403 #endif
    404