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