Home | History | Annotate | Line # | Download | only in dev
bztzsc.c revision 1.9
      1 /*	$NetBSD: bztzsc.c,v 1.9 1998/05/24 19:32:34 is Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Michael L. Hitch
      5  * Copyright (c) 1996 Ignatios Souvatzis
      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 contains software written by Ignatios Souvatzis and
     20  *	Michael L. Hitch for the NetBSD project.
     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  */
     38 
     39 /*
     40  * Initial amiga Blizzard 2060 driver by Ingatios Souvatzis.  Conversion to
     41  * 53c9x MI driver by Michael L. Hitch (mhitch (at) montana.edu).
     42  */
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/errno.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/device.h>
     51 #include <sys/buf.h>
     52 #include <sys/proc.h>
     53 #include <sys/user.h>
     54 #include <sys/queue.h>
     55 
     56 #include <dev/scsipi/scsi_all.h>
     57 #include <dev/scsipi/scsipi_all.h>
     58 #include <dev/scsipi/scsiconf.h>
     59 #include <dev/scsipi/scsi_message.h>
     60 
     61 #include <machine/cpu.h>
     62 #include <machine/param.h>
     63 
     64 #include <dev/ic/ncr53c9xreg.h>
     65 #include <dev/ic/ncr53c9xvar.h>
     66 
     67 #include <amiga/amiga/isr.h>
     68 #include <amiga/dev/bztzscvar.h>
     69 #include <amiga/dev/zbusvar.h>
     70 
     71 void	bztzscattach	__P((struct device *, struct device *, void *));
     72 int	bztzscmatch	__P((struct device *, struct cfdata *, void *));
     73 
     74 /* Linkup to the rest of the kernel */
     75 struct cfattach bztzsc_ca = {
     76 	sizeof(struct bztzsc_softc), bztzscmatch, bztzscattach
     77 };
     78 
     79 struct scsipi_adapter bztzsc_switch = {
     80 	ncr53c9x_scsi_cmd,
     81 	minphys,		/* no max at this level; handled by DMA code */
     82 	NULL,
     83 	NULL,
     84 };
     85 
     86 struct scsipi_device bztzsc_dev = {
     87 	NULL,			/* Use default error handler */
     88 	NULL,			/* have a queue, served by this */
     89 	NULL,			/* have no async handler */
     90 	NULL,			/* Use default 'done' routine */
     91 };
     92 
     93 /*
     94  * Functions and the switch for the MI code.
     95  */
     96 u_char	bztzsc_read_reg __P((struct ncr53c9x_softc *, int));
     97 void	bztzsc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
     98 int	bztzsc_dma_isintr __P((struct ncr53c9x_softc *));
     99 void	bztzsc_dma_reset __P((struct ncr53c9x_softc *));
    100 int	bztzsc_dma_intr __P((struct ncr53c9x_softc *));
    101 int	bztzsc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    102 	    size_t *, int, size_t *));
    103 void	bztzsc_dma_go __P((struct ncr53c9x_softc *));
    104 void	bztzsc_dma_stop __P((struct ncr53c9x_softc *));
    105 int	bztzsc_dma_isactive __P((struct ncr53c9x_softc *));
    106 
    107 struct ncr53c9x_glue bztzsc_glue = {
    108 	bztzsc_read_reg,
    109 	bztzsc_write_reg,
    110 	bztzsc_dma_isintr,
    111 	bztzsc_dma_reset,
    112 	bztzsc_dma_intr,
    113 	bztzsc_dma_setup,
    114 	bztzsc_dma_go,
    115 	bztzsc_dma_stop,
    116 	bztzsc_dma_isactive,
    117 	0,
    118 };
    119 
    120 /* Maximum DMA transfer length to reduce impact on high-speed serial input */
    121 u_long bztzsc_max_dma = 1024;
    122 extern int ser_open_speed;
    123 
    124 u_long bztzsc_cnt_pio = 0;	/* number of PIO transfers */
    125 u_long bztzsc_cnt_dma = 0;	/* number of DMA transfers */
    126 u_long bztzsc_cnt_dma2 = 0;	/* number of DMA transfers broken up */
    127 u_long bztzsc_cnt_dma3 = 0;	/* number of pages combined */
    128 
    129 #ifdef DEBUG
    130 struct {
    131 	u_char hardbits;
    132 	u_char status;
    133 	u_char xx;
    134 	u_char yy;
    135 } bztzsc_trace[128];
    136 int bztzsc_trace_ptr = 0;
    137 int bztzsc_trace_enable = 1;
    138 void bztzsc_dump __P((void));
    139 #endif
    140 
    141 /*
    142  * if we are a Phase5 Blizzard 2060 SCSI
    143  */
    144 int
    145 bztzscmatch(parent, cf, aux)
    146 	struct device *parent;
    147 	struct cfdata *cf;
    148 	void *aux;
    149 {
    150 	struct zbus_args *zap;
    151 	volatile u_char *regs;
    152 
    153 	zap = aux;
    154 	if (zap->manid != 0x2140 || zap->prodid != 24)
    155 		return(0);
    156 	regs = &((volatile u_char *)zap->va)[0x1ff00];
    157 	if (badaddr((caddr_t)regs))
    158 		return(0);
    159 	regs[NCR_CFG1 * 4] = 0;
    160 	regs[NCR_CFG1 * 4] = NCRCFG1_PARENB | 7;
    161 	delay(5);
    162 	if (regs[NCR_CFG1 * 4] != (NCRCFG1_PARENB | 7))
    163 		return(0);
    164 	return(1);
    165 }
    166 
    167 /*
    168  * Attach this instance, and then all the sub-devices
    169  */
    170 void
    171 bztzscattach(parent, self, aux)
    172 	struct device *parent, *self;
    173 	void *aux;
    174 {
    175 	struct bztzsc_softc *bsc = (void *)self;
    176 	struct ncr53c9x_softc *sc = &bsc->sc_ncr53c9x;
    177 	struct zbus_args  *zap;
    178 	extern u_long scsi_nosync;
    179 	extern int shift_nosync;
    180 	extern int ncr53c9x_debug;
    181 
    182 	/*
    183 	 * Set up the glue for MI code early; we use some of it here.
    184 	 */
    185 	sc->sc_glue = &bztzsc_glue;
    186 
    187 	/*
    188 	 * Save the regs
    189 	 */
    190 	zap = aux;
    191 	bsc->sc_reg = &((volatile u_char *)zap->va)[0x1ff00];
    192 	bsc->sc_dmabase = &bsc->sc_reg[0xf0];
    193 
    194 	sc->sc_freq = 40;		/* Clocked at 40Mhz */
    195 
    196 	printf(": address %p", bsc->sc_reg);
    197 
    198 	sc->sc_id = 7;
    199 
    200 	/*
    201 	 * It is necessary to try to load the 2nd config register here,
    202 	 * to find out what rev the FAS chip is, else the ncr53c9x_reset
    203 	 * will not set up the defaults correctly.
    204 	 */
    205 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    206 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    207 	sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
    208 	sc->sc_rev = NCR_VARIANT_FAS216;
    209 
    210 	/*
    211 	 * This is the value used to start sync negotiations
    212 	 * Note that the NCR register "SYNCTP" is programmed
    213 	 * in "clocks per byte", and has a minimum value of 4.
    214 	 * The SCSI period used in negotiation is one-fourth
    215 	 * of the time (in nanoseconds) needed to transfer one byte.
    216 	 * Since the chip's clock is given in MHz, we have the following
    217 	 * formula: 4 * period = (1000 / freq) * 4
    218 	 */
    219 	sc->sc_minsync = 1000 / sc->sc_freq;
    220 
    221 	/*
    222 	 * get flags from -I argument and set cf_flags.
    223 	 * NOTE: low 8 bits are to disable disconnect, and the next
    224 	 *       8 bits are to disable sync.
    225 	 */
    226 	sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
    227 	    & 0xffff;
    228 	shift_nosync += 16;
    229 
    230 	/* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
    231 	ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
    232 	shift_nosync += 16;
    233 
    234 #if 1
    235 	if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
    236 		sc->sc_minsync = 0;
    237 #endif
    238 
    239 	/* Really no limit, but since we want to fit into the TCR... */
    240 	sc->sc_maxxfer = 64 * 1024;
    241 
    242 	bsc->sc_reg[0xe0] = BZTZSC_PB_LED;	/* Turn LED off */
    243 
    244 	/*
    245 	 * Configure interrupts.
    246 	 */
    247 	bsc->sc_isr.isr_intr = (int (*)(void *))ncr53c9x_intr;
    248 	bsc->sc_isr.isr_arg  = sc;
    249 	bsc->sc_isr.isr_ipl  = 2;
    250 	add_isr(&bsc->sc_isr);
    251 
    252 	/*
    253 	 * Now try to attach all the sub-devices
    254 	 */
    255 	ncr53c9x_attach(sc, &bztzsc_switch, &bztzsc_dev);
    256 }
    257 
    258 /*
    259  * Glue functions.
    260  */
    261 
    262 u_char
    263 bztzsc_read_reg(sc, reg)
    264 	struct ncr53c9x_softc *sc;
    265 	int reg;
    266 {
    267 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    268 
    269 	return bsc->sc_reg[reg * 4];
    270 }
    271 
    272 void
    273 bztzsc_write_reg(sc, reg, val)
    274 	struct ncr53c9x_softc *sc;
    275 	int reg;
    276 	u_char val;
    277 {
    278 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    279 	u_char v = val;
    280 
    281 	bsc->sc_reg[reg * 4] = v;
    282 #ifdef DEBUG
    283 if (bztzsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL*/ &&
    284   reg == NCR_CMD/* && bsc->sc_active*/) {
    285   bztzsc_trace[(bztzsc_trace_ptr - 1) & 127].yy = v;
    286 /*  printf(" cmd %x", v);*/
    287 }
    288 #endif
    289 }
    290 
    291 int
    292 bztzsc_dma_isintr(sc)
    293 	struct ncr53c9x_softc *sc;
    294 {
    295 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    296 
    297 	if ((bsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) == 0)
    298 		return 0;
    299 
    300 	if (sc->sc_state == NCR_CONNECTED)
    301 		bsc->sc_reg[0xe0] = 0;			/* Turn LED on */
    302 	else
    303 		bsc->sc_reg[0xe0] = BZTZSC_PB_LED;	/* Turn LED off */
    304 
    305 #ifdef DEBUG
    306 if (/*sc->sc_nexus && sc->sc_nexus->xs->flags & SCSI_POLL &&*/ bztzsc_trace_enable) {
    307   bztzsc_trace[bztzsc_trace_ptr].status = bsc->sc_reg[NCR_STAT * 4];
    308   bztzsc_trace[bztzsc_trace_ptr].xx = bsc->sc_reg[NCR_CMD * 4];
    309   bztzsc_trace[bztzsc_trace_ptr].yy = bsc->sc_active;
    310   bztzsc_trace_ptr = (bztzsc_trace_ptr + 1) & 127;
    311 }
    312 #endif
    313 	return 1;
    314 }
    315 
    316 void
    317 bztzsc_dma_reset(sc)
    318 	struct ncr53c9x_softc *sc;
    319 {
    320 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    321 
    322 	bsc->sc_active = 0;
    323 }
    324 
    325 int
    326 bztzsc_dma_intr(sc)
    327 	struct ncr53c9x_softc *sc;
    328 {
    329 	register struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    330 	register int	cnt;
    331 
    332 	NCR_DMA(("bztzsc_dma_intr: cnt %d int %x stat %x fifo %d ",
    333 	    bsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
    334 	    bsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
    335 	if (bsc->sc_active == 0) {
    336 		printf("bztzsc_intr--inactive DMA\n");
    337 		return -1;
    338 	}
    339 
    340 	/* update sc_dmaaddr and sc_pdmalen */
    341 	cnt = bsc->sc_reg[NCR_TCL * 4];
    342 	cnt += bsc->sc_reg[NCR_TCM * 4] << 8;
    343 	cnt += bsc->sc_reg[NCR_TCH * 4] << 16;
    344 	if (!bsc->sc_datain) {
    345 		cnt += bsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
    346 		bsc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
    347 	}
    348 	cnt = bsc->sc_dmasize - cnt;	/* number of bytes transferred */
    349 	NCR_DMA(("DMA xferred %d\n", cnt));
    350 	if (bsc->sc_xfr_align) {
    351 		bcopy(bsc->sc_alignbuf, *bsc->sc_dmaaddr, cnt);
    352 		bsc->sc_xfr_align = 0;
    353 	}
    354 	*bsc->sc_dmaaddr += cnt;
    355 	*bsc->sc_pdmalen -= cnt;
    356 	bsc->sc_active = 0;
    357 	return 0;
    358 }
    359 
    360 int
    361 bztzsc_dma_setup(sc, addr, len, datain, dmasize)
    362 	struct ncr53c9x_softc *sc;
    363 	caddr_t *addr;
    364 	size_t *len;
    365 	int datain;
    366 	size_t *dmasize;
    367 {
    368 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    369 	vm_offset_t pa;
    370 	u_char *ptr;
    371 	size_t xfer;
    372 
    373 	bsc->sc_dmaaddr = addr;
    374 	bsc->sc_pdmalen = len;
    375 	bsc->sc_datain = datain;
    376 	bsc->sc_dmasize = *dmasize;
    377 	/*
    378 	 * DMA can be nasty for high-speed serial input, so limit the
    379 	 * size of this DMA operation if the serial port is running at
    380 	 * a high speed (higher than 19200 for now - should be adjusted
    381 	 * based on cpu type and speed?).
    382 	 * XXX - add serial speed check XXX
    383 	 */
    384 	if (ser_open_speed > 19200 && bztzsc_max_dma != 0 &&
    385 	    bsc->sc_dmasize > bztzsc_max_dma)
    386 		bsc->sc_dmasize = bztzsc_max_dma;
    387 	ptr = *addr;			/* Kernel virtual address */
    388 	pa = kvtop(ptr);		/* Physical address of DMA */
    389 	xfer = min(bsc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
    390 	bsc->sc_xfr_align = 0;
    391 	/*
    392 	 * If output and unaligned, stuff odd byte into FIFO
    393 	 */
    394 	if (datain == 0 && (int)ptr & 1) {
    395 		NCR_DMA(("bztzsc_dma_setup: align byte written to fifo\n"));
    396 		pa++;
    397 		xfer--;			/* XXXX CHECK THIS !!!! XXXX */
    398 		bsc->sc_reg[NCR_FIFO * 4] = *ptr++;
    399 	}
    400 	/*
    401 	 * If unaligned address, read unaligned bytes into alignment buffer
    402 	 */
    403 	else if ((int)ptr & 1) {
    404 		pa = kvtop((caddr_t)&bsc->sc_alignbuf);
    405 		xfer = bsc->sc_dmasize = min(xfer, sizeof (bsc->sc_alignbuf));
    406 		NCR_DMA(("bztzsc_dma_setup: align read by %d bytes\n", xfer));
    407 		bsc->sc_xfr_align = 1;
    408 	}
    409 ++bztzsc_cnt_dma;		/* number of DMA operations */
    410 
    411 	while (xfer < bsc->sc_dmasize) {
    412 		if ((pa + xfer) != kvtop(*addr + xfer))
    413 			break;
    414 		if ((bsc->sc_dmasize - xfer) < NBPG)
    415 			xfer = bsc->sc_dmasize;
    416 		else
    417 			xfer += NBPG;
    418 ++bztzsc_cnt_dma3;
    419 	}
    420 if (xfer != *len)
    421   ++bztzsc_cnt_dma2;
    422 
    423 	bsc->sc_dmasize = xfer;
    424 	*dmasize = bsc->sc_dmasize;
    425 	bsc->sc_pa = pa;
    426 #if defined(M68040) || defined(M68060)
    427 	if (mmutype == MMU_68040) {
    428 		if (bsc->sc_xfr_align) {
    429 			dma_cachectl(bsc->sc_alignbuf,
    430 			    sizeof(bsc->sc_alignbuf));
    431 		}
    432 		else
    433 			dma_cachectl(*bsc->sc_dmaaddr, bsc->sc_dmasize);
    434 	}
    435 #endif
    436 
    437 	pa >>= 1;
    438 	if (!bsc->sc_datain)
    439 		pa |= 0x80000000;
    440 	bsc->sc_dmabase[12] = (u_int8_t)(pa);
    441 	bsc->sc_dmabase[8] = (u_int8_t)(pa >> 8);
    442 	bsc->sc_dmabase[4] = (u_int8_t)(pa >> 16);
    443 	bsc->sc_dmabase[0] = (u_int8_t)(pa >> 24);
    444 	bsc->sc_active = 1;
    445 	return 0;
    446 }
    447 
    448 void
    449 bztzsc_dma_go(sc)
    450 	struct ncr53c9x_softc *sc;
    451 {
    452 }
    453 
    454 void
    455 bztzsc_dma_stop(sc)
    456 	struct ncr53c9x_softc *sc;
    457 {
    458 }
    459 
    460 int
    461 bztzsc_dma_isactive(sc)
    462 	struct ncr53c9x_softc *sc;
    463 {
    464 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    465 
    466 	return bsc->sc_active;
    467 }
    468 
    469 #ifdef DEBUG
    470 void
    471 bztzsc_dump()
    472 {
    473 	int i;
    474 
    475 	i = bztzsc_trace_ptr;
    476 	printf("bztzsc_trace dump: ptr %x\n", bztzsc_trace_ptr);
    477 	do {
    478 		if (bztzsc_trace[i].hardbits == 0) {
    479 			i = (i + 1) & 127;
    480 			continue;
    481 		}
    482 		printf("%02x%02x%02x%02x(", bztzsc_trace[i].hardbits,
    483 		    bztzsc_trace[i].status, bztzsc_trace[i].xx, bztzsc_trace[i].yy);
    484 		if (bztzsc_trace[i].status & NCRSTAT_INT)
    485 			printf("NCRINT/");
    486 		if (bztzsc_trace[i].status & NCRSTAT_TC)
    487 			printf("NCRTC/");
    488 		switch(bztzsc_trace[i].status & NCRSTAT_PHASE) {
    489 		case 0:
    490 			printf("dataout"); break;
    491 		case 1:
    492 			printf("datain"); break;
    493 		case 2:
    494 			printf("cmdout"); break;
    495 		case 3:
    496 			printf("status"); break;
    497 		case 6:
    498 			printf("msgout"); break;
    499 		case 7:
    500 			printf("msgin"); break;
    501 		default:
    502 			printf("phase%d?", bztzsc_trace[i].status & NCRSTAT_PHASE);
    503 		}
    504 		printf(") ");
    505 		i = (i + 1) & 127;
    506 	} while (i != bztzsc_trace_ptr);
    507 	printf("\n");
    508 }
    509 #endif
    510