Home | History | Annotate | Line # | Download | only in dev
ahsc.c revision 1.22
      1 /*	$NetBSD: ahsc.c,v 1.22 1998/09/04 22:00:27 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/cfdev.h>
     48 #include <amiga/amiga/device.h>
     49 #include <amiga/amiga/isr.h>
     50 #include <amiga/dev/dmavar.h>
     51 #include <amiga/dev/sbicreg.h>
     52 #include <amiga/dev/sbicvar.h>
     53 #include <amiga/dev/ahscreg.h>
     54 #include <amiga/dev/zbusvar.h>
     55 
     56 void ahscattach __P((struct device *, struct device *, void *));
     57 int ahscmatch __P((struct device *, struct cfdata *, void *));
     58 
     59 void ahsc_enintr __P((struct sbic_softc *));
     60 void ahsc_dmastop __P((struct sbic_softc *));
     61 int ahsc_dmanext __P((struct sbic_softc *));
     62 int ahsc_dmaintr __P((void *));
     63 int ahsc_dmago __P((struct sbic_softc *, char *, int, int));
     64 
     65 #ifdef DEBUG
     66 void ahsc_dump __P((void));
     67 #endif
     68 
     69 struct scsipi_adapter ahsc_scsiswitch = {
     70 	sbic_scsicmd,
     71 	sbic_minphys,
     72 	0,			/* no lun support */
     73 	0,			/* no lun support */
     74 };
     75 
     76 struct scsipi_device ahsc_scsidev = {
     77 	NULL,		/* use default error handler */
     78 	NULL,		/* do not have a start functio */
     79 	NULL,		/* have no async handler */
     80 	NULL,		/* Use default done routine */
     81 };
     82 
     83 
     84 #ifdef DEBUG
     85 int	ahsc_dmadebug = 0;
     86 #endif
     87 
     88 struct cfattach ahsc_ca = {
     89 	sizeof(struct sbic_softc), ahscmatch, ahscattach
     90 };
     91 
     92 /*
     93  * if we are an A3000 we are here.
     94  */
     95 int
     96 ahscmatch(pdp, cfp, auxp)
     97 	struct device *pdp;
     98 	struct cfdata *cfp;
     99 	void *auxp;
    100 {
    101 	char *mbusstr;
    102 
    103 	mbusstr = auxp;
    104 	if (is_a3000() && matchname(auxp, "ahsc"))
    105 		return(1);
    106 	return(0);
    107 }
    108 
    109 void
    110 ahscattach(pdp, dp, auxp)
    111 	struct device *pdp, *dp;
    112 	void *auxp;
    113 {
    114 	volatile struct sdmac *rp;
    115 	struct sbic_softc *sc;
    116 	struct cfdev *cdp, *ecdp;
    117 
    118 	ecdp = &cfdev[ncfdev];
    119 
    120 	for (cdp = cfdev; cdp < ecdp; cdp++) {
    121 		if (cdp->rom.manid == 8738 &&
    122 		    cdp->rom.prodid == 35)
    123 				break;
    124 	}
    125 
    126 	sc = (struct sbic_softc *)dp;
    127 	sc->sc_cregs = rp = ztwomap(0xdd0000);
    128 	/*
    129 	 * disable ints and reset bank register
    130 	 */
    131 	rp->CNTR = CNTR_PDMD;
    132 	rp->DAWR = DAWR_AHSC;
    133 	sc->sc_enintr = ahsc_enintr;
    134 	sc->sc_dmago = ahsc_dmago;
    135 	sc->sc_dmanext = ahsc_dmanext;
    136 	sc->sc_dmastop = ahsc_dmastop;
    137 	sc->sc_dmacmd = 0;
    138 
    139 	/*
    140 	 * eveything is a valid dma address
    141 	 */
    142 	sc->sc_dmamask = 0;
    143 
    144 	if (cdp < ecdp) {
    145 		sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x43);
    146 		sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x47);
    147 		printf(": modified for Apollo cpu board\n");
    148 	} else {
    149 		sc->sc_sbic.sbic_asr_p =  ((vu_char *)rp + 0x41);
    150 		sc->sc_sbic.sbic_value_p =  ((vu_char *)rp + 0x43);
    151 		printf("\n");
    152 	}
    153 
    154 	sc->sc_clkfreq = sbic_clock_override ? sbic_clock_override : 143;
    155 
    156 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    157 	sc->sc_link.adapter_softc = sc;
    158 	sc->sc_link.scsipi_scsi.adapter_target = 7;
    159 	sc->sc_link.adapter = &ahsc_scsiswitch;
    160 	sc->sc_link.device = &ahsc_scsidev;
    161 	sc->sc_link.openings = 2;
    162 	sc->sc_link.scsipi_scsi.max_target = 7;
    163 	sc->sc_link.type = BUS_SCSI;
    164 
    165 	sbicinit(sc);
    166 
    167 	sc->sc_isr.isr_intr = ahsc_dmaintr;
    168 	sc->sc_isr.isr_arg = sc;
    169 	sc->sc_isr.isr_ipl = 2;
    170 	add_isr (&sc->sc_isr);
    171 
    172 	/*
    173 	 * attach all scsi units on us
    174 	 */
    175 	config_found(dp, &sc->sc_link, scsiprint);
    176 }
    177 
    178 void
    179 ahsc_enintr(dev)
    180 	struct sbic_softc *dev;
    181 {
    182 	volatile struct sdmac *sdp;
    183 
    184 	sdp = dev->sc_cregs;
    185 
    186 	dev->sc_flags |= SBICF_INTR;
    187 	sdp->CNTR = CNTR_PDMD | CNTR_INTEN;
    188 }
    189 
    190 int
    191 ahsc_dmago(dev, addr, count, flags)
    192 	struct sbic_softc *dev;
    193 	char *addr;
    194 	int count, flags;
    195 {
    196 	volatile struct sdmac *sdp;
    197 
    198 	sdp = dev->sc_cregs;
    199 	/*
    200 	 * Set up the command word based on flags
    201 	 */
    202 	dev->sc_dmacmd = CNTR_PDMD | CNTR_INTEN;
    203 	if ((flags & DMAGO_READ) == 0)
    204 		dev->sc_dmacmd |= CNTR_DDIR;
    205 #ifdef DEBUG
    206 	if (ahsc_dmadebug & DDB_IO)
    207 		printf("ahsc_dmago: cmd %x\n", dev->sc_dmacmd);
    208 #endif
    209 
    210 	dev->sc_flags |= SBICF_INTR;
    211 	sdp->CNTR = dev->sc_dmacmd;
    212 	sdp->ACR = (u_int) dev->sc_cur->dc_addr;
    213 	sdp->ST_DMA = 1;
    214 
    215 	return(dev->sc_tcnt);
    216 }
    217 
    218 void
    219 ahsc_dmastop(dev)
    220 	struct sbic_softc *dev;
    221 {
    222 	volatile struct sdmac *sdp;
    223 	int s;
    224 
    225 	sdp = dev->sc_cregs;
    226 
    227 #ifdef DEBUG
    228 	if (ahsc_dmadebug & DDB_FOLLOW)
    229 		printf("ahsc_dmastop()\n");
    230 #endif
    231 	if (dev->sc_dmacmd) {
    232 		s = splbio();
    233 		if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    234 			/*
    235 			 * only FLUSH if terminal count not enabled,
    236 			 * and reading from peripheral
    237 			 */
    238 			sdp->FLUSH = 1;
    239 			while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    240 				;
    241 		}
    242 		/*
    243 		 * clear possible interrupt and stop dma
    244 		 */
    245 		sdp->CINT = 1;
    246 		sdp->SP_DMA = 1;
    247 		dev->sc_dmacmd = 0;
    248 		splx(s);
    249 	}
    250 }
    251 
    252 int
    253 ahsc_dmaintr(arg)
    254 	void *arg;
    255 {
    256 	struct sbic_softc *dev = arg;
    257 	volatile struct sdmac *sdp;
    258 	int stat, found;
    259 
    260 	sdp = dev->sc_cregs;
    261 	stat = sdp->ISTR;
    262 
    263 	if ((stat & (ISTR_INT_F|ISTR_INT_P)) == 0)
    264 		return (0);
    265 
    266 #ifdef DEBUG
    267 	if (ahsc_dmadebug & DDB_FOLLOW)
    268 		printf("%s: dmaintr 0x%x\n", dev->sc_dev.dv_xname, stat);
    269 #endif
    270 
    271 	/*
    272 	 * both, SCSI and DMA interrupts arrive here. I chose
    273 	 * arbitrarily that DMA interrupts should have higher
    274 	 * precedence than SCSI interrupts.
    275 	 */
    276 	found = 0;
    277 	if (stat & ISTR_E_INT) {
    278 		++found;
    279 
    280 		sdp->CINT = 1;	/* clear possible interrupt */
    281 
    282 		/*
    283 		 * check for SCSI ints in the same go and
    284 		 * eventually save an interrupt
    285 		 */
    286 	}
    287 
    288 	if (dev->sc_flags & SBICF_INTR && stat & ISTR_INTS)
    289 		found += sbicintr(dev);
    290 	return(found);
    291 }
    292 
    293 
    294 int
    295 ahsc_dmanext(dev)
    296 	struct sbic_softc *dev;
    297 {
    298 	volatile struct sdmac *sdp;
    299 
    300 	sdp = dev->sc_cregs;
    301 
    302 	if (dev->sc_cur > dev->sc_last) {
    303 		/* shouldn't happen !! */
    304 		printf("ahsc_dmanext at end !!!\n");
    305 		ahsc_dmastop(dev);
    306 		return(0);
    307 	}
    308 	if ((dev->sc_dmacmd & (CNTR_TCEN | CNTR_DDIR)) == 0) {
    309 		  /*
    310 		   * only FLUSH if terminal count not enabled,
    311 		   * and reading from peripheral
    312 		   */
    313 		sdp->FLUSH = 1;
    314 		while ((sdp->ISTR & ISTR_FE_FLG) == 0)
    315 			;
    316         }
    317 	/*
    318 	 * clear possible interrupt and stop dma
    319 	 */
    320 	sdp->CINT = 1;	/* clear possible interrupt */
    321 	sdp->SP_DMA = 1;	/* stop dma */
    322 	sdp->CNTR = dev->sc_dmacmd;
    323 	sdp->ACR = (u_int)dev->sc_cur->dc_addr;
    324 	sdp->ST_DMA = 1;
    325 
    326 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
    327 	return(dev->sc_tcnt);
    328 }
    329 
    330 #ifdef DEBUG
    331 void
    332 ahsc_dump()
    333 {
    334 	extern struct cfdriver ahsc_cd;
    335 	int i;
    336 
    337 	for (i = 0; i < ahsc_cd.cd_ndevs; ++i)
    338 		if (ahsc_cd.cd_devs[i])
    339 			sbic_dump(ahsc_cd.cd_devs[i]);
    340 }
    341 #endif
    342