xref: /netbsd-src/sys/arch/sun3/dev/si_obio.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: si_obio.c,v 1.31 2005/12/24 23:24:07 perry Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, David Jones, and Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * This file contains only the machine-dependent parts of the
41  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
42  * The machine-independent parts are in ncr5380sbc.c
43  *
44  * Supported hardware includes:
45  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
46  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
47  *
48  * Could be made to support the Sun3/E if someone wanted to.
49  *
50  * Note:  Both supported variants of the Sun SCSI-3 adapter have
51  * some really unusual "features" for this driver to deal with,
52  * generally related to the DMA engine.  The OBIO variant will
53  * ignore any attempt to write the FIFO count register while the
54  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
55  * by setting the FIFO count early in COMMAND or MSG_IN phase.
56  *
57  * The VME variant has a bit to enable or disable the DMA engine,
58  * but that bit also gates the interrupt line from the NCR5380!
59  * Therefore, in order to get any interrupt from the 5380, (i.e.
60  * for reselect) one must clear the DMA engine transfer count and
61  * then enable DMA.  This has the further complication that you
62  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
63  * we have to turn DMA back off before we even look at the 5380.
64  *
65  * What wonderfully whacky hardware this is!
66  *
67  * Credits, history:
68  *
69  * David Jones wrote the initial version of this module, which
70  * included support for the VME adapter only. (no reselection).
71  *
72  * Gordon Ross added support for the OBIO adapter, and re-worked
73  * both the VME and OBIO code to support disconnect/reselect.
74  * (Required figuring out the hardware "features" noted above.)
75  *
76  * The autoconfiguration boilerplate came from Adam Glass.
77  */
78 
79 /*****************************************************************
80  * OBIO functions for DMA
81  ****************************************************************/
82 
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: si_obio.c,v 1.31 2005/12/24 23:24:07 perry Exp $");
85 
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/errno.h>
89 #include <sys/kernel.h>
90 #include <sys/malloc.h>
91 #include <sys/device.h>
92 #include <sys/buf.h>
93 #include <sys/proc.h>
94 #include <sys/user.h>
95 
96 #include <dev/scsipi/scsi_all.h>
97 #include <dev/scsipi/scsipi_all.h>
98 #include <dev/scsipi/scsipi_debug.h>
99 #include <dev/scsipi/scsiconf.h>
100 
101 #include <machine/autoconf.h>
102 #include <machine/dvma.h>
103 
104 /* #define DEBUG XXX */
105 
106 #include <dev/ic/ncr5380reg.h>
107 #include <dev/ic/ncr5380var.h>
108 
109 #include "sireg.h"
110 #include "sivar.h"
111 #include "am9516.h"
112 
113 /*
114  * How many uS. to delay after touching the am9516 UDC.
115  */
116 #define UDC_WAIT_USEC 5
117 
118 void si_obio_dma_setup(struct ncr5380_softc *);
119 void si_obio_dma_start(struct ncr5380_softc *);
120 void si_obio_dma_eop(struct ncr5380_softc *);
121 void si_obio_dma_stop(struct ncr5380_softc *);
122 
123 static void si_obio_reset(struct ncr5380_softc *);
124 
125 static inline void si_obio_udc_write(volatile struct si_regs *, int, int);
126 static inline int si_obio_udc_read(volatile struct si_regs *, int);
127 
128 
129 /*
130  * New-style autoconfig attachment
131  */
132 
133 static int	si_obio_match(struct device *, struct cfdata *, void *);
134 static void	si_obio_attach(struct device *, struct device *, void *);
135 
136 CFATTACH_DECL(si_obio, sizeof(struct si_softc),
137     si_obio_match, si_obio_attach, NULL, NULL);
138 
139 /*
140  * Options for disconnect/reselect, DMA, and interrupts.
141  * By default, allow disconnect/reselect on targets 4-6.
142  * Those are normally tapes that really need it enabled.
143  */
144 int si_obio_options = 0x0f;
145 
146 
147 static int
148 si_obio_match(struct device *parent, struct cfdata *cf, void *aux)
149 {
150 	struct confargs *ca = aux;
151 
152 	/* Make sure something is there... */
153 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1) == -1)
154 		return (0);
155 
156 	/* Default interrupt priority. */
157 	if (ca->ca_intpri == -1)
158 		ca->ca_intpri = 2;
159 
160 	return (1);
161 }
162 
163 static void
164 si_obio_attach(struct device *parent, struct device *self, void *args)
165 {
166 	struct si_softc *sc = (struct si_softc *) self;
167 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
168 	struct cfdata *cf = self->dv_cfdata;
169 	struct confargs *ca = args;
170 
171 	/* Get options from config flags if specified. */
172 	if (cf->cf_flags)
173 		sc->sc_options = cf->cf_flags;
174 	else
175 		sc->sc_options = si_obio_options;
176 
177 	printf(": options=0x%x\n", sc->sc_options);
178 
179 	sc->sc_adapter_type = ca->ca_bustype;
180 	sc->sc_regs = bus_mapin(ca->ca_bustype,
181 		ca->ca_paddr, sizeof(struct si_regs));
182 
183 	/*
184 	 * MD function pointers used by the MI code.
185 	 */
186 	ncr_sc->sc_pio_out = ncr5380_pio_out;
187 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
188 	ncr_sc->sc_dma_alloc = si_dma_alloc;
189 	ncr_sc->sc_dma_free  = si_dma_free;
190 	ncr_sc->sc_dma_setup = si_obio_dma_setup;
191 	ncr_sc->sc_dma_start = si_obio_dma_start;
192 	ncr_sc->sc_dma_poll  = si_dma_poll;
193 	ncr_sc->sc_dma_eop   = si_obio_dma_eop;
194 	ncr_sc->sc_dma_stop  = si_obio_dma_stop;
195 	ncr_sc->sc_intr_on   = NULL;
196 	ncr_sc->sc_intr_off  = NULL;
197 
198 	/* Need DVMA-capable memory for the UDC command block. */
199 	sc->sc_dmacmd = dvma_malloc(sizeof (struct udc_table));
200 
201 	/* Attach interrupt handler. */
202 	isr_add_autovect(si_intr, (void *)sc, ca->ca_intpri);
203 
204 	/* Reset the hardware. */
205 	si_obio_reset(ncr_sc);
206 
207 	/* Do the common attach stuff. */
208 	si_attach(sc);
209 }
210 
211 static void
212 si_obio_reset(struct ncr5380_softc *ncr_sc)
213 {
214 	struct si_softc *sc = (struct si_softc *)ncr_sc;
215 	volatile struct si_regs *si = sc->sc_regs;
216 
217 #ifdef	DEBUG
218 	if (si_debug) {
219 		printf("si_obio_reset\n");
220 	}
221 #endif
222 
223 	/*
224 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
225 	 * 5380 and the DMA.  Make sure it starts out empty.
226 	 *
227 	 * The reset bits in the CSR are active low.
228 	 */
229 	si->si_csr = 0;
230 	delay(10);
231 	si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
232 	delay(10);
233 	si->fifo_count = 0;
234 }
235 
236 static inline void
237 si_obio_udc_write(volatile struct si_regs *si, int regnum, int value)
238 {
239 	si->udc_addr = regnum;
240 	delay(UDC_WAIT_USEC);
241 	si->udc_data = value;
242 	delay(UDC_WAIT_USEC);
243 }
244 
245 static inline int
246 si_obio_udc_read(volatile struct si_regs *si, int regnum)
247 {
248 	int value;
249 
250 	si->udc_addr = regnum;
251 	delay(UDC_WAIT_USEC);
252 	value = si->udc_data;
253 	delay(UDC_WAIT_USEC);
254 
255 	return (value);
256 }
257 
258 
259 /*
260  * This function is called during the COMMAND or MSG_IN phase
261  * that precedes a DATA_IN or DATA_OUT phase, in case we need
262  * to setup the DMA engine before the bus enters a DATA phase.
263  *
264  * The OBIO "si" IGNORES any attempt to set the FIFO count
265  * register after the SCSI bus goes into any DATA phase, so
266  * this function has to setup the evil FIFO logic.
267  */
268 void
269 si_obio_dma_setup(struct ncr5380_softc *ncr_sc)
270 {
271 	struct si_softc *sc = (struct si_softc *)ncr_sc;
272 	struct sci_req *sr = ncr_sc->sc_current;
273 	struct si_dma_handle *dh = sr->sr_dma_hand;
274 	volatile struct si_regs *si = sc->sc_regs;
275 	struct udc_table *cmd;
276 	long data_pa, cmd_pa;
277 	int xlen;
278 
279 	/*
280 	 * Get the DVMA mapping for this segment.
281 	 * XXX - Should separate allocation and mapin.
282 	 */
283 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
284 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
285 	if (data_pa & 1)
286 		panic("si_dma_start: bad pa=0x%lx", data_pa);
287 	xlen = ncr_sc->sc_datalen;
288 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
289 
290 #ifdef	DEBUG
291 	if (si_debug & 2) {
292 		printf("si_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
293 			   dh, data_pa, xlen);
294 	}
295 #endif
296 
297 	/* Reset the UDC. (In case not already reset?) */
298 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
299 
300 	/* Reset the FIFO */
301 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
302 	si->si_csr |= SI_CSR_FIFO_RES;
303 
304 	/* Set direction (send/recv) */
305 	if (dh->dh_flags & SIDH_OUT) {
306 		si->si_csr |= SI_CSR_SEND;
307 	} else {
308 		si->si_csr &= ~SI_CSR_SEND;
309 	}
310 
311 	/* Set the FIFO counter. */
312 	si->fifo_count = xlen;
313 
314 	/* Reset the UDC. */
315 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
316 
317 	/*
318 	 * XXX: Reset the FIFO again!  Comment from Sprite:
319 	 * Go through reset again because of the bug on the 3/50
320 	 * where bytes occasionally linger in the DMA fifo.
321 	 */
322 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
323 	si->si_csr |= SI_CSR_FIFO_RES;
324 
325 #ifdef	DEBUG
326 	/* Make sure the extra FIFO reset did not hit the count. */
327 	if (si->fifo_count != xlen) {
328 		printf("si_dma_setup: fifo_count=0x%x, xlen=0x%x\n",
329 			   si->fifo_count, xlen);
330 		Debugger();
331 	}
332 #endif
333 
334 	/*
335 	 * Set up the DMA controller.  The DMA controller on
336 	 * OBIO needs a command block in DVMA space.
337 	 */
338 	cmd = sc->sc_dmacmd;
339 	cmd->addrh = ((data_pa & 0xFF0000) >> 8) | UDC_ADDR_INFO;
340 	cmd->addrl = data_pa & 0xFFFF;
341 	cmd->count = xlen / 2;	/* bytes -> words */
342 	cmd->cmrh = UDC_CMR_HIGH;
343 	if (dh->dh_flags & SIDH_OUT) {
344 		if (xlen & 1)
345 			cmd->count++;
346 		cmd->cmrl = UDC_CMR_LSEND;
347 		cmd->rsel = UDC_RSEL_SEND;
348 	} else {
349 		cmd->cmrl = UDC_CMR_LRECV;
350 		cmd->rsel = UDC_RSEL_RECV;
351 	}
352 
353 	/* Tell the DMA chip where the control block is. */
354 	cmd_pa = dvma_kvtopa(cmd, BUS_OBIO);
355 	si_obio_udc_write(si, UDC_ADR_CAR_HIGH,
356 					  (cmd_pa & 0xff0000) >> 8);
357 	si_obio_udc_write(si, UDC_ADR_CAR_LOW,
358 					  (cmd_pa & 0xffff));
359 
360 	/* Tell the chip to be a DMA master. */
361 	si_obio_udc_write(si, UDC_ADR_MODE, UDC_MODE);
362 
363 	/* Tell the chip to interrupt on error. */
364 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_CIE);
365 
366 	/* Will do "start chain" command in _dma_start. */
367 }
368 
369 
370 void
371 si_obio_dma_start(struct ncr5380_softc *ncr_sc)
372 {
373 	struct si_softc *sc = (struct si_softc *)ncr_sc;
374 	struct sci_req *sr = ncr_sc->sc_current;
375 	struct si_dma_handle *dh = sr->sr_dma_hand;
376 	volatile struct si_regs *si = sc->sc_regs;
377 	int s;
378 
379 #ifdef	DEBUG
380 	if (si_debug & 2) {
381 		printf("si_dma_start: sr=%p\n", sr);
382 	}
383 #endif
384 
385 	/* This MAY be time critical (not sure). */
386 	s = splhigh();
387 
388 	/* Finally, give the UDC a "start chain" command. */
389 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_STRT_CHN);
390 
391 	/*
392 	 * Acknowledge the phase change.  (After DMA setup!)
393 	 * Put the SBIC into DMA mode, and start the transfer.
394 	 */
395 	if (dh->dh_flags & SIDH_OUT) {
396 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
397 		SCI_CLR_INTR(ncr_sc);
398 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
399 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
400 		*ncr_sc->sci_dma_send = 0;	/* start it */
401 	} else {
402 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
403 		SCI_CLR_INTR(ncr_sc);
404 		*ncr_sc->sci_icmd = 0;
405 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
406 		*ncr_sc->sci_irecv = 0;	/* start it */
407 	}
408 
409 	splx(s);
410 	ncr_sc->sc_state |= NCR_DOINGDMA;
411 
412 #ifdef	DEBUG
413 	if (si_debug & 2) {
414 		printf("si_dma_start: started, flags=0x%x\n",
415 			   ncr_sc->sc_state);
416 	}
417 #endif
418 }
419 
420 
421 void
422 si_obio_dma_eop(struct ncr5380_softc *ncr_sc)
423 {
424 
425 	/* Not needed - DMA was stopped prior to examining sci_csr */
426 }
427 
428 
429 void
430 si_obio_dma_stop(struct ncr5380_softc *ncr_sc)
431 {
432 	struct si_softc *sc = (struct si_softc *)ncr_sc;
433 	struct sci_req *sr = ncr_sc->sc_current;
434 	struct si_dma_handle *dh = sr->sr_dma_hand;
435 	volatile struct si_regs *si = sc->sc_regs;
436 	int resid, ntrans, tmo, udc_cnt;
437 
438 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
439 #ifdef	DEBUG
440 		printf("si_dma_stop: DMA not running\n");
441 #endif
442 		return;
443 	}
444 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
445 
446 	NCR_TRACE("si_dma_stop: top, csr=0x%x\n", si->si_csr);
447 
448 	/* OK, have either phase mis-match or end of DMA. */
449 	/* Set an impossible phase to prevent data movement? */
450 	*ncr_sc->sci_tcmd = PHASE_INVALID;
451 
452 	/* Check for DMA errors. */
453 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
454 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
455 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
456 		ncr_sc->sc_state |= NCR_ABORTING;
457 		si_obio_reset(ncr_sc);
458 		goto out;
459 	}
460 
461 	/* Note that timeout may have set the error flag. */
462 	if (ncr_sc->sc_state & NCR_ABORTING)
463 		goto out;
464 
465 	/*
466 	 * After a read, wait for the FIFO to empty.
467 	 * Note: this only works on the OBIO version.
468 	 */
469 	if ((dh->dh_flags & SIDH_OUT) == 0) {
470 		tmo = 200000;	/* X10 = 2 sec. */
471 		for (;;) {
472 			if (si->si_csr & SI_CSR_FIFO_EMPTY)
473 				break;
474 			if (--tmo <= 0) {
475 				printf("si: DMA FIFO did not empty, reset\n");
476 				ncr_sc->sc_state |= NCR_ABORTING;
477 				/* si_obio_reset(ncr_sc); */
478 				goto out;
479 			}
480 			delay(10);
481 		}
482 	}
483 
484 	/*
485 	 * Now try to figure out how much actually transferred.
486 	 * The fifo_count might not reflect how many bytes were
487 	 * actually transferred.
488 	 */
489 	resid = si->fifo_count & 0xFFFF;
490 	ntrans = sc->sc_reqlen - resid;
491 
492 #ifdef	DEBUG
493 	if (si_debug & 2) {
494 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
495 		       resid, ntrans);
496 	}
497 #endif
498 
499 	/* XXX: Treat (ntrans==0) as a special, non-error case? */
500 	if (ntrans < MIN_DMA_LEN) {
501 		printf("si: fifo count: 0x%x\n", resid);
502 		ncr_sc->sc_state |= NCR_ABORTING;
503 		goto out;
504 	}
505 	if (ntrans > ncr_sc->sc_datalen)
506 		panic("si_dma_stop: excess transfer");
507 
508 	/* Adjust data pointer */
509 	ncr_sc->sc_dataptr += ntrans;
510 	ncr_sc->sc_datalen -= ntrans;
511 
512 	/*
513 	 * After a read, we may need to clean-up
514 	 * "Left-over bytes" (yuck!)
515 	 */
516 	if ((dh->dh_flags & SIDH_OUT) == 0) {
517 		/* If odd transfer count, grab last byte by hand. */
518 		if (ntrans & 1) {
519 			NCR_TRACE("si_dma_stop: leftover 1 at 0x%x\n",
520 				(int) ncr_sc->sc_dataptr - 1);
521 			ncr_sc->sc_dataptr[-1] =
522 				(si->fifo_data & 0xff00) >> 8;
523 			goto out;
524 		}
525 		/* UDC might not have transfered the last word. */
526 		udc_cnt = si_obio_udc_read(si, UDC_ADR_COUNT);
527 		if (((udc_cnt * 2) - resid) == 2) {
528 			NCR_TRACE("si_dma_stop: leftover 2 at 0x%x\n",
529 				(int) ncr_sc->sc_dataptr - 2);
530 			ncr_sc->sc_dataptr[-2] =
531 				(si->fifo_data & 0xff00) >> 8;
532 			ncr_sc->sc_dataptr[-1] =
533 				(si->fifo_data & 0x00ff);
534 		}
535 	}
536 
537 out:
538 	/* Reset the UDC. */
539 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
540 	si->fifo_count = 0;
541 	si->si_csr &= ~SI_CSR_SEND;
542 
543 	/* Reset the FIFO */
544 	si->si_csr &= ~SI_CSR_FIFO_RES;     /* active low */
545 	si->si_csr |= SI_CSR_FIFO_RES;
546 
547 	/* Put SBIC back in PIO mode. */
548 	/* XXX: set tcmd to PHASE_INVALID? */
549 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
550 	*ncr_sc->sci_icmd = 0;
551 }
552 
553