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