Home | History | Annotate | Line # | Download | only in dev
atzsc.c revision 1.25
      1 /*	$NetBSD: atzsc.c,v 1.25 1998/08/21 19:13:28 is 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 <dev/scsipi/scsi_all.h>
     43 #include <dev/scsipi/scsipi_all.h>
     44 #include <dev/scsipi/scsiconf.h>
     45 #include <amiga/amiga/custom.h>
     46 #include <amiga/amiga/cc.h>
     47 #include <amiga/amiga/device.h>
     48 #include <amiga/amiga/isr.h>
     49 #include <amiga/dev/dmavar.h>
     50 #include <amiga/dev/sbicreg.h>
     51 #include <amiga/dev/sbicvar.h>
     52 #include <amiga/dev/atzscreg.h>
     53 #include <amiga/dev/zbusvar.h>
     54 
     55 void atzscattach __P((struct device *, struct device *, void *));
     56 int atzscmatch __P((struct device *, struct cfdata *, void *));
     57 
     58 void atzsc_enintr __P((struct sbic_softc *));
     59 void atzsc_dmastop __P((struct sbic_softc *));
     60 int atzsc_dmanext __P((struct sbic_softc *));
     61 int atzsc_dmaintr __P((void *));
     62 int atzsc_dmago __P((struct sbic_softc *, char *, int, int));
     63 
     64 #ifdef DEBUG
     65 void atzsc_dump __P((void));
     66 #endif
     67 
     68 struct scsipi_adapter atzsc_scsiswitch = {
     69 	sbic_scsicmd,
     70 	sbic_minphys,
     71 	0,			/* no lun support */
     72 	0,			/* no lun support */
     73 };
     74 
     75 struct scsipi_device atzsc_scsidev = {
     76 	NULL,		/* use default error handler */
     77 	NULL,		/* do not have a start functio */
     78 	NULL,		/* have no async handler */
     79 	NULL,		/* Use default done routine */
     80 };
     81 
     82 
     83 #ifdef DEBUG
     84 int	atzsc_dmadebug = 0;
     85 #endif
     86 
     87 struct cfattach atzsc_ca = {
     88 	sizeof(struct sbic_softc), atzscmatch, atzscattach
     89 };
     90 
     91 /*
     92  * if we are an A3000 we are here.
     93  */
     94 int
     95 atzscmatch(pdp, cfp, auxp)
     96 	struct device *pdp;
     97 	struct cfdata *cfp;
     98 	void *auxp;
     99 {
    100 	struct zbus_args *zap;
    101 
    102 	zap = auxp;
    103 
    104 	/*
    105 	 * Check manufacturer and product id.
    106 	 * I was informed that older boards can be 2 also.
    107 	 */
    108 	if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2))
    109 		return(1);
    110 	else
    111 		return(0);
    112 }
    113 
    114 void
    115 atzscattach(pdp, dp, auxp)
    116 	struct device *pdp, *dp;
    117 	void *auxp;
    118 {
    119 	volatile struct sdmac *rp;
    120 	struct sbic_softc *sc;
    121 	struct zbus_args *zap;
    122 
    123 	zap = auxp;
    124 
    125 	sc = (struct sbic_softc *)dp;
    126 	sc->sc_cregs = rp = zap->va;
    127 	/*
    128 	 * disable ints and reset bank register
    129 	 */
    130 	rp->CNTR = CNTR_PDMD;
    131 	rp->DAWR = DAWR_ATZSC;
    132 	sc->sc_enintr = atzsc_enintr;
    133 	sc->sc_dmago = atzsc_dmago;
    134 	sc->sc_dmanext = atzsc_dmanext;
    135 	sc->sc_dmastop = atzsc_dmastop;
    136 	sc->sc_dmacmd = 0;
    137 
    138 	/*
    139 	 * only 24 bit mem.
    140 	 */
    141 	sc->sc_flags |= SBICF_BADDMA;
    142 	sc->sc_dmamask = ~0x00ffffff;
    143 #if 0
    144 	/*
    145 	 * If the users kva space is not ztwo try and allocate a bounce buffer.
    146 	 * XXX this needs to change if we move to multiple memory segments.
    147 	 */
    148 	if (kvtop(sc) & sc->sc_dmamask) {
    149 		sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS * 8); /* XXX */
    150 		if (isztwomem(sc->sc_dmabuffer))
    151 			printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer));
    152 		else if (sc->sc_dmabuffer)
    153 			printf(" bounce pa 0x%x",
    154 			    PREP_DMA_MEM(sc->sc_dmabuffer));
    155 	}
    156 #endif
    157 	sc->sc_sbic.sbic_asr_p = (volatile unsigned char *)rp + 0x91;
    158 	sc->sc_sbic.sbic_value_p = (volatile unsigned char *)rp + 0x93;
    159 
    160 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
    161 
    162 	printf(": dmamask 0x%lx\n", ~sc->sc_dmamask);
    163 
    164 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    165 	sc->sc_link.adapter_softc = sc;
    166 	sc->sc_link.scsipi_scsi.adapter_target = 7;
    167 	sc->sc_link.adapter = &atzsc_scsiswitch;
    168 	sc->sc_link.device = &atzsc_scsidev;
    169 	sc->sc_link.openings = 2;
    170 	sc->sc_link.scsipi_scsi.max_target = 7;
    171 	sc->sc_link.type = BUS_SCSI;
    172 
    173 	sbicinit(sc);
    174 
    175 	sc->sc_isr.isr_intr = atzsc_dmaintr;
    176 	sc->sc_isr.isr_arg = sc;
    177 	sc->sc_isr.isr_ipl = 2;
    178 	add_isr (&sc->sc_isr);
    179 
    180 	/*
    181 	 * attach all scsi units on us
    182 	 */
    183 	config_found(dp, &sc->sc_link, scsiprint);
    184 }
    185 
    186 void
    187 atzsc_enintr(dev)
    188 	struct sbic_softc *dev;
    189 {
    190 	volatile struct sdmac *sdp;
    191 
    192 	sdp = dev->sc_cregs;
    193 
    194 	dev->sc_flags |= SBICF_INTR;
    195 	sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
    196 }
    197 
    198 int
    199 atzsc_dmago(dev, addr, count, flags)
    200 	struct sbic_softc *dev;
    201 	char *addr;
    202 	int count, flags;
    203 {
    204 	volatile struct sdmac *sdp;
    205 
    206 	sdp = dev->sc_cregs;
    207 	/*
    208 	 * Set up the command word based on flags
    209 	 */
    210 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
    211 	if ((flags & DMAGO_READ) == 0)
    212 		dev->sc_dmacmd |= CNTR_DDIR;
    213 #ifdef DEBUG
    214 	if (atzsc_dmadebug & DDB_IO)
    215 		printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd);
    216 #endif
    217 
    218 	dev->sc_flags |= SBICF_INTR;
    219 	sdp->CNTR = dev->sc_dmacmd;
    220 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    221 	sdp->ST_DMA = 1;
    222 
    223 	return(dev->sc_tcnt);
    224 }
    225 
    226 void
    227 atzsc_dmastop(dev)
    228 	struct sbic_softc *dev;
    229 {
    230 	volatile struct sdmac *sdp;
    231 	int s;
    232 
    233 	sdp = dev->sc_cregs;
    234 
    235 #ifdef DEBUG
    236 	if (atzsc_dmadebug & DDB_FOLLOW)
    237 		printf("atzsc_dmastop()\n");
    238 #endif
    239 	if (dev->sc_dmacmd) {
    240 		s = splbio();
    241 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    242 			/*
    243 			 * only FLUSH if terminal count not enabled,
    244 			 * and reading from peripheral
    245 			 */
    246 			sdp->FLUSH = 1;
    247 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    248 				;
    249 		}
    250 		/*
    251 		 * clear possible interrupt and stop dma
    252 		 */
    253 		sdp->CINT = 1;
    254 		sdp->SP_DMA = 1;
    255 		dev->sc_dmacmd = 0;
    256 		splx(s);
    257 	}
    258 }
    259 
    260 int
    261 atzsc_dmaintr(arg)
    262 	void *arg;
    263 {
    264 	struct sbic_softc *dev = arg;
    265 	volatile struct sdmac *sdp;
    266 	int stat, found;
    267 
    268 	sdp = dev->sc_cregs;
    269 	stat = sdp->ISTR;
    270 
    271 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
    272 		return (0);
    273 
    274 #ifdef DEBUG
    275 	if (atzsc_dmadebug & DDB_FOLLOW)
    276 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
    277 #endif
    278 
    279 	/*
    280 	 * both, SCSI and DMA interrupts arrive here. I chose
    281 	 * arbitrarily that DMA interrupts should have higher
    282 	 * precedence than SCSI interrupts.
    283 	 */
    284 	found = 0;
    285 	if (stat & ISTR_E_INT) {
    286 		found++;
    287 
    288 		sdp->CINT = 1;	/* clear possible interrupt */
    289 
    290 		/*
    291 		 * check for SCSI ints in the same go and
    292 		 * eventually save an interrupt
    293 		 */
    294 	}
    295 
    296 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
    297 		found += sbicintr(dev);
    298 	return(found);
    299 }
    300 
    301 
    302 int
    303 atzsc_dmanext(dev)
    304 	struct sbic_softc *dev;
    305 {
    306 	volatile struct sdmac *sdp;
    307 
    308 	sdp = dev->sc_cregs;
    309 
    310 	if (dev->sc_cur > dev->sc_last) {
    311 		/* shouldn't happen !! */
    312 		printf("atzsc_dmanext at end !!!\n");
    313 		atzsc_dmastop(dev);
    314 		return(0);
    315 	}
    316 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    317 		  /*
    318 		   * only FLUSH if terminal count not enabled,
    319 		   * and reading from peripheral
    320 		   */
    321 		sdp->FLUSH = 1;
    322 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    323 			;
    324         }
    325 	/*
    326 	 * clear possible interrupt and stop dma
    327 	 */
    328 	sdp->CINT = 1;	/* clear possible interrupt */
    329 	sdp->SP_DMA = 1;	/* stop dma */
    330 	sdp->CNTR = dev->sc_dmacmd;
    331 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
    332 	sdp->ST_DMA = 1;
    333 
    334 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
    335 	return(dev->sc_tcnt);
    336 }
    337 
    338 #ifdef DEBUG
    339 void
    340 atzsc_dump()
    341 {
    342 	extern struct cfdriver atzsc_cd;
    343 	int i;
    344 
    345 	for (i = 0; i < atzsc_cd.cd_ndevs; ++i)
    346 		if (atzsc_cd.cd_devs[i])
    347 			sbic_dump(atzsc_cd.cd_devs[i]);
    348 }
    349 #endif
    350