sbc.c revision 1.6 1 /* $NetBSD: sbc.c,v 1.6 1996/05/08 03:44:56 scottr Exp $ */
2
3 /*
4 * Copyright (c) 1996 Scott Reynolds
5 * Copyright (c) 1995 David Jones
6 * Copyright (c) 1995 Allen Briggs
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the authors may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 * 4. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by David Jones, Allen
22 * Briggs and Scott Reynolds.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * This file contains only the machine-dependent parts of the mac68k
38 * NCR 5380 SCSI driver. (Autoconfig stuff and PDMA functions.)
39 * The machine-independent parts are in ncr5380sbc.c
40 *
41 * Supported hardware includes:
42 * Macintosh II family 5380-based controller
43 *
44 * Credits, history:
45 *
46 * Scott Reynolds wrote this module, based on work by Allen Briggs
47 * (mac68k), David Jones (sun3), and Leo Weppelman (atari). Allen
48 * supplied some crucial interpretation of the NetBSD 1.1 'ncrscsi'
49 * driver. Allen, Gordon W. Ross, and Jason Thorpe all helped to
50 * refine this code, and were considerable sources of moral support.
51 *
52 * The sbc_options code is based on similar code in Jason's modified
53 * NetBSD/sparc 'si' driver.
54 */
55
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/errno.h>
61 #include <sys/device.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65
66 #include <scsi/scsi_all.h>
67 #include <scsi/scsi_debug.h>
68 #include <scsi/scsiconf.h>
69
70 #include <dev/ic/ncr5380reg.h>
71 #include <dev/ic/ncr5380var.h>
72
73 #include <machine/viareg.h>
74
75 #include "sbcreg.h"
76
77 /*
78 * Transfers smaller than this are done using PIO
79 * (on assumption they're not worth PDMA overhead)
80 */
81 #define MIN_DMA_LEN 128
82
83 /*
84 * Transfers larger than 8192 bytes need to be split up
85 * due to the size of the PDMA space.
86 */
87 #define MAX_DMA_LEN 0x2000
88
89 /*
90 * From Guide to the Macintosh Family Hardware, p. 137
91 * These are offsets from SCSIBase (see pmap_bootstrap.c)
92 */
93 #define SBC_REGISTER_OFFSET 0x10000
94 #define SBC_DMA_DRQ_OFFSET 0x06000
95 #define SBC_DMA_NODRQ_OFFSET 0x12000
96
97 #ifdef SBC_DEBUG
98 # define SBC_DB_INTR 0x01
99 # define SBC_DB_DMA 0x02
100 # define SBC_DB_REG 0x04
101 # define SBC_DB_BREAK 0x08
102
103 int sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
104 int sbc_link_flags = 0 /* | SDEV_DB2 */;
105
106 # ifndef DDB
107 # define Debugger() printf("Debug: sbc.c:%d\n", __LINE__)
108 # endif
109 # define SBC_BREAK \
110 do { if (sbc_debug & SBC_DB_BREAK) Debugger(); } while (0)
111 #else
112 # define SBC_BREAK
113 #endif
114
115 /*
116 * This structure is used to keep track of PDMA requests.
117 */
118 struct sbc_pdma_handle {
119 int dh_flags; /* flags */
120 #define SBC_DH_BUSY 0x01 /* This handle is in use */
121 #define SBC_DH_OUT 0x02 /* PDMA data out (write) */
122 u_char *dh_addr; /* data buffer */
123 int dh_len; /* length of data buffer */
124 };
125
126 /*
127 * The first structure member has to be the ncr5380_softc
128 * so we can just cast to go back and forth between them.
129 */
130 struct sbc_softc {
131 struct ncr5380_softc ncr_sc;
132 volatile struct sbc_regs *sc_regs;
133 volatile long *sc_drq_addr;
134 volatile u_char *sc_nodrq_addr;
135 volatile u_char *sc_ienable;
136 volatile u_char *sc_iflag;
137 int sc_options; /* options for this instance. */
138 struct sbc_pdma_handle sc_pdma[SCI_OPENINGS];
139 };
140
141 /*
142 * Options. By default, SCSI interrupts and reselect are disabled.
143 * You may enable either of these features with the `flags' directive
144 * in your kernel's configuration file.
145 *
146 * Alternatively, you can patch your kernel with DDB or some other
147 * mechanism. The sc_options member of the softc is OR'd with
148 * the value in sbc_options.
149 */
150 #define SBC_PDMA 0x01 /* Use PDMA for polled transfers */
151 #define SBC_INTR 0x02 /* Allow SCSI IRQ/DRQ interrupts */
152 #define SBC_RESELECT 0x04 /* Allow disconnect/reselect */
153 #define SBC_OPTIONS_MASK (SBC_RESELECT|SBC_INTR|SBC_PDMA)
154 #define SBC_OPTIONS_BITS "\10\3RESELECT\2INTR\1PDMA"
155 int sbc_options = SBC_PDMA;
156
157 static int sbc_match __P((struct device *, void *, void *));
158 static void sbc_attach __P((struct device *, struct device *, void *));
159 static int sbc_print __P((void *, char *));
160 static void sbc_minphys __P((struct buf *bp));
161
162 static int sbc_wait_busy __P((struct ncr5380_softc *));
163 static int sbc_ready __P((struct ncr5380_softc *));
164 static int sbc_wait_dreq __P((struct ncr5380_softc *));
165 static int sbc_pdma_in __P((struct ncr5380_softc *, int, int, u_char *));
166 static int sbc_pdma_out __P((struct ncr5380_softc *, int, int, u_char *));
167 #ifdef SBC_DEBUG
168 static void decode_5380_intr __P((struct ncr5380_softc *));
169 #endif
170
171 void sbc_intr_enable __P((struct ncr5380_softc *));
172 void sbc_intr_disable __P((struct ncr5380_softc *));
173 void sbc_irq_intr __P((void *));
174 void sbc_drq_intr __P((void *));
175 void sbc_dma_alloc __P((struct ncr5380_softc *));
176 void sbc_dma_free __P((struct ncr5380_softc *));
177 void sbc_dma_poll __P((struct ncr5380_softc *));
178 void sbc_dma_setup __P((struct ncr5380_softc *));
179 void sbc_dma_start __P((struct ncr5380_softc *));
180 void sbc_dma_eop __P((struct ncr5380_softc *));
181 void sbc_dma_stop __P((struct ncr5380_softc *));
182
183 static struct scsi_adapter sbc_ops = {
184 ncr5380_scsi_cmd, /* scsi_cmd() */
185 sbc_minphys, /* scsi_minphys() */
186 NULL, /* open_target_lu() */
187 NULL, /* close_target_lu() */
188 };
189
190 /* This is copied from julian's bt driver */
191 /* "so we have a default dev struct for our link struct." */
192 static struct scsi_device sbc_dev = {
193 NULL, /* Use default error handler. */
194 NULL, /* Use default start handler. */
195 NULL, /* Use default async handler. */
196 NULL, /* Use default "done" routine. */
197 };
198
199 struct cfattach sbc_ca = {
200 sizeof(struct sbc_softc), sbc_match, sbc_attach
201 };
202
203 struct cfdriver sbc_cd = {
204 NULL, "sbc", DV_DULL
205 };
206
207
208 static int
209 sbc_match(parent, match, args)
210 struct device *parent;
211 void *match, *args;
212 {
213 if (!mac68k_machine.scsi80)
214 return 0;
215 return 1;
216 }
217
218 static void
219 sbc_attach(parent, self, args)
220 struct device *parent, *self;
221 void *args;
222 {
223 struct sbc_softc *sc = (struct sbc_softc *) self;
224 struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *) sc;
225 extern vm_offset_t SCSIBase;
226
227 /* Pull in the options flags. */
228 sc->sc_options = ((ncr_sc->sc_dev.dv_cfdata->cf_flags | sbc_options)
229 & SBC_OPTIONS_MASK);
230
231 /*
232 * Set up base address of 5380
233 */
234 sc->sc_regs = (struct sbc_regs *)(SCSIBase + SBC_REGISTER_OFFSET);
235
236 /*
237 * Fill in the prototype scsi_link.
238 */
239 ncr_sc->sc_link.adapter_softc = sc;
240 ncr_sc->sc_link.adapter_target = 7;
241 ncr_sc->sc_link.adapter = &sbc_ops;
242 ncr_sc->sc_link.device = &sbc_dev;
243
244 /*
245 * Initialize fields used by the MI code
246 */
247 ncr_sc->sci_r0 = &sc->sc_regs->sci_pr0.sci_reg;
248 ncr_sc->sci_r1 = &sc->sc_regs->sci_pr1.sci_reg;
249 ncr_sc->sci_r2 = &sc->sc_regs->sci_pr2.sci_reg;
250 ncr_sc->sci_r3 = &sc->sc_regs->sci_pr3.sci_reg;
251 ncr_sc->sci_r4 = &sc->sc_regs->sci_pr4.sci_reg;
252 ncr_sc->sci_r5 = &sc->sc_regs->sci_pr5.sci_reg;
253 ncr_sc->sci_r6 = &sc->sc_regs->sci_pr6.sci_reg;
254 ncr_sc->sci_r7 = &sc->sc_regs->sci_pr7.sci_reg;
255
256 /*
257 * MD function pointers used by the MI code.
258 */
259 ncr_sc->sc_pio_out = sbc_pdma_out;
260 ncr_sc->sc_pio_in = sbc_pdma_in;
261 ncr_sc->sc_dma_alloc = NULL;
262 ncr_sc->sc_dma_free = NULL;
263 ncr_sc->sc_dma_poll = NULL;
264 ncr_sc->sc_intr_on = NULL;
265 ncr_sc->sc_intr_off = NULL;
266 ncr_sc->sc_dma_setup = NULL;
267 ncr_sc->sc_dma_start = NULL;
268 ncr_sc->sc_dma_eop = NULL;
269 ncr_sc->sc_dma_stop = NULL;
270 ncr_sc->sc_flags = 0;
271 ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
272
273 if ((sc->sc_options & SBC_INTR) == 0) {
274 ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
275 } else {
276 if (sc->sc_options & SBC_RESELECT)
277 ncr_sc->sc_flags |= NCR5380_PERMIT_RESELECT;
278 ncr_sc->sc_dma_alloc = sbc_dma_alloc;
279 ncr_sc->sc_dma_free = sbc_dma_free;
280 ncr_sc->sc_dma_poll = sbc_dma_poll;
281 ncr_sc->sc_dma_setup = sbc_dma_setup;
282 ncr_sc->sc_dma_start = sbc_dma_start;
283 ncr_sc->sc_dma_eop = sbc_dma_eop;
284 ncr_sc->sc_dma_stop = sbc_dma_stop;
285 mac68k_register_scsi_drq(sbc_drq_intr, ncr_sc);
286 mac68k_register_scsi_irq(sbc_irq_intr, ncr_sc);
287 }
288
289 /*
290 * Initialize fields used only here in the MD code.
291 */
292 sc->sc_drq_addr = (long *) (SCSIBase + SBC_DMA_DRQ_OFFSET);
293 sc->sc_nodrq_addr = (u_char *) (SCSIBase + SBC_DMA_NODRQ_OFFSET);
294 if (VIA2 == VIA2OFF) {
295 sc->sc_ienable = Via1Base + VIA2 * 0x2000 + vIER;
296 sc->sc_iflag = Via1Base + VIA2 * 0x2000 + vIFR;
297 } else {
298 sc->sc_ienable = Via1Base + VIA2 * 0x2000 + rIER;
299 sc->sc_iflag = Via1Base + VIA2 * 0x2000 + rIFR;
300 }
301
302 if (sc->sc_options)
303 printf(": options=%b", sc->sc_options, SBC_OPTIONS_BITS);
304 printf("\n");
305
306 /* Now enable SCSI interrupts through VIA2, if appropriate */
307 if (sc->sc_options & SBC_INTR)
308 sbc_intr_enable(ncr_sc);
309
310 #ifdef SBC_DEBUG
311 if (sbc_debug)
312 printf("%s: softc=%p regs=%p\n", ncr_sc->sc_dev.dv_xname,
313 sc, sc->sc_regs);
314 ncr_sc->sc_link.flags |= sbc_link_flags;
315 #endif
316
317 /*
318 * Initialize the SCSI controller itself.
319 */
320 ncr5380_init(ncr_sc);
321 ncr5380_reset_scsibus(ncr_sc);
322 config_found(self, &(ncr_sc->sc_link), sbc_print);
323 }
324
325 static int
326 sbc_print(aux, name)
327 void *aux;
328 char *name;
329 {
330 if (name != NULL)
331 printf("%s: scsibus ", name);
332 return UNCONF;
333 }
334
335 static void
336 sbc_minphys(struct buf *bp)
337 {
338 if (bp->b_bcount > MAX_DMA_LEN)
339 bp->b_bcount = MAX_DMA_LEN;
340 return (minphys(bp));
341 }
342
343
344 /***
345 * General support for Mac-specific SCSI logic.
346 ***/
347
348 /* These are used in the following inline functions. */
349 int sbc_wait_busy_timo = 1000 * 5000; /* X2 = 10 S. */
350 int sbc_ready_timo = 1000 * 5000; /* X2 = 10 S. */
351 int sbc_wait_dreq_timo = 1000 * 5000; /* X2 = 10 S. */
352
353 /* Return zero on success. */
354 static __inline__ int
355 sbc_wait_busy(sc)
356 struct ncr5380_softc *sc;
357 {
358 register int timo = sbc_wait_busy_timo;
359 for (;;) {
360 if (SCI_BUSY(sc)) {
361 timo = 0; /* return 0 */
362 break;
363 }
364 if (--timo < 0)
365 break; /* return -1 */
366 delay(2);
367 }
368 return (timo);
369 }
370
371 static __inline__ int
372 sbc_ready(sc)
373 struct ncr5380_softc *sc;
374 {
375 register int timo = sbc_ready_timo;
376
377 for (;;) {
378 if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
379 == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
380 timo = 0;
381 break;
382 }
383 if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0)
384 || (SCI_BUSY(sc) == 0)) {
385 timo = -1;
386 break;
387 }
388 if (--timo < 0)
389 break; /* return -1 */
390 delay(2);
391 }
392 return (timo);
393 }
394
395 static __inline__ int
396 sbc_wait_dreq(sc)
397 struct ncr5380_softc *sc;
398 {
399 register int timo = sbc_wait_dreq_timo;
400
401 for (;;) {
402 if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
403 == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
404 timo = 0;
405 break;
406 }
407 if (--timo < 0)
408 break; /* return -1 */
409 delay(2);
410 }
411 return (timo);
412 }
413
414
415 /***
416 * Macintosh SCSI interrupt support routines.
417 ***/
418
419 void
420 sbc_intr_enable(ncr_sc)
421 struct ncr5380_softc *ncr_sc;
422 {
423 register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
424 int s;
425
426 s = splhigh();
427 *sc->sc_ienable = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
428 splx(s);
429 }
430
431 void
432 sbc_intr_disable(ncr_sc)
433 struct ncr5380_softc *ncr_sc;
434 {
435 register struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
436 int s;
437
438 s = splhigh();
439 *sc->sc_ienable = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
440 splx(s);
441 }
442
443 void
444 sbc_irq_intr(p)
445 void *p;
446 {
447 register struct ncr5380_softc *ncr_sc = p;
448 register int claimed = 0;
449
450 /* How we ever arrive here without IRQ set is a mystery... */
451 if (*ncr_sc->sci_csr & SCI_CSR_INT) {
452 #ifdef SBC_DEBUG
453 if (sbc_debug & SBC_DB_INTR)
454 decode_5380_intr(ncr_sc);
455 #endif
456 claimed = ncr5380_intr(ncr_sc);
457 if (!claimed) {
458 if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
459 && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0))
460 SCI_CLR_INTR(ncr_sc); /* RST interrupt */
461 #ifdef SBC_DEBUG
462 else {
463 printf("%s: spurious intr\n",
464 ncr_sc->sc_dev.dv_xname);
465 SBC_BREAK;
466 }
467 #endif
468 }
469 }
470 }
471
472 #ifdef SBC_DEBUG
473 void
474 decode_5380_intr(ncr_sc)
475 struct ncr5380_softc *ncr_sc;
476 {
477 register u_char csr = *ncr_sc->sci_csr;
478 register u_char bus_csr = *ncr_sc->sci_bus_csr;
479
480 if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
481 ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
482 if (csr & SCI_BUS_IO)
483 printf("%s: reselect\n", ncr_sc->sc_dev.dv_xname);
484 else
485 printf("%s: select\n", ncr_sc->sc_dev.dv_xname);
486 } else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
487 ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
488 printf("%s: dma eop\n", ncr_sc->sc_dev.dv_xname);
489 else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
490 ((bus_csr & ~SCI_BUS_RST) == 0))
491 printf("%s: bus reset\n", ncr_sc->sc_dev.dv_xname);
492 else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
493 ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
494 printf("%s: parity error\n", ncr_sc->sc_dev.dv_xname);
495 else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
496 ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
497 printf("%s: phase mismatch\n", ncr_sc->sc_dev.dv_xname);
498 else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
499 (bus_csr == 0))
500 printf("%s: disconnect\n", ncr_sc->sc_dev.dv_xname);
501 else
502 printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
503 ncr_sc->sc_dev.dv_xname, csr, bus_csr);
504 }
505 #endif
506
507 /***
508 * The following code implements polled PDMA.
509 ***/
510
511 static int
512 sbc_pdma_out(ncr_sc, phase, count, data)
513 struct ncr5380_softc *ncr_sc;
514 int phase;
515 int count;
516 u_char *data;
517 {
518 struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
519 register volatile long *long_data = sc->sc_drq_addr;
520 register volatile u_char *byte_data = sc->sc_nodrq_addr;
521 register int len = count;
522
523 if (count < ncr_sc->sc_min_dma_len || (sc->sc_options & SBC_PDMA) == 0)
524 return ncr5380_pio_out(ncr_sc, phase, count, data);
525
526 if (sbc_wait_busy(ncr_sc) == 0) {
527 *ncr_sc->sci_mode |= SCI_MODE_DMA;
528 *ncr_sc->sci_icmd |= SCI_ICMD_DATA;
529 *ncr_sc->sci_dma_send = 0;
530
531 #define W1 *byte_data = *data++
532 #define W4 *long_data = *((long*)data)++
533 while (len >= 64) {
534 if (sbc_ready(ncr_sc))
535 goto timeout;
536 W1;
537 if (sbc_ready(ncr_sc))
538 goto timeout;
539 W1;
540 if (sbc_ready(ncr_sc))
541 goto timeout;
542 W1;
543 if (sbc_ready(ncr_sc))
544 goto timeout;
545 W1;
546 if (sbc_ready(ncr_sc))
547 goto timeout;
548 W4; W4; W4; W4;
549 W4; W4; W4; W4;
550 W4; W4; W4; W4;
551 W4; W4; W4;
552 len -= 64;
553 }
554 while (len) {
555 if (sbc_ready(ncr_sc))
556 goto timeout;
557 W1;
558 len--;
559 }
560 #undef W1
561 #undef W4
562 if (sbc_wait_dreq(ncr_sc))
563 printf("%s: timeout waiting for DREQ.\n",
564 ncr_sc->sc_dev.dv_xname);
565
566 *byte_data = 0;
567
568 SCI_CLR_INTR(ncr_sc);
569 *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
570 *ncr_sc->sci_icmd = 0;
571 }
572 return count - len;
573
574 timeout:
575 printf("%s: pdma_out: timeout len=%d count=%d\n",
576 ncr_sc->sc_dev.dv_xname, len, count);
577 if ((*ncr_sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) {
578 *ncr_sc->sci_icmd &= ~SCI_ICMD_DATA;
579 --len;
580 }
581
582 SCI_CLR_INTR(ncr_sc);
583 *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
584 *ncr_sc->sci_icmd = 0;
585 return count - len;
586 }
587
588 static int
589 sbc_pdma_in(ncr_sc, phase, count, data)
590 struct ncr5380_softc *ncr_sc;
591 int phase;
592 int count;
593 u_char *data;
594 {
595 struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
596 register volatile long *long_data = sc->sc_drq_addr;
597 register volatile u_char *byte_data = sc->sc_nodrq_addr;
598 register int len = count;
599
600 if (count < ncr_sc->sc_min_dma_len || (sc->sc_options & SBC_PDMA) == 0)
601 return ncr5380_pio_in(ncr_sc, phase, count, data);
602
603 if (sbc_wait_busy(ncr_sc) == 0) {
604 *ncr_sc->sci_mode |= SCI_MODE_DMA;
605 *ncr_sc->sci_icmd |= SCI_ICMD_DATA;
606 *ncr_sc->sci_irecv = 0;
607
608 #define R4 *((long *)data)++ = *long_data
609 #define R1 *data++ = *byte_data
610 while (len >= 1024) {
611 if (sbc_ready(ncr_sc))
612 goto timeout;
613 R4; R4; R4; R4; R4; R4; R4; R4;
614 R4; R4; R4; R4; R4; R4; R4; R4;
615 R4; R4; R4; R4; R4; R4; R4; R4;
616 R4; R4; R4; R4; R4; R4; R4; R4; /* 128 */
617 if (sbc_ready(ncr_sc))
618 goto timeout;
619 R4; R4; R4; R4; R4; R4; R4; R4;
620 R4; R4; R4; R4; R4; R4; R4; R4;
621 R4; R4; R4; R4; R4; R4; R4; R4;
622 R4; R4; R4; R4; R4; R4; R4; R4; /* 256 */
623 if (sbc_ready(ncr_sc))
624 goto timeout;
625 R4; R4; R4; R4; R4; R4; R4; R4;
626 R4; R4; R4; R4; R4; R4; R4; R4;
627 R4; R4; R4; R4; R4; R4; R4; R4;
628 R4; R4; R4; R4; R4; R4; R4; R4; /* 384 */
629 if (sbc_ready(ncr_sc))
630 goto timeout;
631 R4; R4; R4; R4; R4; R4; R4; R4;
632 R4; R4; R4; R4; R4; R4; R4; R4;
633 R4; R4; R4; R4; R4; R4; R4; R4;
634 R4; R4; R4; R4; R4; R4; R4; R4; /* 512 */
635 if (sbc_ready(ncr_sc))
636 goto timeout;
637 R4; R4; R4; R4; R4; R4; R4; R4;
638 R4; R4; R4; R4; R4; R4; R4; R4;
639 R4; R4; R4; R4; R4; R4; R4; R4;
640 R4; R4; R4; R4; R4; R4; R4; R4; /* 640 */
641 if (sbc_ready(ncr_sc))
642 goto timeout;
643 R4; R4; R4; R4; R4; R4; R4; R4;
644 R4; R4; R4; R4; R4; R4; R4; R4;
645 R4; R4; R4; R4; R4; R4; R4; R4;
646 R4; R4; R4; R4; R4; R4; R4; R4; /* 768 */
647 if (sbc_ready(ncr_sc))
648 goto timeout;
649 R4; R4; R4; R4; R4; R4; R4; R4;
650 R4; R4; R4; R4; R4; R4; R4; R4;
651 R4; R4; R4; R4; R4; R4; R4; R4;
652 R4; R4; R4; R4; R4; R4; R4; R4; /* 896 */
653 if (sbc_ready(ncr_sc))
654 goto timeout;
655 R4; R4; R4; R4; R4; R4; R4; R4;
656 R4; R4; R4; R4; R4; R4; R4; R4;
657 R4; R4; R4; R4; R4; R4; R4; R4;
658 R4; R4; R4; R4; R4; R4; R4; R4; /* 1024 */
659 len -= 1024;
660 }
661 while (len >= 128) {
662 if (sbc_ready(ncr_sc))
663 goto timeout;
664 R4; R4; R4; R4; R4; R4; R4; R4;
665 R4; R4; R4; R4; R4; R4; R4; R4;
666 R4; R4; R4; R4; R4; R4; R4; R4;
667 R4; R4; R4; R4; R4; R4; R4; R4; /* 128 */
668 len -= 128;
669 }
670 while (len) {
671 if (sbc_ready(ncr_sc))
672 goto timeout;
673 R1;
674 len--;
675 }
676 #undef R4
677 #undef R1
678 SCI_CLR_INTR(ncr_sc);
679 *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
680 *ncr_sc->sci_icmd = 0;
681 }
682 return count - len;
683
684 timeout:
685 printf("%s: pdma_in: timeout len=%d count=%d\n",
686 ncr_sc->sc_dev.dv_xname, len, count);
687
688 SCI_CLR_INTR(ncr_sc);
689 *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
690 *ncr_sc->sci_icmd = 0;
691 return count - len;
692 }
693
694
695 /***
696 * The following code implements interrupt-driven PDMA.
697 ***/
698
699 /*
700 * This is the meat of the PDMA transfer.
701 * When we get here, we shove data as fast as the mac can take it.
702 * We depend on several things:
703 * * All macs after the Mac Plus that have a 5380 chip should have a general
704 * logic IC that handshakes data for blind transfers.
705 * * If the SCSI controller finishes sending/receiving data before we do,
706 * the same general logic IC will generate a /BERR for us in short order.
707 * * The fault address for said /BERR minus the base address for the
708 * transfer will be the amount of data that was actually written.
709 *
710 * We use the nofault flag and the setjmp/longjmp in locore.s so we can
711 * detect and handle the bus error for early termination of a command.
712 * This is usually caused by a disconnecting target.
713 */
714 void
715 sbc_drq_intr(p)
716 void *p;
717 {
718 extern int *nofault, mac68k_buserr_addr;
719 register struct sbc_softc *sc = (struct sbc_softc *) p;
720 register struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *) p;
721 register struct sci_req *sr = ncr_sc->sc_current;
722 register struct sbc_pdma_handle *dh = sr->sr_dma_hand;
723 label_t faultbuf;
724 volatile u_int32_t *long_drq;
725 u_int32_t *long_data;
726 volatile u_int8_t *drq;
727 u_int8_t *data;
728 register int count;
729 int dcount, resid;
730
731 /*
732 * If we're not ready to xfer data, or have no more, just return.
733 */
734 if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
735 return;
736
737 #ifdef SBC_DEBUG
738 if (sbc_debug & SBC_DB_INTR)
739 printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
740 ncr_sc->sc_dev.dv_xname, dh->dh_len, dh->dh_flags);
741 #endif
742
743 /*
744 * Setup for a possible bus error caused by SCSI controller
745 * switching out of DATA-IN/OUT before we're done with the
746 * current transfer.
747 */
748 nofault = (int *) &faultbuf;
749
750 if (setjmp((label_t *) nofault)) {
751 nofault = (int *) 0;
752 count = ( (u_long) mac68k_buserr_addr
753 - (u_long) sc->sc_drq_addr);
754
755 if ((count < 0) || (count > dh->dh_len)) {
756 printf("%s: complete=0x%x (pending 0x%x)\n",
757 ncr_sc->sc_dev.dv_xname, count, dh->dh_len);
758 panic("something is wrong");
759 }
760 #ifdef SBC_DEBUG
761 if (sbc_debug & SBC_DB_INTR)
762 printf("%s: drq /berr, pending=0x%x, complete=0x%x\n",
763 ncr_sc->sc_dev.dv_xname, dh->dh_len, count);
764 #endif
765
766 dh->dh_addr += count;
767 dh->dh_len -= count;
768 mac68k_buserr_addr = 0;
769
770 return;
771 }
772
773 if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
774 /*
775 * Get the source address aligned.
776 */
777 resid =
778 count = min(dh->dh_len, 4 - (((int) dh->dh_addr) & 0x3));
779 if (count && count < 4) {
780 data = (u_int8_t *) dh->dh_addr;
781 drq = (u_int8_t *) sc->sc_drq_addr;
782 #define W1 *drq++ = *data++
783 while (count) {
784 W1; count--;
785 }
786 #undef W1
787 dh->dh_addr += resid;
788 dh->dh_len -= resid;
789 }
790
791 /*
792 * Get ready to start the transfer.
793 */
794 while (dh->dh_len) {
795 dcount = count = min(dh->dh_len, MAX_DMA_LEN);
796 long_drq = (volatile u_int32_t *) sc->sc_drq_addr;
797 long_data = (u_int32_t *) dh->dh_addr;
798
799 #define W4 *long_drq++ = *long_data++
800 while (count >= 64) {
801 W4; W4; W4; W4; W4; W4; W4; W4;
802 W4; W4; W4; W4; W4; W4; W4; W4; /* 64 */
803 count -= 64;
804 }
805 while (count >= 4) {
806 W4; count -= 4;
807 }
808 #undef W4
809 data = (u_int8_t *) long_data;
810 drq = (u_int8_t *) long_drq;
811 #define W1 *drq++ = *data++
812 while (count) {
813 W1; count--;
814 }
815 #undef W1
816 dh->dh_len -= dcount;
817 dh->dh_addr += dcount;
818 }
819 } else { /* Data In */
820 /*
821 * Get the dest address aligned.
822 */
823 resid =
824 count = min(dh->dh_len, 4 - (((int) dh->dh_addr) & 0x3));
825 if (count && count < 4) {
826 data = (u_int8_t *) dh->dh_addr;
827 drq = (u_int8_t *) sc->sc_drq_addr;
828 #define R1 *data++ = *drq++
829 while (count) {
830 R1; count--;
831 }
832 #undef R1
833 dh->dh_addr += resid;
834 dh->dh_len -= resid;
835 }
836
837 /*
838 * Get ready to start the transfer.
839 */
840 while (dh->dh_len) {
841 dcount = count = min(dh->dh_len, MAX_DMA_LEN);
842 long_drq = (volatile u_int32_t *) sc->sc_drq_addr;
843 long_data = (u_int32_t *) dh->dh_addr;
844
845 #define R4 *long_data++ = *long_drq++
846 while (count >= 512) {
847 if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0) {
848 nofault = (int *) 0;
849
850 dh->dh_addr += (dcount - count);
851 dh->dh_len -= (dcount - count);
852 return;
853 }
854 R4; R4; R4; R4; R4; R4; R4; R4;
855 R4; R4; R4; R4; R4; R4; R4; R4; /* 64 */
856 R4; R4; R4; R4; R4; R4; R4; R4;
857 R4; R4; R4; R4; R4; R4; R4; R4; /* 128 */
858 R4; R4; R4; R4; R4; R4; R4; R4;
859 R4; R4; R4; R4; R4; R4; R4; R4;
860 R4; R4; R4; R4; R4; R4; R4; R4;
861 R4; R4; R4; R4; R4; R4; R4; R4; /* 256 */
862 R4; R4; R4; R4; R4; R4; R4; R4;
863 R4; R4; R4; R4; R4; R4; R4; R4;
864 R4; R4; R4; R4; R4; R4; R4; R4;
865 R4; R4; R4; R4; R4; R4; R4; R4;
866 R4; R4; R4; R4; R4; R4; R4; R4;
867 R4; R4; R4; R4; R4; R4; R4; R4;
868 R4; R4; R4; R4; R4; R4; R4; R4;
869 R4; R4; R4; R4; R4; R4; R4; R4; /* 512 */
870 count -= 512;
871 }
872 while (count >= 4) {
873 R4; count -= 4;
874 }
875 #undef R4
876 data = (u_int8_t *) long_data;
877 drq = (u_int8_t *) long_drq;
878 #define R1 *data++ = *drq++
879 while (count) {
880 R1; count--;
881 }
882 #undef R1
883 dh->dh_len -= dcount;
884 dh->dh_addr += dcount;
885 }
886 }
887
888 /*
889 * OK. No bus error occurred above. Clear the nofault flag
890 * so we no longer short-circuit bus errors.
891 */
892 nofault = (int *) 0;
893 }
894
895 void
896 sbc_dma_alloc(ncr_sc)
897 struct ncr5380_softc *ncr_sc;
898 {
899 struct sbc_softc *sc = (struct sbc_softc *) ncr_sc;
900 struct sci_req *sr = ncr_sc->sc_current;
901 struct scsi_xfer *xs = sr->sr_xs;
902 struct sbc_pdma_handle *dh;
903 int i, xlen;
904
905 #ifdef DIAGNOSTIC
906 if (sr->sr_dma_hand != NULL)
907 panic("sbc_dma_alloc: already have PDMA handle");
908 #endif
909
910 /* Polled transfers shouldn't allocate a PDMA handle. */
911 if (sr->sr_flags & SR_IMMED)
912 return;
913
914 #ifndef SBCTEST
915 /* XXX - we don't trust PDMA writes yet! */
916 if (xs->flags & SCSI_DATA_OUT)
917 return;
918 #endif
919
920 xlen = ncr_sc->sc_datalen;
921
922 /* Make sure our caller checked sc_min_dma_len. */
923 if (xlen < MIN_DMA_LEN)
924 panic("sbc_dma_alloc: len=0x%x\n", xlen);
925
926 /*
927 * Find free PDMA handle. Guaranteed to find one since we
928 * have as many PDMA handles as the driver has processes.
929 * (instances?)
930 */
931 for (i = 0; i < SCI_OPENINGS; i++) {
932 if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
933 goto found;
934 }
935 panic("sbc: no free PDMA handles");
936 found:
937 dh = &sc->sc_pdma[i];
938 dh->dh_flags = SBC_DH_BUSY;
939 dh->dh_addr = ncr_sc->sc_dataptr;
940 dh->dh_len = xlen;
941
942 /* Copy the 'write' flag for convenience. */
943 if (xs->flags & SCSI_DATA_OUT)
944 dh->dh_flags |= SBC_DH_OUT;
945
946 sr->sr_dma_hand = dh;
947 }
948
949 void
950 sbc_dma_free(ncr_sc)
951 struct ncr5380_softc *ncr_sc;
952 {
953 struct sci_req *sr = ncr_sc->sc_current;
954 struct sbc_pdma_handle *dh = sr->sr_dma_hand;
955
956 #ifdef DIAGNOSTIC
957 if (sr->sr_dma_hand == NULL)
958 panic("sbc_dma_free: no DMA handle");
959 #endif
960
961 if (ncr_sc->sc_state & NCR_DOINGDMA)
962 panic("sbc_dma_free: free while in progress");
963
964 if (dh->dh_flags & SBC_DH_BUSY) {
965 dh->dh_flags = 0;
966 dh->dh_addr = NULL;
967 dh->dh_len = 0;
968 }
969 sr->sr_dma_hand = NULL;
970 }
971
972 void
973 sbc_dma_poll(ncr_sc)
974 struct ncr5380_softc *ncr_sc;
975 {
976 struct sci_req *sr = ncr_sc->sc_current;
977
978 /*
979 * We shouldn't arrive here; if SR_IMMED is set, then
980 * dma_alloc() should have refused to allocate a handle
981 * for the transfer. This forces the polled PDMA code
982 * to handle the request...
983 */
984 #ifdef SBC_DEBUG
985 if (sbc_debug & SBC_DB_DMA)
986 printf("%s: lost DRQ interrupt?\n", ncr_sc->sc_dev.dv_xname);
987 #endif
988 sr->sr_flags |= SR_OVERDUE;
989 }
990
991 void
992 sbc_dma_setup(ncr_sc)
993 struct ncr5380_softc *ncr_sc;
994 {
995 /* Not needed; we don't have real DMA */
996 }
997
998 void
999 sbc_dma_start(ncr_sc)
1000 struct ncr5380_softc *ncr_sc;
1001 {
1002 struct sci_req *sr = ncr_sc->sc_current;
1003 struct sbc_pdma_handle *dh = sr->sr_dma_hand;
1004
1005 /*
1006 * Match bus phase, set DMA mode, and assert data bus (for
1007 * writing only), then start the transfer.
1008 */
1009 if (dh->dh_flags & SBC_DH_OUT) {
1010 *ncr_sc->sci_tcmd = PHASE_DATA_OUT;
1011 SCI_CLR_INTR(ncr_sc);
1012 *ncr_sc->sci_mode |= SCI_MODE_DMA;
1013 *ncr_sc->sci_icmd = SCI_ICMD_DATA;
1014 *ncr_sc->sci_dma_send = 0;
1015 } else {
1016 *ncr_sc->sci_tcmd = PHASE_DATA_IN;
1017 SCI_CLR_INTR(ncr_sc);
1018 *ncr_sc->sci_mode |= SCI_MODE_DMA;
1019 *ncr_sc->sci_icmd = 0;
1020 *ncr_sc->sci_irecv = 0;
1021 }
1022 ncr_sc->sc_state |= NCR_DOINGDMA;
1023
1024 #ifdef SBC_DEBUG
1025 if (sbc_debug & SBC_DB_DMA)
1026 printf("%s: PDMA started, va=%p, len=0x%x\n",
1027 ncr_sc->sc_dev.dv_xname, dh->dh_addr, dh->dh_len);
1028 #endif
1029 }
1030
1031 void
1032 sbc_dma_eop(ncr_sc)
1033 struct ncr5380_softc *ncr_sc;
1034 {
1035 /* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
1036 }
1037
1038 void
1039 sbc_dma_stop(ncr_sc)
1040 struct ncr5380_softc *ncr_sc;
1041 {
1042 struct sci_req *sr = ncr_sc->sc_current;
1043 struct sbc_pdma_handle *dh = sr->sr_dma_hand;
1044 register int ntrans;
1045
1046 if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
1047 #ifdef SBC_DEBUG
1048 if (sbc_debug & SBC_DB_DMA)
1049 printf("%s: dma_stop: DMA not running\n",
1050 ncr_sc->sc_dev.dv_xname);
1051 #endif
1052 return;
1053 }
1054 ncr_sc->sc_state &= ~NCR_DOINGDMA;
1055
1056 if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
1057 ntrans = ncr_sc->sc_datalen - dh->dh_len;
1058
1059 #ifdef SBC_DEBUG
1060 if (sbc_debug & SBC_DB_DMA)
1061 printf("%s: dma_stop: ntrans=0x%x\n",
1062 ncr_sc->sc_dev.dv_xname, ntrans);
1063 #endif
1064
1065 if (ntrans > ncr_sc->sc_datalen)
1066 panic("sbc_dma_stop: excess transfer\n");
1067
1068 /* Adjust data pointer */
1069 ncr_sc->sc_dataptr += ntrans;
1070 ncr_sc->sc_datalen -= ntrans;
1071
1072 /* Clear any pending interrupts. */
1073 SCI_CLR_INTR(ncr_sc);
1074 }
1075
1076 /* Put SBIC back into PIO mode. */
1077 *ncr_sc->sci_mode &= ~SCI_MODE_DMA;
1078 *ncr_sc->sci_icmd = 0;
1079
1080 #ifdef SBC_DEBUG
1081 if (sbc_debug & SBC_DB_REG)
1082 printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
1083 ncr_sc->sc_dev.dv_xname, *ncr_sc->sci_csr,
1084 *ncr_sc->sci_bus_csr);
1085 #endif
1086 }
1087