Home | History | Annotate | Line # | Download | only in dev
atzsc.c revision 1.38
      1 /*	$NetBSD: atzsc.c,v 1.38 2008/06/13 08:13:37 cegger Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)dma.c
     32  */
     33 
     34 /*
     35  * Copyright (c) 1994 Christian E. Hopps
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)dma.c
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: atzsc.c,v 1.38 2008/06/13 08:13:37 cegger Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/kernel.h>
     74 #include <sys/device.h>
     75 #include <sys/intr.h>
     76 #include <dev/scsipi/scsi_all.h>
     77 #include <dev/scsipi/scsipi_all.h>
     78 #include <dev/scsipi/scsiconf.h>
     79 #include <amiga/amiga/custom.h>
     80 #include <amiga/amiga/cc.h>
     81 #include <amiga/amiga/device.h>
     82 #include <amiga/amiga/isr.h>
     83 #include <amiga/dev/dmavar.h>
     84 #include <amiga/dev/sbicreg.h>
     85 #include <amiga/dev/sbicvar.h>
     86 #include <amiga/dev/atzscreg.h>
     87 #include <amiga/dev/zbusvar.h>
     88 
     89 void atzscattach(struct device *, struct device *, void *);
     90 int atzscmatch(struct device *, struct cfdata *, void *);
     91 
     92 void atzsc_enintr(struct sbic_softc *);
     93 void atzsc_dmastop(struct sbic_softc *);
     94 int atzsc_dmanext(struct sbic_softc *);
     95 int atzsc_dmaintr(void *);
     96 int atzsc_dmago(struct sbic_softc *, char *, int, int);
     97 
     98 #ifdef DEBUG
     99 void atzsc_dump(void);
    100 #endif
    101 
    102 #ifdef DEBUG
    103 int	atzsc_dmadebug = 0;
    104 #endif
    105 
    106 CFATTACH_DECL(atzsc, sizeof(struct sbic_softc),
    107     atzscmatch, atzscattach, NULL, NULL);
    108 
    109 /*
    110  * if we are an A3000 we are here.
    111  */
    112 int
    113 atzscmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    114 {
    115 	struct zbus_args *zap;
    116 
    117 	zap = auxp;
    118 
    119 	/*
    120 	 * Check manufacturer and product id.
    121 	 * I was informed that older boards can be 2 also.
    122 	 */
    123 	if (zap->manid == 514 && (zap->prodid == 3 || zap->prodid == 2))
    124 		return(1);
    125 	else
    126 		return(0);
    127 }
    128 
    129 void
    130 atzscattach(struct device *pdp, struct device *dp, void *auxp)
    131 {
    132 	volatile struct sdmac *rp;
    133 	struct sbic_softc *sc = (struct sbic_softc *)dp;
    134 	struct zbus_args *zap;
    135 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    136 	struct scsipi_channel *chan = &sc->sc_channel;
    137 
    138 	zap = auxp;
    139 
    140 	sc->sc_cregs = rp = zap->va;
    141 	/*
    142 	 * disable ints and reset bank register
    143 	 */
    144 	rp->CNTR = CNTR_PDMD;
    145 	rp->DAWR = DAWR_ATZSC;
    146 	sc->sc_enintr = atzsc_enintr;
    147 	sc->sc_dmago = atzsc_dmago;
    148 	sc->sc_dmanext = atzsc_dmanext;
    149 	sc->sc_dmastop = atzsc_dmastop;
    150 	sc->sc_dmacmd = 0;
    151 
    152 	/*
    153 	 * only 24 bit mem.
    154 	 */
    155 	sc->sc_flags |= SBICF_BADDMA;
    156 	sc->sc_dmamask = ~0x00ffffff;
    157 #if 0
    158 	/*
    159 	 * If the users kva space is not ztwo try and allocate a bounce buffer.
    160 	 * XXX this needs to change if we move to multiple memory segments.
    161 	 */
    162 	if (kvtop(sc) & sc->sc_dmamask) {
    163 		sc->sc_dmabuffer = (char *)alloc_z2mem(MAXPHYS * 8); /* XXX */
    164 		if (isztwomem(sc->sc_dmabuffer))
    165 			printf(" bounce pa 0x%x", kvtop(sc->sc_dmabuffer));
    166 		else if (sc->sc_dmabuffer)
    167 			printf(" bounce pa 0x%x",
    168 			    PREP_DMA_MEM(sc->sc_dmabuffer));
    169 	}
    170 #endif
    171 	sc->sc_sbic.sbic_asr_p = (volatile unsigned char *)rp + 0x91;
    172 	sc->sc_sbic.sbic_value_p = (volatile unsigned char *)rp + 0x93;
    173 
    174 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 77;
    175 
    176 	printf(": dmamask 0x%lx\n", ~sc->sc_dmamask);
    177 
    178 	/*
    179 	 * Fill in the scsipi_adapter.
    180 	 */
    181 	memset(adapt, 0, sizeof(*adapt));
    182 	adapt->adapt_dev = &sc->sc_dev;
    183 	adapt->adapt_nchannels = 1;
    184 	adapt->adapt_openings = 7;
    185 	adapt->adapt_max_periph = 1;
    186 	adapt->adapt_request = sbic_scsipi_request;
    187 	adapt->adapt_minphys = sbic_minphys;
    188 
    189 	/*
    190 	 * Fill in the scsipi_channel.
    191 	 */
    192 	memset(chan, 0, sizeof(*chan));
    193 	chan->chan_adapter = adapt;
    194 	chan->chan_bustype = &scsi_bustype;
    195 	chan->chan_channel = 0;
    196 	chan->chan_ntargets = 8;
    197 	chan->chan_nluns = 8;
    198 	chan->chan_id = 7;
    199 
    200 	sbicinit(sc);
    201 
    202 	sc->sc_isr.isr_intr = atzsc_dmaintr;
    203 	sc->sc_isr.isr_arg = sc;
    204 	sc->sc_isr.isr_ipl = 2;
    205 	add_isr (&sc->sc_isr);
    206 
    207 	/*
    208 	 * attach all scsi units on us
    209 	 */
    210 	config_found(dp, chan, scsiprint);
    211 }
    212 
    213 void
    214 atzsc_enintr(struct sbic_softc *dev)
    215 {
    216 	volatile struct sdmac *sdp;
    217 
    218 	sdp = dev->sc_cregs;
    219 
    220 	dev->sc_flags |= SBICF_INTR;
    221 	sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
    222 }
    223 
    224 int
    225 atzsc_dmago(struct sbic_softc *dev, char *addr, int count, int flags)
    226 {
    227 	volatile struct sdmac *sdp;
    228 
    229 	sdp = dev->sc_cregs;
    230 	/*
    231 	 * Set up the command word based on flags
    232 	 */
    233 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
    234 	if ((flags & DMAGO_READ) == 0)
    235 		dev->sc_dmacmd |= CNTR_DDIR;
    236 #ifdef DEBUG
    237 	if (atzsc_dmadebug & DDB_IO)
    238 		printf("atzsc_dmago: cmd %x\n", dev->sc_dmacmd);
    239 #endif
    240 
    241 	dev->sc_flags |= SBICF_INTR;
    242 	sdp->CNTR = dev->sc_dmacmd;
    243 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    244 	sdp->ST_DMA = 1;
    245 
    246 	return(dev->sc_tcnt);
    247 }
    248 
    249 void
    250 atzsc_dmastop(struct sbic_softc *dev)
    251 {
    252 	volatile struct sdmac *sdp;
    253 	int s;
    254 
    255 	sdp = dev->sc_cregs;
    256 
    257 #ifdef DEBUG
    258 	if (atzsc_dmadebug & DDB_FOLLOW)
    259 		printf("atzsc_dmastop()\n");
    260 #endif
    261 	if (dev->sc_dmacmd) {
    262 		s = splbio();
    263 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    264 			/*
    265 			 * only FLUSH if terminal count not enabled,
    266 			 * and reading from peripheral
    267 			 */
    268 			sdp->FLUSH = 1;
    269 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    270 				;
    271 		}
    272 		/*
    273 		 * clear possible interrupt and stop DMA
    274 		 */
    275 		sdp->CINT = 1;
    276 		sdp->SP_DMA = 1;
    277 		dev->sc_dmacmd = 0;
    278 		splx(s);
    279 	}
    280 }
    281 
    282 int
    283 atzsc_dmaintr(void *arg)
    284 {
    285 	struct sbic_softc *dev = arg;
    286 	volatile struct sdmac *sdp;
    287 	int stat, found;
    288 
    289 	sdp = dev->sc_cregs;
    290 	stat = sdp->ISTR;
    291 
    292 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
    293 		return (0);
    294 
    295 #ifdef DEBUG
    296 	if (atzsc_dmadebug & DDB_FOLLOW)
    297 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
    298 #endif
    299 
    300 	/*
    301 	 * both, SCSI and DMA interrupts arrive here. I chose
    302 	 * arbitrarily that DMA interrupts should have higher
    303 	 * precedence than SCSI interrupts.
    304 	 */
    305 	found = 0;
    306 	if (stat & ISTR_E_INT) {
    307 		found++;
    308 
    309 		sdp->CINT = 1;	/* clear possible interrupt */
    310 
    311 		/*
    312 		 * check for SCSI ints in the same go and
    313 		 * eventually save an interrupt
    314 		 */
    315 	}
    316 
    317 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
    318 		found += sbicintr(dev);
    319 	return(found);
    320 }
    321 
    322 
    323 int
    324 atzsc_dmanext(struct sbic_softc *dev)
    325 {
    326 	volatile struct sdmac *sdp;
    327 
    328 	sdp = dev->sc_cregs;
    329 
    330 	if (dev->sc_cur > dev->sc_last) {
    331 		/* shouldn't happen !! */
    332 		printf("atzsc_dmanext at end !!!\n");
    333 		atzsc_dmastop(dev);
    334 		return(0);
    335 	}
    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 void
    360 atzsc_dump(void)
    361 {
    362 	extern struct cfdriver atzsc_cd;
    363 	struct sbic_softc *sc;
    364 	int i;
    365 
    366 	for (i = 0; i < atzsc_cd.cd_ndevs; ++i) {
    367 		sc = device_lookup_private(&atysc_cd, i);
    368 		if (sc != NULL)
    369 			sbic_dump(sc);
    370 	}
    371 }
    372 #endif
    373