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