si_vme.c revision 1.33 1 /* $NetBSD: si_vme.c,v 1.33 2024/12/20 23:52:00 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * This file contains only the machine-dependent parts of the
34 * Sun3 SCSI driver. (Autoconfig stuff and DMA functions.)
35 * The machine-independent parts are in ncr5380sbc.c
36 *
37 * Supported hardware includes:
38 * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
39 * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
40 *
41 * Could be made to support the Sun3/E if someone wanted to.
42 *
43 * Note: Both supported variants of the Sun SCSI-3 adapter have
44 * some really unusual "features" for this driver to deal with,
45 * generally related to the DMA engine. The OBIO variant will
46 * ignore any attempt to write the FIFO count register while the
47 * SCSI bus is in DATA_IN or DATA_OUT phase. This is dealt with
48 * by setting the FIFO count early in COMMAND or MSG_IN phase.
49 *
50 * The VME variant has a bit to enable or disable the DMA engine,
51 * but that bit also gates the interrupt line from the NCR5380!
52 * Therefore, in order to get any interrupt from the 5380, (i.e.
53 * for reselect) one must clear the DMA engine transfer count and
54 * then enable DMA. This has the further complication that you
55 * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
56 * we have to turn DMA back off before we even look at the 5380.
57 *
58 * What wonderfully whacky hardware this is!
59 *
60 * Credits, history:
61 *
62 * David Jones wrote the initial version of this module, which
63 * included support for the VME adapter only. (no reselection).
64 *
65 * Gordon Ross added support for the OBIO adapter, and re-worked
66 * both the VME and OBIO code to support disconnect/reselect.
67 * (Required figuring out the hardware "features" noted above.)
68 *
69 * The autoconfiguration boilerplate came from Adam Glass.
70 */
71
72 /*****************************************************************
73 * VME functions for DMA
74 ****************************************************************/
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: si_vme.c,v 1.33 2024/12/20 23:52:00 tsutsui Exp $");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/errno.h>
82 #include <sys/kernel.h>
83 #include <sys/device.h>
84 #include <sys/buf.h>
85 #include <sys/proc.h>
86
87 #include <dev/scsipi/scsi_all.h>
88 #include <dev/scsipi/scsipi_all.h>
89 #include <dev/scsipi/scsipi_debug.h>
90 #include <dev/scsipi/scsiconf.h>
91
92 #include <machine/autoconf.h>
93 #include <machine/dvma.h>
94
95 /* #define DEBUG XXX */
96
97 #include <dev/ic/ncr5380reg.h>
98 #include <dev/ic/ncr5380var.h>
99
100 #include "sireg.h"
101 #include "sivar.h"
102
103 void si_vme_dma_setup(struct ncr5380_softc *);
104 void si_vme_dma_start(struct ncr5380_softc *);
105 void si_vme_dma_eop(struct ncr5380_softc *);
106 void si_vme_dma_stop(struct ncr5380_softc *);
107
108 void si_vme_intr_on (struct ncr5380_softc *);
109 void si_vme_intr_off(struct ncr5380_softc *);
110
111 static void si_vme_reset(struct ncr5380_softc *);
112
113 /*
114 * New-style autoconfig attachment
115 */
116
117 static int si_vme_match(device_t, cfdata_t, void *);
118 static void si_vme_attach(device_t, device_t, void *);
119
120 CFATTACH_DECL_NEW(si_vme, sizeof(struct si_softc),
121 si_vme_match, si_vme_attach, NULL, NULL);
122
123 /*
124 * Options for disconnect/reselect, DMA, and interrupts.
125 * By default, allow disconnect/reselect on targets 4-6.
126 * Those are normally tapes that really need it enabled.
127 */
128 int si_vme_options = 0x0f;
129
130
131 static int
132 si_vme_match(device_t parent, cfdata_t cf, void *aux)
133 {
134 struct confargs *ca = aux;
135 int probe_addr;
136
137 /* No default VME address. */
138 if (ca->ca_paddr == -1)
139 return 0;
140
141 /* Make sure something is there... */
142 probe_addr = ca->ca_paddr + 1;
143 if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
144 return 0;
145
146 /*
147 * If this is a VME SCSI board, we have to determine whether
148 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board. This can
149 * be determined using the fact that the "sc" board occupies
150 * 4K bytes in VME space but the "si" board occupies 2K bytes.
151 */
152 /* Note: the "si" board should NOT respond here. */
153 probe_addr = ca->ca_paddr + 0x801;
154 if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) {
155 /* Something responded at 2K+1. Maybe an "sc" board? */
156 #ifdef DEBUG
157 printf("%s: May be an `sc' board at pa=0x%lx\n",
158 __func__, ca->ca_paddr);
159 #endif
160 return 0;
161 }
162
163 /* Default interrupt priority. */
164 if (ca->ca_intpri == -1)
165 ca->ca_intpri = 2;
166
167 return 1;
168 }
169
170 static void
171 si_vme_attach(device_t parent, device_t self, void *args)
172 {
173 struct si_softc *sc = device_private(self);
174 struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
175 struct cfdata *cf = device_cfdata(self);
176 struct confargs *ca = args;
177
178 ncr_sc->sc_dev = self;
179 sc->sc_bst = ca->ca_bustag;
180 sc->sc_dmat = ca->ca_dmatag;
181
182 if (bus_space_map(sc->sc_bst, ca->ca_paddr, sizeof(struct si_regs), 0,
183 &sc->sc_bsh) != 0) {
184 aprint_error(": can't map register\n");
185 return;
186 }
187 sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
188
189 if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
190 BUS_DMA_NOWAIT, &sc->sc_dmap) != 0) {
191 aprint_error(": can't create DMA map\n");
192 return;
193 }
194
195 /* Get options from config flags if specified. */
196 if (cf->cf_flags)
197 sc->sc_options = cf->cf_flags;
198 else
199 sc->sc_options = si_vme_options;
200
201 aprint_normal(": options=0x%x\n", sc->sc_options);
202
203 sc->sc_adapter_type = ca->ca_bustype;
204 sc->sc_adapter_iv_am = VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
205
206 /*
207 * MD function pointers used by the MI code.
208 */
209 ncr_sc->sc_pio_out = ncr5380_pio_out;
210 ncr_sc->sc_pio_in = ncr5380_pio_in;
211 ncr_sc->sc_dma_alloc = si_dma_alloc;
212 ncr_sc->sc_dma_free = si_dma_free;
213 ncr_sc->sc_dma_setup = si_vme_dma_setup;
214 ncr_sc->sc_dma_start = si_vme_dma_start;
215 ncr_sc->sc_dma_poll = si_dma_poll;
216 ncr_sc->sc_dma_eop = si_vme_dma_eop;
217 ncr_sc->sc_dma_stop = si_vme_dma_stop;
218 ncr_sc->sc_intr_on = si_vme_intr_on;
219 ncr_sc->sc_intr_off = si_vme_intr_off;
220
221 /* Attach interrupt handler. */
222 isr_add_vectored(si_intr, (void *)sc, ca->ca_intpri, ca->ca_intvec);
223
224 /* Reset the hardware. */
225 si_vme_reset(ncr_sc);
226
227 /* Do the common attach stuff. */
228 si_attach(sc);
229 }
230
231 static void
232 si_vme_reset(struct ncr5380_softc *ncr_sc)
233 {
234 struct si_softc *sc = (struct si_softc *)ncr_sc;
235 volatile struct si_regs *si = sc->sc_regs;
236
237 #ifdef DEBUG
238 if (si_debug) {
239 printf("%s\n", __func__);
240 }
241 #endif
242
243 /*
244 * The SCSI3 controller has an 8K FIFO to buffer data between the
245 * 5380 and the DMA. Make sure it starts out empty.
246 *
247 * The reset bits in the CSR are active low.
248 */
249 si->si_csr = 0;
250 delay(10);
251 si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
252 delay(10);
253 si->fifo_count = 0;
254
255 /* Make sure the DMA engine is stopped. */
256 si->dma_addrh = 0;
257 si->dma_addrl = 0;
258 si->dma_counth = 0;
259 si->dma_countl = 0;
260 si->si_iv_am = sc->sc_adapter_iv_am;
261 si->fifo_cnt_hi = 0;
262 }
263
264 /*
265 * This is called when the bus is going idle,
266 * so we want to enable the SBC interrupts.
267 * That is controlled by the DMA enable!
268 * Who would have guessed!
269 * What a NASTY trick!
270 */
271 void
272 si_vme_intr_on(struct ncr5380_softc *ncr_sc)
273 {
274 struct si_softc *sc = (struct si_softc *)ncr_sc;
275 volatile struct si_regs *si = sc->sc_regs;
276
277 /* receive mode should be safer */
278 si->si_csr &= ~SI_CSR_SEND;
279
280 /* Clear the count so nothing happens. */
281 si->dma_counth = 0;
282 si->dma_countl = 0;
283
284 /* Clear the start address too. (paranoid?) */
285 si->dma_addrh = 0;
286 si->dma_addrl = 0;
287
288 /* Finally, enable the DMA engine. */
289 si->si_csr |= SI_CSR_DMA_EN;
290 }
291
292 /*
293 * This is called when the bus is idle and we are
294 * about to start playing with the SBC chip.
295 */
296 void
297 si_vme_intr_off(struct ncr5380_softc *ncr_sc)
298 {
299 struct si_softc *sc = (struct si_softc *)ncr_sc;
300 volatile struct si_regs *si = sc->sc_regs;
301
302 si->si_csr &= ~SI_CSR_DMA_EN;
303 }
304
305 /*
306 * This function is called during the COMMAND or MSG_IN phase
307 * that precedes a DATA_IN or DATA_OUT phase, in case we need
308 * to setup the DMA engine before the bus enters a DATA phase.
309 *
310 * XXX: The VME adapter appears to suppress SBC interrupts
311 * when the FIFO is not empty or the FIFO count is non-zero!
312 *
313 * On the VME version, setup the start address, but clear the
314 * count (to make sure it stays idle) and set that later.
315 */
316 void
317 si_vme_dma_setup(struct ncr5380_softc *ncr_sc)
318 {
319 struct si_softc *sc = (struct si_softc *)ncr_sc;
320 struct sci_req *sr = ncr_sc->sc_current;
321 struct si_dma_handle *dh = sr->sr_dma_hand;
322 volatile struct si_regs *si = sc->sc_regs;
323 long data_pa;
324 int xlen;
325
326 /*
327 * Get the DVMA mapping for this segment.
328 * XXX - Should separate allocation and mapin.
329 */
330 data_pa = dh->dh_dmaaddr;
331 if (data_pa & 1)
332 panic("%s: bad pa=0x%lx", __func__, data_pa);
333 xlen = dh->dh_dmalen;
334 xlen &= ~1; /* XXX: necessary? */
335 sc->sc_reqlen = xlen; /* XXX: or less? */
336
337 #ifdef DEBUG
338 if (si_debug & 2) {
339 printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n",
340 __func__, dh, data_pa, xlen);
341 }
342 #endif
343
344 /* Set direction (send/recv) */
345 if (dh->dh_flags & SIDH_OUT) {
346 si->si_csr |= SI_CSR_SEND;
347 } else {
348 si->si_csr &= ~SI_CSR_SEND;
349 }
350
351 /* Reset the FIFO. */
352 si->si_csr &= ~SI_CSR_FIFO_RES; /* active low */
353 si->si_csr |= SI_CSR_FIFO_RES;
354
355 if (data_pa & 2) {
356 si->si_csr |= SI_CSR_BPCON;
357 } else {
358 si->si_csr &= ~SI_CSR_BPCON;
359 }
360
361 /* Load the start address. */
362 si->dma_addrh = (uint16_t)(data_pa >> 16);
363 si->dma_addrl = (uint16_t)(data_pa & 0xFFFF);
364
365 /*
366 * Keep the count zero or it may start early!
367 */
368 si->dma_counth = 0;
369 si->dma_countl = 0;
370
371 #if 0
372 /* Clear FIFO counter. (also hits dma_count) */
373 si->fifo_cnt_hi = 0;
374 si->fifo_count = 0;
375 #endif
376 }
377
378
379 void
380 si_vme_dma_start(struct ncr5380_softc *ncr_sc)
381 {
382 struct si_softc *sc = (struct si_softc *)ncr_sc;
383 struct sci_req *sr = ncr_sc->sc_current;
384 struct si_dma_handle *dh = sr->sr_dma_hand;
385 volatile struct si_regs *si = sc->sc_regs;
386 int s, xlen;
387
388 xlen = sc->sc_reqlen;
389
390 /* This MAY be time critical (not sure). */
391 s = splhigh();
392
393 si->dma_counth = (uint16_t)(xlen >> 16);
394 si->dma_countl = (uint16_t)(xlen & 0xFFFF);
395
396 /* Set it anyway, even though dma_count hits it. */
397 si->fifo_cnt_hi = (uint16_t)(xlen >> 16);
398 si->fifo_count = (uint16_t)(xlen & 0xFFFF);
399
400 /*
401 * Acknowledge the phase change. (After DMA setup!)
402 * Put the SBIC into DMA mode, and start the transfer.
403 */
404 if (dh->dh_flags & SIDH_OUT) {
405 *ncr_sc->sci_tcmd = PHASE_DATA_OUT;
406 SCI_CLR_INTR(ncr_sc);
407 *ncr_sc->sci_icmd = SCI_ICMD_DATA;
408 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
409 *ncr_sc->sci_dma_send = 0; /* start it */
410 } else {
411 *ncr_sc->sci_tcmd = PHASE_DATA_IN;
412 SCI_CLR_INTR(ncr_sc);
413 *ncr_sc->sci_icmd = 0;
414 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
415 *ncr_sc->sci_irecv = 0; /* start it */
416 }
417
418 /* Let'er rip! */
419 si->si_csr |= SI_CSR_DMA_EN;
420
421 splx(s);
422 ncr_sc->sc_state |= NCR_DOINGDMA;
423
424 #ifdef DEBUG
425 if (si_debug & 2) {
426 printf("%s: started, flags=0x%x\n",
427 __func__, ncr_sc->sc_state);
428 }
429 #endif
430 }
431
432
433 void
434 si_vme_dma_eop(struct ncr5380_softc *ncr_sc)
435 {
436
437 /* Not needed - DMA was stopped prior to examining sci_csr */
438 }
439
440
441 void
442 si_vme_dma_stop(struct ncr5380_softc *ncr_sc)
443 {
444 struct si_softc *sc = (struct si_softc *)ncr_sc;
445 struct sci_req *sr = ncr_sc->sc_current;
446 struct si_dma_handle *dh = sr->sr_dma_hand;
447 volatile struct si_regs *si = sc->sc_regs;
448 int resid, ntrans;
449
450 if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
451 #ifdef DEBUG
452 printf("%s: DMA not running\n", __func__);
453 #endif
454 return;
455 }
456 ncr_sc->sc_state &= ~NCR_DOINGDMA;
457
458 /* First, halt the DMA engine. */
459 si->si_csr &= ~SI_CSR_DMA_EN; /* VME only */
460
461 /* Set an impossible phase to prevent data movement? */
462 *ncr_sc->sci_tcmd = PHASE_INVALID;
463
464 if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
465 printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
466 sr->sr_xs->error = XS_DRIVER_STUFFUP;
467 ncr_sc->sc_state |= NCR_ABORTING;
468 si_vme_reset(ncr_sc);
469 goto out;
470 }
471
472 /* Note that timeout may have set the error flag. */
473 if (ncr_sc->sc_state & NCR_ABORTING)
474 goto out;
475
476 /* XXX: Wait for DMA to actually finish? */
477
478 /*
479 * Now try to figure out how much actually transferred
480 *
481 * The fifo_count does not reflect how many bytes were
482 * actually transferred for VME.
483 *
484 * SCSI-3 VME interface is a little funny on writes:
485 * if we have a disconnect, the DMA has overshot by
486 * one byte and the resid needs to be incremented.
487 * Only happens for partial transfers.
488 * (Thanks to Matt Jacob)
489 */
490
491 resid = si->fifo_count & 0xFFFF;
492 if (dh->dh_flags & SIDH_OUT)
493 if ((resid > 0) && (resid < sc->sc_reqlen))
494 resid++;
495 ntrans = sc->sc_reqlen - resid;
496
497 #ifdef DEBUG
498 if (si_debug & 2) {
499 printf("%s: resid=0x%x ntrans=0x%x\n",
500 __func__, resid, ntrans);
501 }
502 #endif
503
504 if (ntrans < MIN_DMA_LEN) {
505 printf("si: fifo count: 0x%x\n", resid);
506 ncr_sc->sc_state |= NCR_ABORTING;
507 goto out;
508 }
509 if (ntrans > ncr_sc->sc_datalen)
510 panic("%s: excess transfer", __func__);
511
512 /* Adjust data pointer */
513 ncr_sc->sc_dataptr += ntrans;
514 ncr_sc->sc_datalen -= ntrans;
515
516 /*
517 * After a read, we may need to clean-up
518 * "Left-over bytes" (yuck!)
519 */
520 if (((dh->dh_flags & SIDH_OUT) == 0) &&
521 ((si->si_csr & SI_CSR_LOB) != 0)) {
522 uint8_t *cp = ncr_sc->sc_dataptr;
523 #ifdef DEBUG
524 printf("si: Got Left-over bytes!\n");
525 #endif
526 if (si->si_csr & SI_CSR_BPCON) {
527 /* have SI_CSR_BPCON */
528 cp[-1] = (si->si_bprl & 0xff00) >> 8;
529 } else {
530 switch (si->si_csr & SI_CSR_LOB) {
531 case SI_CSR_LOB_THREE:
532 cp[-3] = (si->si_bprh & 0xff00) >> 8;
533 cp[-2] = (si->si_bprh & 0x00ff);
534 cp[-1] = (si->si_bprl & 0xff00) >> 8;
535 break;
536 case SI_CSR_LOB_TWO:
537 cp[-2] = (si->si_bprh & 0xff00) >> 8;
538 cp[-1] = (si->si_bprh & 0x00ff);
539 break;
540 case SI_CSR_LOB_ONE:
541 cp[-1] = (si->si_bprh & 0xff00) >> 8;
542 break;
543 }
544 }
545 }
546
547 out:
548 si->dma_addrh = 0;
549 si->dma_addrl = 0;
550
551 si->dma_counth = 0;
552 si->dma_countl = 0;
553
554 si->fifo_cnt_hi = 0;
555 si->fifo_count = 0;
556
557 /* Put SBIC back in PIO mode. */
558 *ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
559 *ncr_sc->sci_icmd = 0;
560 }
561