Home | History | Annotate | Line # | Download | only in dev
bztzsc.c revision 1.4
      1  1.4  bouyer /*	$NetBSD: bztzsc.c,v 1.4 1997/08/27 11:23:05 bouyer Exp $	*/
      2  1.1      is 
      3  1.1      is /*
      4  1.1      is  * Copyright (c) 1996 Ignatios Souvatzis
      5  1.1      is  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  1.1      is  * All rights reserved.
      7  1.1      is  *
      8  1.1      is  * Redistribution and use in source and binary forms, with or without
      9  1.1      is  * modification, are permitted provided that the following conditions
     10  1.1      is  * are met:
     11  1.1      is  * 1. Redistributions of source code must retain the above copyright
     12  1.1      is  *    notice, this list of conditions and the following disclaimer.
     13  1.1      is  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      is  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      is  *    documentation and/or other materials provided with the distribution.
     16  1.1      is  * 3. All advertising materials mentioning features or use of this software
     17  1.1      is  *    must display the following acknowledgement:
     18  1.1      is  *	This product contains software written by Ignatios Souvatzis for
     19  1.1      is  *	the NetBSD project.
     20  1.1      is  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      is  *    may be used to endorse or promote products derived from this software
     22  1.1      is  *    without specific prior written permission.
     23  1.1      is  *
     24  1.1      is  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      is  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      is  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      is  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      is  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      is  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      is  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      is  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      is  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      is  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      is  * SUCH DAMAGE.
     35  1.1      is  *
     36  1.1      is  */
     37  1.1      is 
     38  1.1      is #include <sys/param.h>
     39  1.1      is #include <sys/systm.h>
     40  1.1      is #include <sys/kernel.h>
     41  1.1      is #include <sys/device.h>
     42  1.4  bouyer #include <dev/scsipi/scsi_all.h>
     43  1.4  bouyer #include <dev/scsipi/scsipi_all.h>
     44  1.4  bouyer #include <dev/scsipi/scsiconf.h>
     45  1.1      is #include <vm/vm.h>
     46  1.1      is #include <vm/vm_kern.h>
     47  1.1      is #include <vm/vm_page.h>
     48  1.1      is #include <machine/pmap.h>
     49  1.1      is #include <amiga/amiga/custom.h>
     50  1.1      is #include <amiga/amiga/cc.h>
     51  1.1      is #include <amiga/amiga/device.h>
     52  1.1      is #include <amiga/amiga/isr.h>
     53  1.1      is #include <amiga/dev/sfasreg.h>
     54  1.1      is #include <amiga/dev/sfasvar.h>
     55  1.1      is #include <amiga/dev/zbusvar.h>
     56  1.1      is #include <amiga/dev/bztzscreg.h>
     57  1.1      is #include <amiga/dev/bztzscvar.h>
     58  1.1      is 
     59  1.1      is void bztzscattach __P((struct device *, struct device *, void *));
     60  1.2   veego int  bztzscmatch  __P((struct device *, struct cfdata *, void *));
     61  1.1      is 
     62  1.4  bouyer struct scsipi_adapter bztzsc_scsiswitch = {
     63  1.1      is 	sfas_scsicmd,
     64  1.1      is 	sfas_minphys,
     65  1.1      is 	0,			/* no lun support */
     66  1.1      is 	0,			/* no lun support */
     67  1.1      is };
     68  1.1      is 
     69  1.4  bouyer struct scsipi_device bztzsc_scsidev = {
     70  1.1      is 	NULL,		/* use default error handler */
     71  1.1      is 	NULL,		/* do not have a start functio */
     72  1.1      is 	NULL,		/* have no async handler */
     73  1.1      is 	NULL,		/* Use default done routine */
     74  1.1      is };
     75  1.1      is 
     76  1.1      is struct cfattach bztzsc_ca = {
     77  1.1      is 	sizeof(struct bztzsc_softc), bztzscmatch, bztzscattach
     78  1.1      is };
     79  1.1      is 
     80  1.1      is struct cfdriver bztzsc_cd = {
     81  1.1      is 	NULL, "bztzsc", DV_DULL, NULL, 0
     82  1.1      is };
     83  1.1      is 
     84  1.1      is int bztzsc_intr		 __P((void *));
     85  1.1      is void bztzsc_set_dma_tc	 __P((struct sfas_softc *sc, unsigned int len));
     86  1.1      is int bztzsc_setup_dma	 __P((struct sfas_softc *sc, vm_offset_t ptr, int len,
     87  1.1      is 			      int mode));
     88  1.1      is int bztzsc_build_dma_chain __P((struct sfas_softc *sc,
     89  1.1      is 			      struct sfas_dma_chain *chain, void *p, int l));
     90  1.1      is int bztzsc_need_bump	 __P((struct sfas_softc *sc, vm_offset_t ptr, int len));
     91  1.1      is void bztzsc_led		 __P((struct sfas_softc *sc, int mode));
     92  1.1      is 
     93  1.1      is /*
     94  1.1      is  * If we are an Phase 5 Devices Blizzard-2060 SCSI option:
     95  1.1      is  */
     96  1.1      is int
     97  1.2   veego bztzscmatch(pdp, cfp, auxp)
     98  1.1      is 	struct device	*pdp;
     99  1.2   veego 	struct cfdata	*cfp;
    100  1.2   veego 	void		*auxp;
    101  1.1      is {
    102  1.1      is 	struct zbus_args *zap;
    103  1.1      is 	volatile u_int8_t *ta;
    104  1.1      is 
    105  1.1      is 	zap = auxp;
    106  1.1      is 
    107  1.1      is 	if (zap->manid != 0x2140)	/* Phase V ? */
    108  1.1      is 		return(0);
    109  1.1      is 
    110  1.1      is 
    111  1.1      is 	if (zap->prodid != 24)		/* is it B2060? */
    112  1.1      is 		return 0;
    113  1.1      is 
    114  1.1      is        	ta = (vu_char *)(((char *)zap->va) + 0x1ff00 + 0x20);
    115  1.1      is 
    116  1.1      is 	if (badbaddr((caddr_t)ta))
    117  1.1      is 		return(0);
    118  1.1      is 
    119  1.1      is 	*ta = 0;
    120  1.1      is 	*ta = 1;
    121  1.1      is 	DELAY(5);
    122  1.1      is 	if (*ta != 1)
    123  1.1      is 		return(0);
    124  1.1      is 
    125  1.1      is 	return(1);
    126  1.1      is }
    127  1.1      is 
    128  1.1      is u_int32_t bztzsc_flags = 0;
    129  1.1      is 
    130  1.1      is void
    131  1.1      is bztzscattach(pdp, dp, auxp)
    132  1.1      is 	struct device	*pdp;
    133  1.1      is 	struct device	*dp;
    134  1.1      is 	void		*auxp;
    135  1.1      is {
    136  1.1      is 	struct bztzsc_softc *sc;
    137  1.1      is 	struct zbus_args  *zap;
    138  1.1      is 	bztzsc_regmap_p	   rp;
    139  1.1      is 	vu_char		  *fas;
    140  1.1      is 
    141  1.1      is 	zap = auxp;
    142  1.1      is 
    143  1.1      is 	fas = &((vu_char *)zap->va)[0x1ff00];
    144  1.1      is 
    145  1.1      is 	sc = (struct bztzsc_softc *)dp;
    146  1.1      is 	rp = &sc->sc_regmap;
    147  1.1      is 
    148  1.1      is 	rp->FAS216.sfas_tc_low	= &fas[0x00];
    149  1.1      is 	rp->FAS216.sfas_tc_mid	= &fas[0x04];
    150  1.1      is 	rp->FAS216.sfas_fifo	= &fas[0x08];
    151  1.1      is 	rp->FAS216.sfas_command	= &fas[0x0C];
    152  1.1      is 	rp->FAS216.sfas_dest_id	= &fas[0x10];
    153  1.1      is 	rp->FAS216.sfas_timeout	= &fas[0x14];
    154  1.1      is 	rp->FAS216.sfas_syncper	= &fas[0x18];
    155  1.1      is 	rp->FAS216.sfas_syncoff	= &fas[0x1C];
    156  1.1      is 	rp->FAS216.sfas_config1	= &fas[0x20];
    157  1.1      is 	rp->FAS216.sfas_clkconv	= &fas[0x24];
    158  1.1      is 	rp->FAS216.sfas_test	= &fas[0x28];
    159  1.1      is 	rp->FAS216.sfas_config2	= &fas[0x2C];
    160  1.1      is 	rp->FAS216.sfas_config3	= &fas[0x30];
    161  1.1      is 	rp->FAS216.sfas_tc_high	= &fas[0x38];
    162  1.1      is 	rp->FAS216.sfas_fifo_bot = &fas[0x3C];
    163  1.1      is 
    164  1.1      is 	rp->hardbits		= &fas[0xe0];
    165  1.1      is 	rp->addrport		= &fas[0xf0];
    166  1.1      is 
    167  1.1      is 	sc->sc_softc.sc_fas	= (sfas_regmap_p)rp;
    168  1.1      is 
    169  1.1      is 	sc->sc_softc.sc_led	= bztzsc_led;
    170  1.1      is 
    171  1.1      is 	sc->sc_softc.sc_setup_dma	= bztzsc_setup_dma;
    172  1.1      is 	sc->sc_softc.sc_build_dma_chain = bztzsc_build_dma_chain;
    173  1.1      is 	sc->sc_softc.sc_need_bump	= bztzsc_need_bump;
    174  1.1      is 
    175  1.1      is 	sc->sc_softc.sc_clock_freq   = 40;   /* Phase5 SCSI all run at 40MHz */
    176  1.1      is 	sc->sc_softc.sc_timeout      = 250;  /* Set default timeout to 250ms */
    177  1.1      is 
    178  1.1      is 	sc->sc_softc.sc_config_flags = bztzsc_flags;	/* for the moment */
    179  1.1      is 
    180  1.1      is 	sc->sc_softc.sc_host_id      = 7;    /* Should check the jumpers */
    181  1.1      is 
    182  1.1      is 	sc->sc_softc.sc_bump_sz = NBPG;	/* XXX should be the VM pagesize */
    183  1.1      is 	sc->sc_softc.sc_bump_pa = 0x0;
    184  1.1      is 
    185  1.1      is 	sfasinitialize((struct sfas_softc *)sc);
    186  1.1      is 
    187  1.1      is 	sc->sc_softc.sc_link.adapter_softc  = sc;
    188  1.4  bouyer 	sc->sc_softc.sc_link.scsipi_scsi.adapter_target = sc->sc_softc.sc_host_id;
    189  1.1      is 	sc->sc_softc.sc_link.adapter	    = &bztzsc_scsiswitch;
    190  1.1      is 	sc->sc_softc.sc_link.device	    = &bztzsc_scsidev;
    191  1.1      is 	sc->sc_softc.sc_link.openings	    = 1;
    192  1.4  bouyer 	sc->sc_softc.sc_link.scsipi_scsi.max_target     = 7;
    193  1.4  bouyer 	sc->sc_softc.sc_link.type = BUS_SCSI;
    194  1.1      is 
    195  1.1      is 	sc->sc_softc.sc_isr.isr_intr = bztzsc_intr;
    196  1.1      is 	sc->sc_softc.sc_isr.isr_arg  = &sc->sc_softc;
    197  1.1      is 	sc->sc_softc.sc_isr.isr_ipl  = 2;
    198  1.1      is 	add_isr(&sc->sc_softc.sc_isr);
    199  1.1      is 
    200  1.1      is /* We don't want interrupt until we're initialized! */
    201  1.1      is 
    202  1.1      is 	printf("\n");
    203  1.1      is 
    204  1.1      is /* attach all scsi units on us */
    205  1.1      is 	config_found(dp, &sc->sc_softc.sc_link, scsiprint);
    206  1.1      is }
    207  1.1      is 
    208  1.1      is int
    209  1.1      is bztzsc_intr(arg)
    210  1.1      is 	void *arg;
    211  1.1      is {
    212  1.1      is 	struct sfas_softc *dev = arg;
    213  1.1      is 	bztzsc_regmap_p	      rp;
    214  1.1      is 	int		      quickints;
    215  1.1      is 
    216  1.1      is 	rp = (bztzsc_regmap_p)dev->sc_fas;
    217  1.1      is 
    218  1.1      is 	if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
    219  1.1      is 		quickints = 16;
    220  1.1      is 		do {
    221  1.1      is 			dev->sc_status = *rp->FAS216.sfas_status;
    222  1.1      is 			dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
    223  1.1      is 
    224  1.1      is 			if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
    225  1.1      is 				dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
    226  1.1      is 				dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
    227  1.1      is 			}
    228  1.1      is 			sfasintr(dev);
    229  1.1      is 
    230  1.1      is 		} while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
    231  1.1      is 			&& --quickints);
    232  1.1      is 
    233  1.1      is 		return(1);
    234  1.1      is 	}
    235  1.1      is 	return(0);
    236  1.1      is }
    237  1.1      is 
    238  1.1      is /* Set DMA transfer counter */
    239  1.1      is void
    240  1.1      is bztzsc_set_dma_tc(sc, len)
    241  1.1      is 	struct sfas_softc *sc;
    242  1.1      is 	unsigned int	  len;
    243  1.1      is {
    244  1.1      is 	*sc->sc_fas->sfas_tc_low  = len; len >>= 8;
    245  1.1      is 	*sc->sc_fas->sfas_tc_mid  = len; len >>= 8;
    246  1.1      is 	*sc->sc_fas->sfas_tc_high = len;
    247  1.1      is }
    248  1.1      is 
    249  1.1      is /* Initialize DMA for transfer */
    250  1.1      is int
    251  1.1      is bztzsc_setup_dma(sc, ptr, len, mode)
    252  1.1      is 	struct sfas_softc *sc;
    253  1.1      is 	vm_offset_t	  ptr;
    254  1.1      is 	int		  len;
    255  1.1      is 	int		  mode;
    256  1.1      is {
    257  1.1      is 	int		retval;
    258  1.1      is 	u_int32_t	d;
    259  1.1      is 	bztzsc_regmap_p	   rp;
    260  1.1      is 
    261  1.1      is 	retval = 0;
    262  1.1      is 
    263  1.1      is 	switch(mode) {
    264  1.1      is 
    265  1.1      is 	case SFAS_DMA_READ:
    266  1.1      is 	case SFAS_DMA_WRITE:
    267  1.1      is 
    268  1.1      is 		rp = (bztzsc_regmap_p)sc->sc_fas;
    269  1.1      is 
    270  1.1      is 		d = (u_int32_t)ptr;
    271  1.1      is 		d >>= 1;
    272  1.1      is 
    273  1.1      is 		if (mode == SFAS_DMA_WRITE)
    274  1.1      is 			d |= (1L << 31);
    275  1.1      is 
    276  1.1      is 		rp->addrport[12] = (u_int8_t)d;
    277  1.1      is 		__asm __volatile("nop");
    278  1.1      is 
    279  1.1      is 		d >>= 8;
    280  1.1      is 		rp->addrport[8] = (u_int8_t)d;
    281  1.1      is 		__asm __volatile("nop");
    282  1.1      is 
    283  1.1      is 		d >>= 8;
    284  1.1      is 		rp->addrport[4] = (u_int8_t)d;
    285  1.1      is 		__asm __volatile("nop");
    286  1.1      is 
    287  1.1      is 		d >>= 8;
    288  1.1      is 		rp->addrport[0] = (u_int8_t)d;
    289  1.1      is 		__asm __volatile("nop");
    290  1.1      is 
    291  1.1      is 		bztzsc_set_dma_tc(sc, len);
    292  1.1      is 		break;
    293  1.1      is 
    294  1.1      is 	case SFAS_DMA_CLEAR:
    295  1.1      is 	default:
    296  1.1      is 		retval = (*sc->sc_fas->sfas_tc_high << 16) |
    297  1.1      is 			 (*sc->sc_fas->sfas_tc_mid  <<  8) |
    298  1.1      is 			  *sc->sc_fas->sfas_tc_low;
    299  1.1      is 
    300  1.1      is 		bztzsc_set_dma_tc(sc, 0);
    301  1.1      is 		break;
    302  1.1      is 	}
    303  1.1      is 
    304  1.1      is 	return(retval);
    305  1.1      is }
    306  1.1      is 
    307  1.1      is /* Check if address and len is ok for DMA transfer */
    308  1.1      is int
    309  1.1      is bztzsc_need_bump(sc, ptr, len)
    310  1.1      is 	struct sfas_softc *sc;
    311  1.1      is 	vm_offset_t	  ptr;
    312  1.1      is 	int		  len;
    313  1.1      is {
    314  1.1      is 	int	p;
    315  1.1      is 
    316  1.1      is 	p = (int)ptr & 0x03;
    317  1.1      is 
    318  1.1      is 	if (p) {
    319  1.1      is 		p = 4-p;
    320  1.1      is 
    321  1.1      is 		if (len < 256)
    322  1.1      is 			p = len;
    323  1.1      is 	}
    324  1.1      is 	return(p);
    325  1.1      is }
    326  1.1      is 
    327  1.1      is /* Interrupt driven routines */
    328  1.1      is /* XXX some of this is voodoo might be remnants intended for the Fastlane. */
    329  1.1      is int
    330  1.1      is bztzsc_build_dma_chain(sc, chain, p, l)
    331  1.1      is 	struct sfas_softc	*sc;
    332  1.1      is 	struct sfas_dma_chain	*chain;
    333  1.1      is 	void			*p;
    334  1.1      is 	int			 l;
    335  1.1      is {
    336  1.1      is 	vm_offset_t  pa, lastpa;
    337  1.1      is 	char	    *ptr;
    338  1.1      is 	int	     len, prelen, max_t, n;
    339  1.1      is 
    340  1.1      is 	if (l == 0)
    341  1.1      is 		return(0);
    342  1.1      is 
    343  1.1      is #define set_link(n, p, l, f)\
    344  1.1      is do { chain[n].ptr = (p); chain[n].len = (l); chain[n++].flg = (f); } while(0)
    345  1.1      is 
    346  1.1      is 	n = 0;
    347  1.1      is 
    348  1.1      is 	if (l < 512)
    349  1.1      is 		set_link(n, (vm_offset_t)p, l, SFAS_CHAIN_BUMP);
    350  1.1      is 	else if ((p >= (void *)0xFF000000)
    351  1.1      is #if defined(M68040) || defined(M68060)
    352  1.1      is 		 && ((mmutype == MMU_68040) && (p >= (void *)0xFFFC0000))
    353  1.1      is #endif
    354  1.1      is 		 ) {
    355  1.1      is 		while(l != 0) {
    356  1.1      is 			len = ((l > sc->sc_bump_sz) ? sc->sc_bump_sz : l);
    357  1.1      is 
    358  1.1      is 			set_link(n, (vm_offset_t)p, len, SFAS_CHAIN_BUMP);
    359  1.1      is 
    360  1.1      is 			p += len;
    361  1.1      is 			l -= len;
    362  1.1      is 		}
    363  1.1      is 	} else {
    364  1.1      is 		ptr = p;
    365  1.1      is 		len = l;
    366  1.1      is 
    367  1.1      is 		pa = kvtop(ptr);
    368  1.1      is 		prelen = ((int)ptr & 0x03);
    369  1.1      is 
    370  1.1      is 		if (prelen) {
    371  1.1      is 			prelen = 4-prelen;
    372  1.1      is 			set_link(n, (vm_offset_t)ptr, prelen, SFAS_CHAIN_BUMP);
    373  1.1      is 			ptr += prelen;
    374  1.1      is 			len -= prelen;
    375  1.1      is 		}
    376  1.1      is 
    377  1.1      is 		lastpa = 0;
    378  1.1      is 		while(len > 3) {
    379  1.1      is 			pa = kvtop(ptr);
    380  1.1      is 			max_t = NBPG - (pa & PGOFSET);
    381  1.1      is 			if (max_t > len)
    382  1.1      is 			  max_t = len;
    383  1.1      is 
    384  1.1      is 			max_t &= ~3;
    385  1.1      is 
    386  1.1      is 			if (lastpa == pa)
    387  1.1      is 				sc->sc_chain[n-1].len += max_t;
    388  1.1      is 			else
    389  1.1      is 				set_link(n, pa, max_t, SFAS_CHAIN_DMA);
    390  1.1      is 
    391  1.1      is 			lastpa = pa+max_t;
    392  1.1      is 
    393  1.1      is 			ptr += max_t;
    394  1.1      is 			len -= max_t;
    395  1.1      is 		}
    396  1.1      is 
    397  1.1      is 		if (len)
    398  1.1      is 			set_link(n, (vm_offset_t)ptr, len, SFAS_CHAIN_BUMP);
    399  1.1      is 	}
    400  1.1      is 
    401  1.1      is 	return(n);
    402  1.1      is }
    403  1.1      is 
    404  1.1      is /* real one for 2060 */
    405  1.1      is void
    406  1.1      is bztzsc_led(sc, mode)
    407  1.1      is 	struct sfas_softc *sc;
    408  1.1      is 	int		  mode;
    409  1.1      is {
    410  1.1      is 	bztzsc_regmap_p		rp;
    411  1.1      is 
    412  1.1      is 	rp = (bztzsc_regmap_p)sc->sc_fas;
    413  1.1      is 
    414  1.1      is 	if (mode)
    415  1.1      is 		*rp->hardbits = 0x00;	/* Led on, Int on */
    416  1.1      is 	else
    417  1.1      is 		*rp->hardbits = 0x02;	/* Led off, Int on */
    418  1.1      is }
    419