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