si_obio.c revision 1.28 1 /* $NetBSD: si_obio.c,v 1.28 2004/02/24 15:05:54 wiz 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.28 2004/02/24 15:05:54 wiz 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 __P((struct ncr5380_softc *));
119 void si_obio_dma_start __P((struct ncr5380_softc *));
120 void si_obio_dma_eop __P((struct ncr5380_softc *));
121 void si_obio_dma_stop __P((struct ncr5380_softc *));
122
123 static void si_obio_reset __P((struct ncr5380_softc *));
124
125 static __inline__ void si_obio_udc_write
126 __P((volatile struct si_regs *si, int regnum, int value));
127 static __inline__ int si_obio_udc_read
128 __P((volatile struct si_regs *si, int regnum));
129
130
131 /*
132 * New-style autoconfig attachment
133 */
134
135 static int si_obio_match __P((struct device *, struct cfdata *, void *));
136 static void si_obio_attach __P((struct device *, struct device *, void *));
137
138 CFATTACH_DECL(si_obio, sizeof(struct si_softc),
139 si_obio_match, si_obio_attach, NULL, NULL);
140
141 /*
142 * Options for disconnect/reselect, DMA, and interrupts.
143 * By default, allow disconnect/reselect on targets 4-6.
144 * Those are normally tapes that really need it enabled.
145 */
146 int si_obio_options = 0x0f;
147
148
149 static int
150 si_obio_match(parent, cf, aux)
151 struct device *parent;
152 struct cfdata *cf;
153 void *aux;
154 {
155 struct confargs *ca = aux;
156
157 /* Make sure something is there... */
158 if (bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1) == -1)
159 return (0);
160
161 /* Default interrupt priority. */
162 if (ca->ca_intpri == -1)
163 ca->ca_intpri = 2;
164
165 return (1);
166 }
167
168 static void
169 si_obio_attach(parent, self, args)
170 struct device *parent, *self;
171 void *args;
172 {
173 struct si_softc *sc = (struct si_softc *) self;
174 struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
175 struct cfdata *cf = self->dv_cfdata;
176 struct confargs *ca = args;
177
178 /* Get options from config flags if specified. */
179 if (cf->cf_flags)
180 sc->sc_options = cf->cf_flags;
181 else
182 sc->sc_options = si_obio_options;
183
184 printf(": options=0x%x\n", sc->sc_options);
185
186 sc->sc_adapter_type = ca->ca_bustype;
187 sc->sc_regs = bus_mapin(ca->ca_bustype,
188 ca->ca_paddr, sizeof(struct si_regs));
189
190 /*
191 * MD function pointers used by the MI code.
192 */
193 ncr_sc->sc_pio_out = ncr5380_pio_out;
194 ncr_sc->sc_pio_in = ncr5380_pio_in;
195 ncr_sc->sc_dma_alloc = si_dma_alloc;
196 ncr_sc->sc_dma_free = si_dma_free;
197 ncr_sc->sc_dma_setup = si_obio_dma_setup;
198 ncr_sc->sc_dma_start = si_obio_dma_start;
199 ncr_sc->sc_dma_poll = si_dma_poll;
200 ncr_sc->sc_dma_eop = si_obio_dma_eop;
201 ncr_sc->sc_dma_stop = si_obio_dma_stop;
202 ncr_sc->sc_intr_on = NULL;
203 ncr_sc->sc_intr_off = NULL;
204
205 /* Need DVMA-capable memory for the UDC command block. */
206 sc->sc_dmacmd = dvma_malloc(sizeof (struct udc_table));
207
208 /* Attach interrupt handler. */
209 isr_add_autovect(si_intr, (void *)sc, ca->ca_intpri);
210
211 /* Reset the hardware. */
212 si_obio_reset(ncr_sc);
213
214 /* Do the common attach stuff. */
215 si_attach(sc);
216 }
217
218 static void
219 si_obio_reset(struct ncr5380_softc *ncr_sc)
220 {
221 struct si_softc *sc = (struct si_softc *)ncr_sc;
222 volatile struct si_regs *si = sc->sc_regs;
223
224 #ifdef DEBUG
225 if (si_debug) {
226 printf("si_obio_reset\n");
227 }
228 #endif
229
230 /*
231 * The SCSI3 controller has an 8K FIFO to buffer data between the
232 * 5380 and the DMA. Make sure it starts out empty.
233 *
234 * The reset bits in the CSR are active low.
235 */
236 si->si_csr = 0;
237 delay(10);
238 si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
239 delay(10);
240 si->fifo_count = 0;
241 }
242
243 static __inline__ void
244 si_obio_udc_write(si, regnum, value)
245 volatile struct si_regs *si;
246 int regnum, value;
247 {
248 si->udc_addr = regnum;
249 delay(UDC_WAIT_USEC);
250 si->udc_data = value;
251 delay(UDC_WAIT_USEC);
252 }
253
254 static __inline__ int
255 si_obio_udc_read(si, regnum)
256 volatile struct si_regs *si;
257 int regnum;
258 {
259 int value;
260
261 si->udc_addr = regnum;
262 delay(UDC_WAIT_USEC);
263 value = si->udc_data;
264 delay(UDC_WAIT_USEC);
265
266 return (value);
267 }
268
269
270 /*
271 * This function is called during the COMMAND or MSG_IN phase
272 * that precedes a DATA_IN or DATA_OUT phase, in case we need
273 * to setup the DMA engine before the bus enters a DATA phase.
274 *
275 * The OBIO "si" IGNORES any attempt to set the FIFO count
276 * register after the SCSI bus goes into any DATA phase, so
277 * this function has to setup the evil FIFO logic.
278 */
279 void
280 si_obio_dma_setup(ncr_sc)
281 struct ncr5380_softc *ncr_sc;
282 {
283 struct si_softc *sc = (struct si_softc *)ncr_sc;
284 struct sci_req *sr = ncr_sc->sc_current;
285 struct si_dma_handle *dh = sr->sr_dma_hand;
286 volatile struct si_regs *si = sc->sc_regs;
287 struct udc_table *cmd;
288 long data_pa, cmd_pa;
289 int xlen;
290
291 /*
292 * Get the DVMA mapping for this segment.
293 * XXX - Should separate allocation and mapin.
294 */
295 data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
296 data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
297 if (data_pa & 1)
298 panic("si_dma_start: bad pa=0x%lx", data_pa);
299 xlen = ncr_sc->sc_datalen;
300 sc->sc_reqlen = xlen; /* XXX: or less? */
301
302 #ifdef DEBUG
303 if (si_debug & 2) {
304 printf("si_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
305 dh, data_pa, xlen);
306 }
307 #endif
308
309 /* Reset the UDC. (In case not already reset?) */
310 si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
311
312 /* Reset the FIFO */
313 si->si_csr &= ~SI_CSR_FIFO_RES; /* active low */
314 si->si_csr |= SI_CSR_FIFO_RES;
315
316 /* Set direction (send/recv) */
317 if (dh->dh_flags & SIDH_OUT) {
318 si->si_csr |= SI_CSR_SEND;
319 } else {
320 si->si_csr &= ~SI_CSR_SEND;
321 }
322
323 /* Set the FIFO counter. */
324 si->fifo_count = xlen;
325
326 /* Reset the UDC. */
327 si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
328
329 /*
330 * XXX: Reset the FIFO again! Comment from Sprite:
331 * Go through reset again because of the bug on the 3/50
332 * where bytes occasionally linger in the DMA fifo.
333 */
334 si->si_csr &= ~SI_CSR_FIFO_RES; /* active low */
335 si->si_csr |= SI_CSR_FIFO_RES;
336
337 #ifdef DEBUG
338 /* Make sure the extra FIFO reset did not hit the count. */
339 if (si->fifo_count != xlen) {
340 printf("si_dma_setup: fifo_count=0x%x, xlen=0x%x\n",
341 si->fifo_count, xlen);
342 Debugger();
343 }
344 #endif
345
346 /*
347 * Set up the DMA controller. The DMA controller on
348 * OBIO needs a command block in DVMA space.
349 */
350 cmd = sc->sc_dmacmd;
351 cmd->addrh = ((data_pa & 0xFF0000) >> 8) | UDC_ADDR_INFO;
352 cmd->addrl = data_pa & 0xFFFF;
353 cmd->count = xlen / 2; /* bytes -> words */
354 cmd->cmrh = UDC_CMR_HIGH;
355 if (dh->dh_flags & SIDH_OUT) {
356 if (xlen & 1)
357 cmd->count++;
358 cmd->cmrl = UDC_CMR_LSEND;
359 cmd->rsel = UDC_RSEL_SEND;
360 } else {
361 cmd->cmrl = UDC_CMR_LRECV;
362 cmd->rsel = UDC_RSEL_RECV;
363 }
364
365 /* Tell the DMA chip where the control block is. */
366 cmd_pa = dvma_kvtopa(cmd, BUS_OBIO);
367 si_obio_udc_write(si, UDC_ADR_CAR_HIGH,
368 (cmd_pa & 0xff0000) >> 8);
369 si_obio_udc_write(si, UDC_ADR_CAR_LOW,
370 (cmd_pa & 0xffff));
371
372 /* Tell the chip to be a DMA master. */
373 si_obio_udc_write(si, UDC_ADR_MODE, UDC_MODE);
374
375 /* Tell the chip to interrupt on error. */
376 si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_CIE);
377
378 /* Will do "start chain" command in _dma_start. */
379 }
380
381
382 void
383 si_obio_dma_start(ncr_sc)
384 struct ncr5380_softc *ncr_sc;
385 {
386 struct si_softc *sc = (struct si_softc *)ncr_sc;
387 struct sci_req *sr = ncr_sc->sc_current;
388 struct si_dma_handle *dh = sr->sr_dma_hand;
389 volatile struct si_regs *si = sc->sc_regs;
390 int s;
391
392 #ifdef DEBUG
393 if (si_debug & 2) {
394 printf("si_dma_start: sr=%p\n", sr);
395 }
396 #endif
397
398 /* This MAY be time critical (not sure). */
399 s = splhigh();
400
401 /* Finally, give the UDC a "start chain" command. */
402 si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_STRT_CHN);
403
404 /*
405 * Acknowledge the phase change. (After DMA setup!)
406 * Put the SBIC into DMA mode, and start the transfer.
407 */
408 if (dh->dh_flags & SIDH_OUT) {
409 *ncr_sc->sci_tcmd = PHASE_DATA_OUT;
410 SCI_CLR_INTR(ncr_sc);
411 *ncr_sc->sci_icmd = SCI_ICMD_DATA;
412 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
413 *ncr_sc->sci_dma_send = 0; /* start it */
414 } else {
415 *ncr_sc->sci_tcmd = PHASE_DATA_IN;
416 SCI_CLR_INTR(ncr_sc);
417 *ncr_sc->sci_icmd = 0;
418 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
419 *ncr_sc->sci_irecv = 0; /* start it */
420 }
421
422 splx(s);
423 ncr_sc->sc_state |= NCR_DOINGDMA;
424
425 #ifdef DEBUG
426 if (si_debug & 2) {
427 printf("si_dma_start: started, flags=0x%x\n",
428 ncr_sc->sc_state);
429 }
430 #endif
431 }
432
433
434 void
435 si_obio_dma_eop(ncr_sc)
436 struct ncr5380_softc *ncr_sc;
437 {
438
439 /* Not needed - DMA was stopped prior to examining sci_csr */
440 }
441
442
443 void
444 si_obio_dma_stop(ncr_sc)
445 struct ncr5380_softc *ncr_sc;
446 {
447 struct si_softc *sc = (struct si_softc *)ncr_sc;
448 struct sci_req *sr = ncr_sc->sc_current;
449 struct si_dma_handle *dh = sr->sr_dma_hand;
450 volatile struct si_regs *si = sc->sc_regs;
451 int resid, ntrans, tmo, udc_cnt;
452
453 if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
454 #ifdef DEBUG
455 printf("si_dma_stop: DMA not running\n");
456 #endif
457 return;
458 }
459 ncr_sc->sc_state &= ~NCR_DOINGDMA;
460
461 NCR_TRACE("si_dma_stop: top, csr=0x%x\n", si->si_csr);
462
463 /* OK, have either phase mis-match or end of DMA. */
464 /* Set an impossible phase to prevent data movement? */
465 *ncr_sc->sci_tcmd = PHASE_INVALID;
466
467 /* Check for DMA errors. */
468 if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
469 printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
470 sr->sr_xs->error = XS_DRIVER_STUFFUP;
471 ncr_sc->sc_state |= NCR_ABORTING;
472 si_obio_reset(ncr_sc);
473 goto out;
474 }
475
476 /* Note that timeout may have set the error flag. */
477 if (ncr_sc->sc_state & NCR_ABORTING)
478 goto out;
479
480 /*
481 * After a read, wait for the FIFO to empty.
482 * Note: this only works on the OBIO version.
483 */
484 if ((dh->dh_flags & SIDH_OUT) == 0) {
485 tmo = 200000; /* X10 = 2 sec. */
486 for (;;) {
487 if (si->si_csr & SI_CSR_FIFO_EMPTY)
488 break;
489 if (--tmo <= 0) {
490 printf("si: DMA FIFO did not empty, reset\n");
491 ncr_sc->sc_state |= NCR_ABORTING;
492 /* si_obio_reset(ncr_sc); */
493 goto out;
494 }
495 delay(10);
496 }
497 }
498
499 /*
500 * Now try to figure out how much actually transferred.
501 * The fifo_count might not reflect how many bytes were
502 * actually transferred.
503 */
504 resid = si->fifo_count & 0xFFFF;
505 ntrans = sc->sc_reqlen - resid;
506
507 #ifdef DEBUG
508 if (si_debug & 2) {
509 printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
510 resid, ntrans);
511 }
512 #endif
513
514 /* XXX: Treat (ntrans==0) as a special, non-error case? */
515 if (ntrans < MIN_DMA_LEN) {
516 printf("si: fifo count: 0x%x\n", resid);
517 ncr_sc->sc_state |= NCR_ABORTING;
518 goto out;
519 }
520 if (ntrans > ncr_sc->sc_datalen)
521 panic("si_dma_stop: excess transfer");
522
523 /* Adjust data pointer */
524 ncr_sc->sc_dataptr += ntrans;
525 ncr_sc->sc_datalen -= ntrans;
526
527 /*
528 * After a read, we may need to clean-up
529 * "Left-over bytes" (yuck!)
530 */
531 if ((dh->dh_flags & SIDH_OUT) == 0) {
532 /* If odd transfer count, grab last byte by hand. */
533 if (ntrans & 1) {
534 NCR_TRACE("si_dma_stop: leftover 1 at 0x%x\n",
535 (int) ncr_sc->sc_dataptr - 1);
536 ncr_sc->sc_dataptr[-1] =
537 (si->fifo_data & 0xff00) >> 8;
538 goto out;
539 }
540 /* UDC might not have transfered the last word. */
541 udc_cnt = si_obio_udc_read(si, UDC_ADR_COUNT);
542 if (((udc_cnt * 2) - resid) == 2) {
543 NCR_TRACE("si_dma_stop: leftover 2 at 0x%x\n",
544 (int) ncr_sc->sc_dataptr - 2);
545 ncr_sc->sc_dataptr[-2] =
546 (si->fifo_data & 0xff00) >> 8;
547 ncr_sc->sc_dataptr[-1] =
548 (si->fifo_data & 0x00ff);
549 }
550 }
551
552 out:
553 /* Reset the UDC. */
554 si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
555 si->fifo_count = 0;
556 si->si_csr &= ~SI_CSR_SEND;
557
558 /* Reset the FIFO */
559 si->si_csr &= ~SI_CSR_FIFO_RES; /* active low */
560 si->si_csr |= SI_CSR_FIFO_RES;
561
562 /* Put SBIC back in PIO mode. */
563 /* XXX: set tcmd to PHASE_INVALID? */
564 *ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
565 *ncr_sc->sci_icmd = 0;
566 }
567
568