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