Home | History | Annotate | Line # | Download | only in dev
wstsc.c revision 1.14
      1 /*	$NetBSD: wstsc.c,v 1.14 1996/08/28 18:59:47 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 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 includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     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  *	@(#)supradma.c
     37  */
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <scsi/scsi_all.h>
     43 #include <scsi/scsiconf.h>
     44 #include <amiga/amiga/device.h>
     45 #include <amiga/amiga/isr.h>
     46 #include <amiga/dev/scireg.h>
     47 #include <amiga/dev/scivar.h>
     48 #include <amiga/dev/zbusvar.h>
     49 
     50 void wstscattach __P((struct device *, struct device *, void *));
     51 int wstscmatch __P((struct device *, void *, void *));
     52 
     53 int wstsc_dma_xfer_in __P((struct sci_softc *dev, int len,
     54     register u_char *buf, int phase));
     55 int wstsc_dma_xfer_out __P((struct sci_softc *dev, int len,
     56     register u_char *buf, int phase));
     57 int wstsc_dma_xfer_in2 __P((struct sci_softc *dev, int len,
     58     register u_short *buf, int phase));
     59 int wstsc_dma_xfer_out2 __P((struct sci_softc *dev, int len,
     60     register u_short *buf, int phase));
     61 int wstsc_intr __P((void *));
     62 
     63 struct scsi_adapter wstsc_scsiswitch = {
     64 	sci_scsicmd,
     65 	sci_minphys,
     66 	0,			/* no lun support */
     67 	0,			/* no lun support */
     68 };
     69 
     70 struct scsi_device wstsc_scsidev = {
     71 	NULL,		/* use default error handler */
     72 	NULL,		/* do not have a start functio */
     73 	NULL,		/* have no async handler */
     74 	NULL,		/* Use default done routine */
     75 };
     76 
     77 #ifdef DEBUG
     78 extern int sci_debug;
     79 #define QPRINTF(a) if (sci_debug > 1) printf a
     80 #else
     81 #define QPRINTF(a)
     82 #endif
     83 
     84 extern int sci_data_wait;
     85 
     86 int supradma_pseudo = 0;	/* 0=none, 1=byte, 2=word */
     87 
     88 struct cfattach wstsc_ca = {
     89 	sizeof(struct sci_softc), wstscmatch, wstscattach
     90 };
     91 
     92 struct cfdriver wstsc_cd = {
     93 	NULL, "wstsc", DV_DULL, NULL, 0
     94 };
     95 
     96 /*
     97  * if this a Supra WordSync board
     98  */
     99 int
    100 wstscmatch(pdp, match, auxp)
    101 	struct device *pdp;
    102 	void *match, *auxp;
    103 {
    104 	struct zbus_args *zap;
    105 
    106 	zap = auxp;
    107 
    108 	/*
    109 	 * Check manufacturer and product id.
    110 	 */
    111 	if (zap->manid == 1056 && (
    112 	    zap->prodid == 12 ||	/* WordSync */
    113 	    zap->prodid == 13))		/* ByteSync */
    114 		return(1);
    115 	else
    116 		return(0);
    117 }
    118 
    119 void
    120 wstscattach(pdp, dp, auxp)
    121 	struct device *pdp, *dp;
    122 	void *auxp;
    123 {
    124 	volatile u_char *rp;
    125 	struct sci_softc *sc;
    126 	struct zbus_args *zap;
    127 
    128 	printf("\n");
    129 
    130 	zap = auxp;
    131 
    132 	sc = (struct sci_softc *)dp;
    133 	rp = zap->va;
    134 	/*
    135 	 * set up 5380 register pointers
    136 	 * (Needs check on which Supra board this is - for now,
    137 	 *  just do the WordSync)
    138 	 */
    139 	sc->sci_data = rp + 0;
    140 	sc->sci_odata = rp + 0;
    141 	sc->sci_icmd = rp + 2;
    142 	sc->sci_mode = rp + 4;
    143 	sc->sci_tcmd = rp + 6;
    144 	sc->sci_bus_csr = rp + 8;
    145 	sc->sci_sel_enb = rp + 8;
    146 	sc->sci_csr = rp + 10;
    147 	sc->sci_dma_send = rp + 10;
    148 	sc->sci_idata = rp + 12;
    149 	sc->sci_trecv = rp + 12;
    150 	sc->sci_iack = rp + 14;
    151 	sc->sci_irecv = rp + 14;
    152 
    153 	if (supradma_pseudo == 2) {
    154 		sc->dma_xfer_in = (int(*)(struct sci_softc *, int, u_char *, int))wstsc_dma_xfer_in2;
    155 		sc->dma_xfer_out = (int(*)(struct sci_softc *, int, u_char *, int))wstsc_dma_xfer_out2;
    156 	}
    157 	else if (supradma_pseudo == 1) {
    158 		sc->dma_xfer_in = wstsc_dma_xfer_in;
    159 		sc->dma_xfer_out = wstsc_dma_xfer_out;
    160 	}
    161 
    162 	sc->sc_isr.isr_intr = wstsc_intr;
    163 	sc->sc_isr.isr_arg = sc;
    164 	sc->sc_isr.isr_ipl = 2;
    165 	add_isr(&sc->sc_isr);
    166 
    167 	scireset(sc);
    168 
    169 	sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
    170 	sc->sc_link.adapter_softc = sc;
    171 	sc->sc_link.adapter_target = 7;
    172 	sc->sc_link.adapter = &wstsc_scsiswitch;
    173 	sc->sc_link.device = &wstsc_scsidev;
    174 	sc->sc_link.openings = 1;
    175 	TAILQ_INIT(&sc->sc_xslist);
    176 
    177 	/*
    178 	 * attach all scsi units on us
    179 	 */
    180 	config_found(dp, &sc->sc_link, scsiprint);
    181 }
    182 
    183 int
    184 wstsc_dma_xfer_in (dev, len, buf, phase)
    185 	struct sci_softc *dev;
    186 	int len;
    187 	register u_char *buf;
    188 	int phase;
    189 {
    190 	int wait = sci_data_wait;
    191 	volatile register u_char *sci_dma = dev->sci_idata;
    192 	volatile register u_char *sci_csr = dev->sci_csr;
    193 #ifdef DEBUG
    194 	u_char *obp = (u_char *) buf;
    195 #endif
    196 
    197 	QPRINTF(("supradma_in %d, csr=%02x\n", len, *dev->sci_bus_csr));
    198 
    199 	*dev->sci_tcmd = phase;
    200 	*dev->sci_icmd = 0;
    201 	*dev->sci_mode = SCI_MODE_DMA;
    202 	*dev->sci_irecv = 0;
    203 
    204 	while (len >= 128) {
    205 		wait = sci_data_wait;
    206 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    207 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    208 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    209 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    210 			  || --wait < 0) {
    211 #ifdef DEBUG
    212 				if (sci_debug | 1)
    213 					printf("supradma2_in fail: l%d i%x w%d\n",
    214 					len, *dev->sci_bus_csr, wait);
    215 #endif
    216 				*dev->sci_mode = 0;
    217 				return 0;
    218 			}
    219 		}
    220 
    221 #define R1	(*buf++ = *sci_dma)
    222 		R1; R1; R1; R1; R1; R1; R1; R1;
    223 		R1; R1; R1; R1; R1; R1; R1; R1;
    224 		R1; R1; R1; R1; R1; R1; R1; R1;
    225 		R1; R1; R1; R1; R1; R1; R1; R1;
    226 		R1; R1; R1; R1; R1; R1; R1; R1;
    227 		R1; R1; R1; R1; R1; R1; R1; R1;
    228 		R1; R1; R1; R1; R1; R1; R1; R1;
    229 		R1; R1; R1; R1; R1; R1; R1; R1;
    230 		R1; R1; R1; R1; R1; R1; R1; R1;
    231 		R1; R1; R1; R1; R1; R1; R1; R1;
    232 		R1; R1; R1; R1; R1; R1; R1; R1;
    233 		R1; R1; R1; R1; R1; R1; R1; R1;
    234 		R1; R1; R1; R1; R1; R1; R1; R1;
    235 		R1; R1; R1; R1; R1; R1; R1; R1;
    236 		R1; R1; R1; R1; R1; R1; R1; R1;
    237 		R1; R1; R1; R1; R1; R1; R1; R1;
    238 		len -= 128;
    239 	}
    240 
    241 	while (len > 0) {
    242 		wait = sci_data_wait;
    243 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    244 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    245 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    246 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    247 			  || --wait < 0) {
    248 #ifdef DEBUG
    249 				if (sci_debug | 1)
    250 					printf("supradma1_in fail: l%d i%x w%d\n",
    251 					len, *dev->sci_bus_csr, wait);
    252 #endif
    253 				*dev->sci_mode = 0;
    254 				return 0;
    255 			}
    256 		}
    257 
    258 		*buf++ = *sci_dma;
    259 		len--;
    260 	}
    261 
    262 	QPRINTF(("supradma_in {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    263 	  len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    264 	  obp[6], obp[7], obp[8], obp[9]));
    265 
    266 	*dev->sci_mode = 0;
    267 	return 0;
    268 }
    269 
    270 int
    271 wstsc_dma_xfer_out (dev, len, buf, phase)
    272 	struct sci_softc *dev;
    273 	int len;
    274 	register u_char *buf;
    275 	int phase;
    276 {
    277 	int wait = sci_data_wait;
    278 	volatile register u_char *sci_dma = dev->sci_data;
    279 	volatile register u_char *sci_csr = dev->sci_csr;
    280 
    281 	QPRINTF(("supradma_out %d, csr=%02x\n", len, *dev->sci_bus_csr));
    282 
    283 	QPRINTF(("supradma_out {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    284   	 len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
    285 	 buf[6], buf[7], buf[8], buf[9]));
    286 
    287 	*dev->sci_tcmd = phase;
    288 	*dev->sci_mode = SCI_MODE_DMA;
    289 	*dev->sci_icmd = SCI_ICMD_DATA;
    290 	*dev->sci_dma_send = 0;
    291 	while (len > 0) {
    292 		wait = sci_data_wait;
    293 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    294 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    295 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    296 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    297 			  || --wait < 0) {
    298 #ifdef DEBUG
    299 				if (sci_debug)
    300 					printf("supradma_out fail: l%d i%x w%d\n",
    301 					len, *dev->sci_bus_csr, wait);
    302 #endif
    303 				*dev->sci_mode = 0;
    304 				return 0;
    305 			}
    306 		}
    307 
    308 		*sci_dma = *buf++;
    309 		len--;
    310 	}
    311 
    312 	wait = sci_data_wait;
    313 	while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) ==
    314 	  SCI_CSR_PHASE_MATCH && --wait);
    315 
    316 
    317 	*dev->sci_mode = 0;
    318 	*dev->sci_icmd = 0;
    319 	return 0;
    320 }
    321 
    322 
    323 int
    324 wstsc_dma_xfer_in2 (dev, len, buf, phase)
    325 	struct sci_softc *dev;
    326 	int len;
    327 	register u_short *buf;
    328 	int phase;
    329 {
    330 	volatile register u_short *sci_dma = (u_short *)(dev->sci_idata + 0x10);
    331 	volatile register u_char *sci_csr = dev->sci_csr + 0x10;
    332 #ifdef DEBUG
    333 	u_char *obp = (u_char *) buf;
    334 #endif
    335 #if 0
    336 	int wait = sci_data_wait;
    337 #endif
    338 
    339 	QPRINTF(("supradma_in2 %d, csr=%02x\n", len, *dev->sci_bus_csr));
    340 
    341 	*dev->sci_tcmd = phase;
    342 	*dev->sci_mode = SCI_MODE_DMA;
    343 	*dev->sci_icmd = 0;
    344 	*(dev->sci_irecv + 16) = 0;
    345 	while (len >= 128) {
    346 #if 0
    347 		wait = sci_data_wait;
    348 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    349 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    350 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    351 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    352 			  || --wait < 0) {
    353 #ifdef DEBUG
    354 				if (sci_debug | 1)
    355 					printf("supradma2_in2 fail: l%d i%x w%d\n",
    356 					len, *dev->sci_bus_csr, wait);
    357 #endif
    358 				*dev->sci_mode &= ~SCI_MODE_DMA;
    359 				return 0;
    360 			}
    361 		}
    362 #else
    363 		while (!(*sci_csr & SCI_CSR_DREQ))
    364 			;
    365 #endif
    366 
    367 #define R2	(*buf++ = *sci_dma)
    368 		R2; R2; R2; R2; R2; R2; R2; R2;
    369 		R2; R2; R2; R2; R2; R2; R2; R2;
    370 		R2; R2; R2; R2; R2; R2; R2; R2;
    371 		R2; R2; R2; R2; R2; R2; R2; R2;
    372 		R2; R2; R2; R2; R2; R2; R2; R2;
    373 		R2; R2; R2; R2; R2; R2; R2; R2;
    374 		R2; R2; R2; R2; R2; R2; R2; R2;
    375 		R2; R2; R2; R2; R2; R2; R2; R2;
    376 		len -= 128;
    377 	}
    378 	while (len > 0) {
    379 #if 0
    380 		wait = sci_data_wait;
    381 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    382 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    383 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    384 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    385 			  || --wait < 0) {
    386 #ifdef DEBUG
    387 				if (sci_debug | 1)
    388 					printf("supradma1_in2 fail: l%d i%x w%d\n",
    389 					len, *dev->sci_bus_csr, wait);
    390 #endif
    391 				*dev->sci_mode &= ~SCI_MODE_DMA;
    392 				return 0;
    393 			}
    394 		}
    395 #else
    396 		while (!(*sci_csr * SCI_CSR_DREQ))
    397 			;
    398 #endif
    399 
    400 		*buf++ = *sci_dma;
    401 		len -= 2;
    402 	}
    403 
    404 	QPRINTF(("supradma_in2 {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    405 	  len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    406 	  obp[6], obp[7], obp[8], obp[9]));
    407 
    408 	*dev->sci_irecv = 0;
    409 	*dev->sci_mode = 0;
    410 	return 0;
    411 }
    412 
    413 int
    414 wstsc_dma_xfer_out2 (dev, len, buf, phase)
    415 	struct sci_softc *dev;
    416 	int len;
    417 	register u_short *buf;
    418 	int phase;
    419 {
    420 	volatile register u_short *sci_dma = (ushort *)(dev->sci_data + 0x10);
    421 	volatile register u_char *sci_bus_csr = dev->sci_bus_csr;
    422 #ifdef DEBUG
    423 	u_char *obp = (u_char *) buf;
    424 #endif
    425 #if 0
    426 	int wait = sci_data_wait;
    427 #endif
    428 
    429 	QPRINTF(("supradma_out2 %d, csr=%02x\n", len, *dev->sci_bus_csr));
    430 
    431 	QPRINTF(("supradma_out2 {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    432   	 len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    433 	 obp[6], obp[7], obp[8], obp[9]));
    434 
    435 	*dev->sci_tcmd = phase;
    436 	*dev->sci_mode = SCI_MODE_DMA;
    437 	*dev->sci_icmd = SCI_ICMD_DATA;
    438 	*dev->sci_dma_send = 0;
    439 	while (len > 64) {
    440 #if 0
    441 		wait = sci_data_wait;
    442 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    443 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    444 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    445 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    446 			  || --wait < 0) {
    447 #ifdef DEBUG
    448 				if (sci_debug)
    449 					printf("supradma_out2 fail: l%d i%x w%d\n",
    450 					len, csr, wait);
    451 #endif
    452 				*dev->sci_mode = 0;
    453 				return 0;
    454 			}
    455 		}
    456 #else
    457 		*dev->sci_mode = 0;
    458 		*dev->sci_icmd &= ~SCI_ICMD_ACK;
    459 		while (!(*sci_bus_csr & SCI_BUS_REQ))
    460 			;
    461 		*dev->sci_mode = SCI_MODE_DMA;
    462 		*dev->sci_dma_send = 0;
    463 #endif
    464 
    465 #define W2	(*sci_dma = *buf++)
    466 		W2; W2; W2; W2; W2; W2; W2; W2;
    467 		W2; W2; W2; W2; W2; W2; W2; W2;
    468 		if (*(sci_bus_csr + 0x10) & SCI_BUS_REQ)
    469 			;
    470 		len -= 64;
    471 	}
    472 
    473 	while (len > 0) {
    474 #if 0
    475 		wait = sci_data_wait;
    476 		while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) !=
    477 		  (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
    478 			if (!(*sci_csr & SCI_CSR_PHASE_MATCH)
    479 			  || !(*dev->sci_bus_csr & SCI_BUS_BSY)
    480 			  || --wait < 0) {
    481 #ifdef DEBUG
    482 				if (sci_debug)
    483 					printf("supradma_out2 fail: l%d i%x w%d\n",
    484 					len, csr, wait);
    485 #endif
    486 				*dev->sci_mode = 0;
    487 				return 0;
    488 			}
    489 		}
    490 #else
    491 		*dev->sci_mode = 0;
    492 		*dev->sci_icmd &= ~SCI_ICMD_ACK;
    493 		while (!(*sci_bus_csr & SCI_BUS_REQ))
    494 			;
    495 		*dev->sci_mode = SCI_MODE_DMA;
    496 		*dev->sci_dma_send = 0;
    497 #endif
    498 
    499 		*sci_dma = *buf++;
    500 		if (*(sci_bus_csr + 0x10) & SCI_BUS_REQ)
    501 			;
    502 		len -= 2;
    503 	}
    504 
    505 #if 0
    506 	wait = sci_data_wait;
    507 	while ((*sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) ==
    508 	  SCI_CSR_PHASE_MATCH && --wait);
    509 #endif
    510 
    511 
    512 	*dev->sci_irecv = 0;
    513 	*dev->sci_icmd &= ~SCI_ICMD_ACK;
    514 	*dev->sci_mode = 0;
    515 	*dev->sci_icmd = 0;
    516 	return 0;
    517 }
    518 
    519 int
    520 wstsc_intr(arg)
    521 	void *arg;
    522 {
    523 	struct sci_softc *dev = arg;
    524 	u_char stat;
    525 
    526 	if ((*(dev->sci_csr + 0x10) & SCI_CSR_INT) == 0)
    527 		return (0);
    528 	stat = *(dev->sci_iack + 0x10);
    529 	return (1);
    530 }
    531