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