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