Home | History | Annotate | Line # | Download | only in dev
flsc.c revision 1.9
      1 /*	$NetBSD: flsc.c,v 1.9 1996/08/27 21:54:41 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Daniel Widenfalk
      5  * Copyright (c) 1994 Christian E. Hopps
      6  * Copyright (c) 1982, 1990 The Regents of the University of California.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	@(#)dma.c
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <scsi/scsi_all.h>
     45 #include <scsi/scsiconf.h>
     46 #include <vm/vm.h>
     47 #include <vm/vm_kern.h>
     48 #include <vm/vm_page.h>
     49 #include <machine/pmap.h>
     50 #include <amiga/amiga/custom.h>
     51 #include <amiga/amiga/cc.h>
     52 #include <amiga/amiga/device.h>
     53 #include <amiga/amiga/isr.h>
     54 #include <amiga/dev/sfasreg.h>
     55 #include <amiga/dev/sfasvar.h>
     56 #include <amiga/dev/zbusvar.h>
     57 #include <amiga/dev/flscreg.h>
     58 #include <amiga/dev/flscvar.h>
     59 
     60 int  flscprint  __P((void *auxp, const char *));
     61 void flscattach __P((struct device *, struct device *, void *));
     62 int  flscmatch  __P((struct device *, void *, void *));
     63 
     64 struct scsi_adapter flsc_scsiswitch = {
     65 	sfas_scsicmd,
     66 	sfas_minphys,
     67 	0,			/* no lun support */
     68 	0,			/* no lun support */
     69 };
     70 
     71 struct scsi_device flsc_scsidev = {
     72 	NULL,		/* use default error handler */
     73 	NULL,		/* do not have a start functio */
     74 	NULL,		/* have no async handler */
     75 	NULL,		/* Use default done routine */
     76 };
     77 
     78 struct cfattach flsc_ca = {
     79 	sizeof(struct flsc_softc), flscmatch, flscattach
     80 };
     81 
     82 struct cfdriver flsc_cd = {
     83 	NULL, "flsc", DV_DULL, NULL, 0
     84 };
     85 
     86 int flsc_intr		 __P((void *));
     87 void flsc_set_dma_adr	 __P((struct sfas_softc *sc, vm_offset_t ptr));
     88 void flsc_set_dma_tc	 __P((struct sfas_softc *sc, unsigned int len));
     89 void flsc_set_dma_mode	 __P((struct sfas_softc *sc, int mode));
     90 int flsc_setup_dma	 __P((struct sfas_softc *sc, vm_offset_t ptr, int len,
     91 			      int mode));
     92 int flsc_build_dma_chain __P((struct sfas_softc *sc,
     93 			      struct sfas_dma_chain *chain, void *p, int l));
     94 int flsc_need_bump	 __P((struct sfas_softc *sc, vm_offset_t ptr, int len));
     95 void flsc_led		 __P((struct sfas_softc *sc, int mode));
     96 
     97 /*
     98  * if we are an Advanced Systems & Software FastlaneZ3
     99  */
    100 int
    101 flscmatch(pdp, match, auxp)
    102 	struct device	*pdp;
    103 	void		*match, *auxp;
    104 {
    105 	struct zbus_args *zap;
    106 
    107 	if (!is_a4000() && !is_a3000())
    108 		return(0);
    109 
    110 	zap = auxp;
    111 	if (zap->manid == 0x2140 && zap->prodid == 11
    112 	    && iszthreepa(zap->pa))
    113 		return(1);
    114 
    115 	return(0);
    116 }
    117 
    118 void
    119 flscattach(pdp, dp, auxp)
    120 	struct device	*pdp;
    121 	struct device	*dp;
    122 	void		*auxp;
    123 {
    124 	struct flsc_softc *sc;
    125 	struct zbus_args  *zap;
    126 	flsc_regmap_p	   rp;
    127 	vu_char		  *fas;
    128 
    129 	zap = auxp;
    130 	fas = &((vu_char *)zap->va)[0x1000001];
    131 
    132 	sc = (struct flsc_softc *)dp;
    133 	rp = &sc->sc_regmap;
    134 
    135 	rp->FAS216.sfas_tc_low	= &fas[0x00];
    136 	rp->FAS216.sfas_tc_mid	= &fas[0x04];
    137 	rp->FAS216.sfas_fifo	= &fas[0x08];
    138 	rp->FAS216.sfas_command	= &fas[0x0C];
    139 	rp->FAS216.sfas_dest_id	= &fas[0x10];
    140 	rp->FAS216.sfas_timeout	= &fas[0x14];
    141 	rp->FAS216.sfas_syncper	= &fas[0x18];
    142 	rp->FAS216.sfas_syncoff	= &fas[0x1C];
    143 	rp->FAS216.sfas_config1	= &fas[0x20];
    144 	rp->FAS216.sfas_clkconv	= &fas[0x24];
    145 	rp->FAS216.sfas_test	= &fas[0x28];
    146 	rp->FAS216.sfas_config2	= &fas[0x2C];
    147 	rp->FAS216.sfas_config3	= &fas[0x30];
    148 	rp->FAS216.sfas_tc_high	= &fas[0x38];
    149 	rp->FAS216.sfas_fifo_bot = &fas[0x3C];
    150 	rp->hardbits		= &fas[0x40];
    151 	rp->clear		= &fas[0x80];
    152 	rp->dmabase		= zap->va;
    153 
    154 	sc->sc_softc.sc_fas	= (sfas_regmap_p)rp;
    155 	sc->sc_softc.sc_spec	= &sc->sc_specific;
    156 
    157 	sc->sc_softc.sc_led	= flsc_led;
    158 
    159 	sc->sc_softc.sc_setup_dma	= flsc_setup_dma;
    160 	sc->sc_softc.sc_build_dma_chain = flsc_build_dma_chain;
    161 	sc->sc_softc.sc_need_bump	= flsc_need_bump;
    162 
    163 	sc->sc_softc.sc_clock_freq   = 40;   /* FastlaneZ3 runs at 40MHz */
    164 	sc->sc_softc.sc_timeout      = 250;  /* Set default timeout to 250ms */
    165 	sc->sc_softc.sc_config_flags = 0;    /* No config flags yet */
    166 	sc->sc_softc.sc_host_id      = 7;    /* Should check the jumpers */
    167 
    168 	sc->sc_specific.portbits = 0xA0 | FLSC_PB_EDI | FLSC_PB_ESI;
    169 	sc->sc_specific.hardbits = *rp->hardbits;
    170 
    171 	sc->sc_softc.sc_bump_sz = NBPG;
    172 	sc->sc_softc.sc_bump_pa = 0x0;
    173 
    174 	sfasinitialize((struct sfas_softc *)sc);
    175 
    176 	sc->sc_softc.sc_link.adapter_softc  = sc;
    177 	sc->sc_softc.sc_link.adapter_target = sc->sc_softc.sc_host_id;
    178 	sc->sc_softc.sc_link.adapter	    = &flsc_scsiswitch;
    179 	sc->sc_softc.sc_link.device	    = &flsc_scsidev;
    180 	sc->sc_softc.sc_link.openings	    = 1;
    181 
    182 	sc->sc_softc.sc_isr.isr_intr = flsc_intr;
    183 	sc->sc_softc.sc_isr.isr_arg  = &sc->sc_softc;
    184 	sc->sc_softc.sc_isr.isr_ipl  = 2;
    185 	add_isr(&sc->sc_softc.sc_isr);
    186 
    187 /* We don't want interrupt until we're initialized! */
    188 	*rp->hardbits = sc->sc_specific.portbits;
    189 
    190 	printf("\n");
    191 
    192 /* attach all scsi units on us */
    193 	config_found(dp, &sc->sc_softc.sc_link, flscprint);
    194 }
    195 
    196 /* print diag if pnp is NULL else just extra */
    197 int
    198 flscprint(auxp, pnp)
    199 	void *auxp;
    200 	const char *pnp;
    201 {
    202 	if (pnp == NULL)
    203 		return(UNCONF);
    204 
    205 	return(QUIET);
    206 }
    207 
    208 int
    209 flsc_intr(arg)
    210 	void *arg;
    211 {
    212 	struct sfas_softc *dev = arg;
    213 	flsc_regmap_p	      rp;
    214 	struct flsc_specific *flspec;
    215 	int		      quickints;
    216 	u_char		      hb;
    217 
    218 	flspec = dev->sc_spec;
    219 	rp = (flsc_regmap_p)dev->sc_fas;
    220 	hb = *rp->hardbits;
    221 
    222 	if (hb & FLSC_HB_IACT)
    223 		return(0);
    224 
    225 	flspec->hardbits = hb;
    226 	if ((hb & FLSC_HB_CREQ) &&
    227 	    !(hb & FLSC_HB_MINT) &&
    228 	    (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)) {
    229 		quickints = 16;
    230 		do {
    231 			dev->sc_status = *rp->FAS216.sfas_status;
    232 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
    233 
    234 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    235 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
    236 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
    237 			}
    238 			sfasintr(dev);
    239 
    240 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
    241 			&& --quickints);
    242 	}
    243 
    244 	/* Reset fastlane interrupt bits */
    245 	*rp->hardbits = flspec->portbits & ~FLSC_PB_INT_BITS;
    246 	*rp->hardbits = flspec->portbits;
    247 
    248 	return(1);
    249 }
    250 
    251 /* Load transfer adress into dma register */
    252 void
    253 flsc_set_dma_adr(sc, ptr)
    254 	struct sfas_softc *sc;
    255 	vm_offset_t	  ptr;
    256 {
    257 	flsc_regmap_p	rp;
    258 	unsigned int   *p;
    259 	unsigned int	d;
    260 
    261 	rp = (flsc_regmap_p)sc->sc_fas;
    262 
    263 	d = (unsigned int)ptr;
    264 	p = (unsigned int *)((d & 0xFFFFFF) + (int)rp->dmabase);
    265 
    266 	*rp->clear=0;
    267 	*p = d;
    268 }
    269 
    270 /* Set DMA transfer counter */
    271 void
    272 flsc_set_dma_tc(sc, len)
    273 	struct sfas_softc *sc;
    274 	unsigned int	  len;
    275 {
    276 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
    277 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
    278 	*sc->sc_fas->sfas_tc_high = len;
    279 }
    280 
    281 /* Set DMA mode */
    282 void
    283 flsc_set_dma_mode(sc, mode)
    284 	struct sfas_softc *sc;
    285 	int		  mode;
    286 {
    287 	struct flsc_specific *spec;
    288 
    289 	spec = sc->sc_spec;
    290 
    291 	spec->portbits = (spec->portbits & ~FLSC_PB_DMA_BITS) | mode;
    292 	*((flsc_regmap_p)sc->sc_fas)->hardbits = spec->portbits;
    293 }
    294 
    295 /* Initialize DMA for transfer */
    296 int
    297 flsc_setup_dma(sc, ptr, len, mode)
    298 	struct sfas_softc *sc;
    299 	vm_offset_t	  ptr;
    300 	int		  len;
    301 	int		  mode;
    302 {
    303 	int	retval;
    304 
    305 	retval = 0;
    306 
    307 	switch(mode) {
    308 	case SFAS_DMA_READ:
    309 	case SFAS_DMA_WRITE:
    310 		flsc_set_dma_adr(sc, ptr);
    311 		if (mode == SFAS_DMA_READ)
    312 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_READ);
    313 		else
    314 		  flsc_set_dma_mode(sc,FLSC_PB_ENABLE_DMA | FLSC_PB_DMA_WRITE);
    315 
    316 		flsc_set_dma_tc(sc, len);
    317 		break;
    318 
    319 	case SFAS_DMA_CLEAR:
    320 	default:
    321 		flsc_set_dma_mode(sc, FLSC_PB_DISABLE_DMA);
    322 		flsc_set_dma_adr(sc, 0);
    323 
    324 		retval = (*sc->sc_fas->sfas_tc_high << 16) |
    325 			 (*sc->sc_fas->sfas_tc_mid  <<  8) |
    326 			  *sc->sc_fas->sfas_tc_low;
    327 
    328 		flsc_set_dma_tc(sc, 0);
    329 		break;
    330 	}
    331 
    332 	return(retval);
    333 }
    334 
    335 /* Check if address and len is ok for DMA transfer */
    336 int
    337 flsc_need_bump(sc, ptr, len)
    338 	struct sfas_softc *sc;
    339 	vm_offset_t	  ptr;
    340 	int		  len;
    341 {
    342 	int	p;
    343 
    344 	if (((int)ptr & 0x03) || (len & 0x03)) {
    345 		if (len < 256)
    346 			p = len;
    347 		else
    348 			p = 256;
    349 	} else
    350 		p = 0;
    351 
    352 	return(p);
    353 }
    354 
    355 /* Interrupt driven routines */
    356 int
    357 flsc_build_dma_chain(sc, chain, p, l)
    358 	struct sfas_softc	*sc;
    359 	struct sfas_dma_chain	*chain;
    360 	void			*p;
    361 	int			 l;
    362 {
    363 	vm_offset_t  pa, lastpa;
    364 	char	    *ptr;
    365 	int	     len, prelen, max_t, n;
    366 
    367 	if (l == 0)
    368 		return(0);
    369 
    370 #define set_link(n, p, l, f)\
    371 do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
    372 
    373 	n = 0;
    374 
    375 	if (l < 512)
    376 		set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
    377 	else if ((p >= (void *)0xFF000000)
    378 #if defined(M68040) || defined(M68060)
    379 		 && ((mmutype == MMU_68040) && (p >= (void *)0xFFFC0000))
    380 #endif
    381 		 ) {
    382 		while(l != 0) {
    383 			len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
    384 
    385 			set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
    386 
    387 			p += len;
    388 			l -= len;
    389 		}
    390 	} else {
    391 		ptr = p;
    392 		len = l;
    393 
    394 		pa = kvtop(ptr);
    395 		prelen = ((int)ptr & 0x03);
    396 
    397 		if (prelen) {
    398 			prelen = 4-prelen;
    399 			set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
    400 			ptr += prelen;
    401 			len -= prelen;
    402 		}
    403 
    404 		lastpa = 0;
    405 		while(len > 3) {
    406 			pa = kvtop(ptr);
    407 			max_t = NBPG - (pa & PGOFSET);
    408 			if (max_t > len)
    409 			  max_t = len;
    410 
    411 			max_t &= ~3;
    412 
    413 			if (lastpa == pa)
    414 				sc->sc_chain[n-1].len += max_t;
    415 			else
    416 				set_link(n, pa, max_t, SFAS_CHAIN_DMA);
    417 
    418 			lastpa = pa+max_t;
    419 
    420 			ptr += max_t;
    421 			len -= max_t;
    422 		}
    423 
    424 		if (len)
    425 			set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
    426 	}
    427 
    428 	return(n);
    429 }
    430 
    431 /* Turn on/off led */
    432 void
    433 flsc_led(sc, mode)
    434 	struct sfas_softc *sc;
    435 	int		  mode;
    436 {
    437 	struct flsc_specific   *spec;
    438 	flsc_regmap_p		rp;
    439 
    440 	spec = sc->sc_spec;
    441 	rp = (flsc_regmap_p)sc->sc_fas;
    442 
    443 	if (mode) {
    444 		sc->sc_led_status++;
    445 
    446 		spec->portbits |= FLSC_PB_LED;
    447 		*rp->hardbits = spec->portbits;
    448 	} else {
    449 		if (sc->sc_led_status)
    450 			sc->sc_led_status--;
    451 
    452 		if (!sc->sc_led_status) {
    453 			spec->portbits &= ~FLSC_PB_LED;
    454 			*rp->hardbits = spec->portbits;
    455 		}
    456 	}
    457 }
    458