xref: /netbsd-src/sys/arch/sun3/dev/si_vme.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: si_vme.c,v 1.28 2008/04/04 16:00:58 tsutsui 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  * VME functions for DMA
81  ****************************************************************/
82 
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: si_vme.c,v 1.28 2008/04/04 16:00:58 tsutsui 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 
112 void si_vme_dma_setup(struct ncr5380_softc *);
113 void si_vme_dma_start(struct ncr5380_softc *);
114 void si_vme_dma_eop(struct ncr5380_softc *);
115 void si_vme_dma_stop(struct ncr5380_softc *);
116 
117 void si_vme_intr_on (struct ncr5380_softc *);
118 void si_vme_intr_off(struct ncr5380_softc *);
119 
120 static void si_vme_reset(struct ncr5380_softc *);
121 
122 /*
123  * New-style autoconfig attachment
124  */
125 
126 static int	si_vme_match(device_t, cfdata_t, void *);
127 static void	si_vme_attach(device_t, device_t, void *);
128 
129 CFATTACH_DECL_NEW(si_vme, sizeof(struct si_softc),
130     si_vme_match, si_vme_attach, NULL, NULL);
131 
132 /*
133  * Options for disconnect/reselect, DMA, and interrupts.
134  * By default, allow disconnect/reselect on targets 4-6.
135  * Those are normally tapes that really need it enabled.
136  */
137 int si_vme_options = 0x0f;
138 
139 
140 static int
141 si_vme_match(device_t parent, cfdata_t cf, void *aux)
142 {
143 	struct confargs *ca = aux;
144 	int probe_addr;
145 
146 	/* No default VME address. */
147 	if (ca->ca_paddr == -1)
148 		return 0;
149 
150 	/* Make sure something is there... */
151 	probe_addr = ca->ca_paddr + 1;
152 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
153 		return 0;
154 
155 	/*
156 	 * If this is a VME SCSI board, we have to determine whether
157 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
158 	 * be determined using the fact that the "sc" board occupies
159 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
160 	 */
161 	/* Note: the "si" board should NOT respond here. */
162 	probe_addr = ca->ca_paddr + 0x801;
163 	if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) {
164 		/* Something responded at 2K+1.  Maybe an "sc" board? */
165 #ifdef	DEBUG
166 		printf("%s: May be an `sc' board at pa=0x%lx\n",
167 		    __func__, ca->ca_paddr);
168 #endif
169 		return 0;
170 	}
171 
172 	/* Default interrupt priority. */
173 	if (ca->ca_intpri == -1)
174 		ca->ca_intpri = 2;
175 
176 	return 1;
177 }
178 
179 static void
180 si_vme_attach(device_t parent, device_t self, void *args)
181 {
182 	struct si_softc *sc = device_private(self);
183 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
184 	struct cfdata *cf = device_cfdata(self);
185 	struct confargs *ca = args;
186 
187 	ncr_sc->sc_dev = self;
188 	sc->sc_bst = ca->ca_bustag;
189 	sc->sc_dmat = ca->ca_dmatag;
190 
191 	if (bus_space_map(sc->sc_bst, ca->ca_paddr, sizeof(struct si_regs), 0,
192 	    &sc->sc_bsh) != 0) {
193 		aprint_error(": can't map register\n");
194 		return;
195 	}
196 	sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
197 
198 	if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
199 	    BUS_DMA_NOWAIT, &sc->sc_dmap) != 0) {
200 		aprint_error(": can't create DMA map\n");
201 		return;
202 	}
203 
204 	/* Get options from config flags if specified. */
205 	if (cf->cf_flags)
206 		sc->sc_options = cf->cf_flags;
207 	else
208 		sc->sc_options = si_vme_options;
209 
210 	aprint_normal(": options=0x%x\n", sc->sc_options);
211 
212 	sc->sc_adapter_type = ca->ca_bustype;
213 	sc->sc_adapter_iv_am = VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
214 
215 	/*
216 	 * MD function pointers used by the MI code.
217 	 */
218 	ncr_sc->sc_pio_out = ncr5380_pio_out;
219 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
220 	ncr_sc->sc_dma_alloc = si_dma_alloc;
221 	ncr_sc->sc_dma_free  = si_dma_free;
222 	ncr_sc->sc_dma_setup = si_vme_dma_setup;
223 	ncr_sc->sc_dma_start = si_vme_dma_start;
224 	ncr_sc->sc_dma_poll  = si_dma_poll;
225 	ncr_sc->sc_dma_eop   = si_vme_dma_eop;
226 	ncr_sc->sc_dma_stop  = si_vme_dma_stop;
227 	ncr_sc->sc_intr_on   = si_vme_intr_on;
228 	ncr_sc->sc_intr_off  = si_vme_intr_off;
229 
230 	/* Attach interrupt handler. */
231 	isr_add_vectored(si_intr, (void *)sc, ca->ca_intpri, ca->ca_intvec);
232 
233 	/* Reset the hardware. */
234 	si_vme_reset(ncr_sc);
235 
236 	/* Do the common attach stuff. */
237 	si_attach(sc);
238 }
239 
240 static void
241 si_vme_reset(struct ncr5380_softc *ncr_sc)
242 {
243 	struct si_softc *sc = (struct si_softc *)ncr_sc;
244 	volatile struct si_regs *si = sc->sc_regs;
245 
246 #ifdef	DEBUG
247 	if (si_debug) {
248 		printf("%s\n", __func__);
249 	}
250 #endif
251 
252 	/*
253 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
254 	 * 5380 and the DMA.  Make sure it starts out empty.
255 	 *
256 	 * The reset bits in the CSR are active low.
257 	 */
258 	si->si_csr = 0;
259 	delay(10);
260 	si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
261 	delay(10);
262 	si->fifo_count = 0;
263 
264 	/* Make sure the DMA engine is stopped. */
265 	si->dma_addrh = 0;
266 	si->dma_addrl = 0;
267 	si->dma_counth = 0;
268 	si->dma_countl = 0;
269 	si->si_iv_am = sc->sc_adapter_iv_am;
270 	si->fifo_cnt_hi = 0;
271 }
272 
273 /*
274  * This is called when the bus is going idle,
275  * so we want to enable the SBC interrupts.
276  * That is controlled by the DMA enable!
277  * Who would have guessed!
278  * What a NASTY trick!
279  */
280 void
281 si_vme_intr_on(struct ncr5380_softc *ncr_sc)
282 {
283 	struct si_softc *sc = (struct si_softc *)ncr_sc;
284 	volatile struct si_regs *si = sc->sc_regs;
285 
286 	/* receive mode should be safer */
287 	si->si_csr &= ~SI_CSR_SEND;
288 
289 	/* Clear the count so nothing happens. */
290 	si->dma_counth = 0;
291 	si->dma_countl = 0;
292 
293 	/* Clear the start address too. (paranoid?) */
294 	si->dma_addrh = 0;
295 	si->dma_addrl = 0;
296 
297 	/* Finally, enable the DMA engine. */
298 	si->si_csr |= SI_CSR_DMA_EN;
299 }
300 
301 /*
302  * This is called when the bus is idle and we are
303  * about to start playing with the SBC chip.
304  */
305 void
306 si_vme_intr_off(struct ncr5380_softc *ncr_sc)
307 {
308 	struct si_softc *sc = (struct si_softc *)ncr_sc;
309 	volatile struct si_regs *si = sc->sc_regs;
310 
311 	si->si_csr &= ~SI_CSR_DMA_EN;
312 }
313 
314 /*
315  * This function is called during the COMMAND or MSG_IN phase
316  * that precedes a DATA_IN or DATA_OUT phase, in case we need
317  * to setup the DMA engine before the bus enters a DATA phase.
318  *
319  * XXX: The VME adapter appears to suppress SBC interrupts
320  * when the FIFO is not empty or the FIFO count is non-zero!
321  *
322  * On the VME version, setup the start addres, but clear the
323  * count (to make sure it stays idle) and set that later.
324  */
325 void
326 si_vme_dma_setup(struct ncr5380_softc *ncr_sc)
327 {
328 	struct si_softc *sc = (struct si_softc *)ncr_sc;
329 	struct sci_req *sr = ncr_sc->sc_current;
330 	struct si_dma_handle *dh = sr->sr_dma_hand;
331 	volatile struct si_regs *si = sc->sc_regs;
332 	long data_pa;
333 	int xlen;
334 
335 	/*
336 	 * Get the DVMA mapping for this segment.
337 	 * XXX - Should separate allocation and mapin.
338 	 */
339 	data_pa = dh->dh_dmaaddr;
340 	if (data_pa & 1)
341 		panic("%s: bad pa=0x%lx", __func__, data_pa);
342 	xlen = dh->dh_dmalen;
343 	xlen &= ~1;				/* XXX: necessary? */
344 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
345 
346 #ifdef	DEBUG
347 	if (si_debug & 2) {
348 		printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n",
349 		    __func__, dh, data_pa, xlen);
350 	}
351 #endif
352 
353 	/* Set direction (send/recv) */
354 	if (dh->dh_flags & SIDH_OUT) {
355 		si->si_csr |= SI_CSR_SEND;
356 	} else {
357 		si->si_csr &= ~SI_CSR_SEND;
358 	}
359 
360 	/* Reset the FIFO. */
361 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
362 	si->si_csr |= SI_CSR_FIFO_RES;
363 
364 	if (data_pa & 2) {
365 		si->si_csr |= SI_CSR_BPCON;
366 	} else {
367 		si->si_csr &= ~SI_CSR_BPCON;
368 	}
369 
370 	/* Load the start address. */
371 	si->dma_addrh = (uint16_t)(data_pa >> 16);
372 	si->dma_addrl = (uint16_t)(data_pa & 0xFFFF);
373 
374 	/*
375 	 * Keep the count zero or it may start early!
376 	 */
377 	si->dma_counth = 0;
378 	si->dma_countl = 0;
379 
380 #if 0
381 	/* Clear FIFO counter. (also hits dma_count) */
382 	si->fifo_cnt_hi = 0;
383 	si->fifo_count = 0;
384 #endif
385 }
386 
387 
388 void
389 si_vme_dma_start(struct ncr5380_softc *ncr_sc)
390 {
391 	struct si_softc *sc = (struct si_softc *)ncr_sc;
392 	struct sci_req *sr = ncr_sc->sc_current;
393 	struct si_dma_handle *dh = sr->sr_dma_hand;
394 	volatile struct si_regs *si = sc->sc_regs;
395 	int s, xlen;
396 
397 	xlen = sc->sc_reqlen;
398 
399 	/* This MAY be time critical (not sure). */
400 	s = splhigh();
401 
402 	si->dma_counth = (uint16_t)(xlen >> 16);
403 	si->dma_countl = (uint16_t)(xlen & 0xFFFF);
404 
405 	/* Set it anyway, even though dma_count hits it. */
406 	si->fifo_cnt_hi = (uint16_t)(xlen >> 16);
407 	si->fifo_count  = (uint16_t)(xlen & 0xFFFF);
408 
409 	/*
410 	 * Acknowledge the phase change.  (After DMA setup!)
411 	 * Put the SBIC into DMA mode, and start the transfer.
412 	 */
413 	if (dh->dh_flags & SIDH_OUT) {
414 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
415 		SCI_CLR_INTR(ncr_sc);
416 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
417 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
418 		*ncr_sc->sci_dma_send = 0;	/* start it */
419 	} else {
420 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
421 		SCI_CLR_INTR(ncr_sc);
422 		*ncr_sc->sci_icmd = 0;
423 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
424 		*ncr_sc->sci_irecv = 0;	/* start it */
425 	}
426 
427 	/* Let'er rip! */
428 	si->si_csr |= SI_CSR_DMA_EN;
429 
430 	splx(s);
431 	ncr_sc->sc_state |= NCR_DOINGDMA;
432 
433 #ifdef	DEBUG
434 	if (si_debug & 2) {
435 		printf("%s: started, flags=0x%x\n",
436 		    __func__, ncr_sc->sc_state);
437 	}
438 #endif
439 }
440 
441 
442 void
443 si_vme_dma_eop(struct ncr5380_softc *ncr_sc)
444 {
445 
446 	/* Not needed - DMA was stopped prior to examining sci_csr */
447 }
448 
449 
450 void
451 si_vme_dma_stop(struct ncr5380_softc *ncr_sc)
452 {
453 	struct si_softc *sc = (struct si_softc *)ncr_sc;
454 	struct sci_req *sr = ncr_sc->sc_current;
455 	struct si_dma_handle *dh = sr->sr_dma_hand;
456 	volatile struct si_regs *si = sc->sc_regs;
457 	int resid, ntrans;
458 
459 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
460 #ifdef	DEBUG
461 		printf("%s: DMA not running\n", __func__);
462 #endif
463 		return;
464 	}
465 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
466 
467 	/* First, halt the DMA engine. */
468 	si->si_csr &= ~SI_CSR_DMA_EN;	/* VME only */
469 
470 	/* Set an impossible phase to prevent data movement? */
471 	*ncr_sc->sci_tcmd = PHASE_INVALID;
472 
473 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
474 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
475 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
476 		ncr_sc->sc_state |= NCR_ABORTING;
477 		si_vme_reset(ncr_sc);
478 		goto out;
479 	}
480 
481 	/* Note that timeout may have set the error flag. */
482 	if (ncr_sc->sc_state & NCR_ABORTING)
483 		goto out;
484 
485 	/* XXX: Wait for DMA to actually finish? */
486 
487 	/*
488 	 * Now try to figure out how much actually transferred
489 	 *
490 	 * The fifo_count does not reflect how many bytes were
491 	 * actually transferred for VME.
492 	 *
493 	 * SCSI-3 VME interface is a little funny on writes:
494 	 * if we have a disconnect, the DMA has overshot by
495 	 * one byte and the resid needs to be incremented.
496 	 * Only happens for partial transfers.
497 	 * (Thanks to Matt Jacob)
498 	 */
499 
500 	resid = si->fifo_count & 0xFFFF;
501 	if (dh->dh_flags & SIDH_OUT)
502 		if ((resid > 0) && (resid < sc->sc_reqlen))
503 			resid++;
504 	ntrans = sc->sc_reqlen - resid;
505 
506 #ifdef	DEBUG
507 	if (si_debug & 2) {
508 		printf("%s: resid=0x%x ntrans=0x%x\n",
509 		    __func__, resid, ntrans);
510 	}
511 #endif
512 
513 	if (ntrans < MIN_DMA_LEN) {
514 		printf("si: fifo count: 0x%x\n", resid);
515 		ncr_sc->sc_state |= NCR_ABORTING;
516 		goto out;
517 	}
518 	if (ntrans > ncr_sc->sc_datalen)
519 		panic("%s: excess transfer", __func__);
520 
521 	/* Adjust data pointer */
522 	ncr_sc->sc_dataptr += ntrans;
523 	ncr_sc->sc_datalen -= ntrans;
524 
525 	/*
526 	 * After a read, we may need to clean-up
527 	 * "Left-over bytes" (yuck!)
528 	 */
529 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
530 		((si->si_csr & SI_CSR_LOB) != 0)) {
531 		uint8_t *cp = ncr_sc->sc_dataptr;
532 #ifdef DEBUG
533 		printf("si: Got Left-over bytes!\n");
534 #endif
535 		if (si->si_csr & SI_CSR_BPCON) {
536 			/* have SI_CSR_BPCON */
537 			cp[-1] = (si->si_bprl & 0xff00) >> 8;
538 		} else {
539 			switch (si->si_csr & SI_CSR_LOB) {
540 			case SI_CSR_LOB_THREE:
541 				cp[-3] = (si->si_bprh & 0xff00) >> 8;
542 				cp[-2] = (si->si_bprh & 0x00ff);
543 				cp[-1] = (si->si_bprl & 0xff00) >> 8;
544 				break;
545 			case SI_CSR_LOB_TWO:
546 				cp[-2] = (si->si_bprh & 0xff00) >> 8;
547 				cp[-1] = (si->si_bprh & 0x00ff);
548 				break;
549 			case SI_CSR_LOB_ONE:
550 				cp[-1] = (si->si_bprh & 0xff00) >> 8;
551 				break;
552 			}
553 		}
554 	}
555 
556 out:
557 	si->dma_addrh = 0;
558 	si->dma_addrl = 0;
559 
560 	si->dma_counth = 0;
561 	si->dma_countl = 0;
562 
563 	si->fifo_cnt_hi = 0;
564 	si->fifo_count  = 0;
565 
566 	/* Put SBIC back in PIO mode. */
567 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
568 	*ncr_sc->sci_icmd = 0;
569 }
570