mac68k5380.c revision 1.18 1 /* $NetBSD: mac68k5380.c,v 1.18 1996/02/03 23:17:53 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1995 Allen Briggs
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Allen Briggs
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 * Derived from atari5380.c for the mac68k port of NetBSD.
33 *
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/dkstat.h>
41 #include <sys/syslog.h>
42 #include <sys/buf.h>
43 #include <scsi/scsi_all.h>
44 #include <scsi/scsi_message.h>
45 #include <scsi/scsiconf.h>
46
47 /*
48 * Include the driver definitions
49 */
50 #include <mac68k/dev/ncr5380reg.h>
51
52 #include <machine/stdarg.h>
53
54 #include "../mac68k/via.h"
55
56 /*
57 * Set the various driver options
58 */
59 #define NREQ 18 /* Size of issue queue */
60 #define AUTO_SENSE 1 /* Automatically issue a request-sense */
61
62 #define DRNAME ncrscsi /* used in various prints */
63 #undef DBG_SEL /* Show the selection process */
64 #undef DBG_REQ /* Show enqueued/ready requests */
65 #undef DBG_NOWRITE /* Do not allow writes to the targets */
66 #undef DBG_PIO /* Show the polled-I/O process */
67 #undef DBG_INF /* Show information transfer process */
68 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
69 #define DBG_PID 25 /* Keep track of driver */
70 #ifdef DBG_NOSTATIC
71 # define static
72 #endif
73 #ifdef DBG_SEL
74 # define DBG_SELPRINT(a,b) printf(a,b)
75 #else
76 # define DBG_SELPRINT(a,b)
77 #endif
78 #ifdef DBG_PIO
79 # define DBG_PIOPRINT(a,b,c) printf(a,b,c)
80 #else
81 # define DBG_PIOPRINT(a,b,c)
82 #endif
83 #ifdef DBG_INF
84 # define DBG_INFPRINT(a,b,c) a(b,c)
85 #else
86 # define DBG_INFPRINT(a,b,c)
87 #endif
88 #ifdef DBG_PID
89 /* static char *last_hit = NULL, *olast_hit = NULL; */
90 static char *last_hit[DBG_PID];
91 # define PID(a) \
92 { int i; \
93 for (i=0; i< DBG_PID-1; i++) \
94 last_hit[i] = last_hit[i+1]; \
95 last_hit[DBG_PID-1] = a; }
96 #else
97 # define PID(a)
98 #endif
99
100 #undef REAL_DMA /* Use DMA if sensible */
101 #define fair_to_keep_dma() 1
102 #define claimed_dma() 1
103 #define reconsider_dma()
104 #define USE_PDMA 1 /* Use special pdma-transfer function */
105 #define MIN_PHYS 0x2000 /* pdma space w/ /DSACK is only 0x2000 */
106
107 #define ENABLE_NCR5380(sc) cur_softc = sc;
108
109 /*
110 * softc of currently active controller (well, we only have one for now).
111 */
112
113 static struct ncr_softc *cur_softc;
114
115 struct scsi_5380 {
116 volatile u_char scsi_5380[8*16]; /* 8 regs, 1 every 16th byte. */
117 };
118
119 extern vm_offset_t SCSIBase;
120 static volatile u_char *ncr = (volatile u_char *) 0x10000;
121 static volatile u_char *ncr_5380_with_drq = (volatile u_char *) 0x6000;
122 static volatile u_char *ncr_5380_without_drq = (volatile u_char *) 0x12000;
123
124 static volatile u_char *scsi_enable = NULL;
125
126 #define SCSI_5380 ((struct scsi_5380 *) ncr)
127 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[((rnum)<<4)]
128 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[((rnum)<<4)] = (val))
129
130 void ncr5380_irq_intr(void *);
131 void ncr5380_drq_intr(void *);
132
133 static __inline__ void
134 scsi_clr_ipend()
135 {
136 int tmp;
137
138 tmp = GET_5380_REG(NCR5380_IRCV);
139 }
140
141 extern __inline__ void
142 scsi_ienable()
143 {
144 int s;
145
146 s = splhigh();
147 *scsi_enable = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
148 splx(s);
149 }
150
151 extern __inline__ void
152 scsi_idisable()
153 {
154 int s;
155
156 s = splhigh();
157 *scsi_enable = V2IF_SCSIIRQ | V2IF_SCSIDRQ;
158 splx(s);
159 }
160
161 static void
162 scsi_mach_init(sc)
163 struct ncr_softc *sc;
164 {
165 static int initted = 0;
166
167 if (initted++)
168 panic("scsi_mach_init called again.\n");
169
170 ncr = (volatile u_char *)
171 (SCSIBase + (u_long) ncr);
172 ncr_5380_with_drq = (volatile u_char *)
173 (SCSIBase + (u_int) ncr_5380_with_drq);
174 ncr_5380_without_drq = (volatile u_char *)
175 (SCSIBase + (u_int) ncr_5380_without_drq);
176
177 if (VIA2 == VIA2OFF)
178 scsi_enable = Via1Base + VIA2 * 0x2000 + vIER;
179 else
180 scsi_enable = Via1Base + VIA2 * 0x2000 + rIER;
181
182 mac68k_register_scsi_irq(ncr5380_irq_intr, sc);
183 mac68k_register_scsi_drq(ncr5380_drq_intr, sc);
184 }
185
186 static int
187 machine_match(pdp, cdp, auxp, cd)
188 struct device *pdp;
189 struct cfdata *cdp;
190 void *auxp;
191 struct cfdriver *cd;
192 {
193 if (matchbyname(pdp, cdp, auxp) == 0)
194 return 0;
195 if (!mac68k_machine.scsi80)
196 return 0;
197 if (cdp->cf_unit != 0)
198 return 0;
199 return 1;
200 }
201
202 #if USE_PDMA
203 int pdma_5380_dir = 0;
204
205 u_char *pending_5380_data;
206 u_long pending_5380_count;
207
208 #define NCR5380_PDMA_DEBUG 1 /* Maybe we try with this off eventually. */
209
210 #if NCR5380_PDMA_DEBUG
211 int pdma_5380_sends = 0;
212 int pdma_5380_bytes = 0;
213
214 void
215 pdma_stat()
216 {
217 printf("PDMA SCSI: %d xfers completed for %d bytes.\n",
218 pdma_5380_sends, pdma_5380_bytes);
219 printf("pdma_5380_dir = %d\t",
220 pdma_5380_dir);
221 printf("datap = 0x%x, remainder = %d.\n",
222 pending_5380_data, pending_5380_count);
223 scsi_show();
224 }
225 #endif
226
227 void
228 pdma_cleanup(void)
229 {
230 SC_REQ *reqp = connected;
231 int bytes, s;
232
233 s = splbio();
234 PID("pdma_cleanup0");
235
236 pdma_5380_dir = 0;
237
238 #if NCR5380_PDMA_DEBUG
239 pdma_5380_sends++;
240 pdma_5380_bytes+=(reqp->xdata_len - pending_5380_count);
241 #endif
242
243 /*
244 * Update pointers.
245 */
246 reqp->xdata_ptr += reqp->xdata_len - pending_5380_count;
247 reqp->xdata_len = pending_5380_count;
248
249 /*
250 * Reset DMA mode.
251 */
252 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
253
254 /*
255 * Clear any pending interrupts.
256 */
257 scsi_clr_ipend();
258
259 /*
260 * Tell interrupt functions that DMA has ended.
261 */
262 reqp->dr_flag &= ~DRIVER_IN_DMA;
263
264 SET_5380_REG(NCR5380_MODE, IMODE_BASE);
265 SET_5380_REG(NCR5380_ICOM, 0);
266
267 splx(s);
268
269 /*
270 * Back for more punishment.
271 */
272 PID("pdma_cleanup1");
273 run_main(cur_softc);
274 PID("pdma_cleanup2");
275 }
276 #endif
277
278 static __inline__ int
279 pdma_ready()
280 {
281 #if USE_PDMA
282 SC_REQ *reqp = connected;
283 int dmstat, idstat;
284 extern u_char ncr5380_no_parchk;
285
286 PID("pdma_ready0");
287 if (pdma_5380_dir) {
288 PID("pdma_ready1.")
289 /*
290 * If Mr. IRQ isn't set one might wonder how we got
291 * here. It does happen, though.
292 */
293 dmstat = GET_5380_REG(NCR5380_DMSTAT);
294 if (!(dmstat & SC_IRQ_SET)) {
295 PID("pdma_ready2");
296 return 0;
297 }
298 /*
299 * For a phase mis-match, ATN is a "don't care," IRQ is 1 and
300 * all other bits in the Bus & Status Register are 0. Also,
301 * the current SCSI Bus Status Register has a 1 for BSY and
302 * REQ. Since we're just checking that this interrupt isn't a
303 * reselection or a reset, we just check for either.
304 */
305 idstat = GET_5380_REG(NCR5380_IDSTAT);
306 if ( ((dmstat & (0xff & ~SC_ATN_STAT)) == SC_IRQ_SET)
307 && ((idstat & (SC_S_BSY|SC_S_REQ))
308 == (SC_S_BSY | SC_S_REQ)) ) {
309 PID("pdma_ready3");
310 pdma_cleanup();
311 return 1;
312 } else if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
313 if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
314 /* XXX: Should be parity error ???? */
315 reqp->xs->error = XS_DRIVER_STUFFUP;
316 PID("pdma_ready4");
317 /* XXX: is this the right reaction? */
318 pdma_cleanup();
319 return 1;
320 } else if ( !(idstat & SC_S_REQ)
321 || (((idstat>>2) & 7) != reqp->phase)) {
322 #ifdef DIAGNOSTIC
323 /* XXX: is this the right reaction? Can this happen? */
324 scsi_show();
325 printf("Unexpected phase change.\n");
326 #endif
327 reqp->xs->error = XS_DRIVER_STUFFUP;
328 pdma_cleanup();
329 return 1;
330 } else {
331 scsi_show();
332 panic("Spurious interrupt during PDMA xfer.\n");
333 }
334 } else
335 PID("pdma_ready5");
336 #endif
337 return 0;
338 }
339
340 void
341 ncr5380_irq_intr(p)
342 void *p;
343 {
344 struct ncr_softc *sc = p;
345
346 PID("irq");
347 #if USE_PDMA
348 if (pdma_ready()) {
349 return;
350 }
351 #endif
352 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
353 scsi_idisable();
354 ncr_ctrl_intr(cur_softc);
355 }
356 }
357
358 /*
359 * This is the meat of the PDMA transfer.
360 * When we get here, we shove data as fast as the mac can take it.
361 * We depend on several things:
362 * * All macs after the Mac Plus that have a 5380 chip should have a general
363 * logic IC that handshakes data for blind transfers.
364 * * If the SCSI controller finishes sending/receiving data before we do,
365 * the same general logic IC will generate a /BERR for us in short order.
366 * * The fault address for said /BERR minus the base address for the
367 * transfer will be the amount of data that was actually written.
368 *
369 * We use the nofault flag and the setjmp/longjmp in locore.s so we can
370 * detect and handle the bus error for early termination of a command.
371 * This is usually caused by a disconnecting target.
372 */
373 void
374 ncr5380_drq_intr(p)
375 void *p;
376 {
377 #if USE_PDMA
378 extern int *nofault, mac68k_buserr_addr;
379 struct ncr_softc *sc = p;
380 label_t faultbuf;
381 register int count;
382 volatile u_int32_t *long_drq;
383 u_int32_t *long_data;
384 volatile u_int8_t *drq;
385 u_int8_t *data;
386
387 /*
388 * If we're not ready to xfer data, just return.
389 */
390 if ( !(GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
391 || !pdma_5380_dir) {
392 PID("drq0");
393 return;
394 }
395
396 /*
397 * I don't think this should be necessary, but it is
398 * for writes--at least to some devices. They don't
399 * let go of PH_DATAOUT until we do pdma_cleanup().
400 */
401 if (pending_5380_count == 0) {
402 #if DBG_PID
403 if (pdma_5380_dir == 2) {
404 PID("drq1 (in)");
405 } else {
406 PID("drq1 (out)");
407 }
408 #endif
409 pdma_cleanup();
410 return;
411 }
412
413 #if DBG_PID
414 if (pdma_5380_dir == 2) {
415 PID("drq (in)");
416 } else {
417 PID("drq (out)");
418 }
419 #endif
420
421 /*
422 * Setup for a possible bus error caused by SCSI controller
423 * switching out of DATA-IN/OUT before we're done with the
424 * current transfer.
425 */
426 nofault = (int *) &faultbuf;
427
428 if (setjmp((label_t *) nofault)) {
429 PID("drq berr");
430 nofault = (int *) 0;
431 count = ( (u_long) mac68k_buserr_addr
432 - (u_long) ncr_5380_with_drq);
433 if ((count < 0) || (count > pending_5380_count)) {
434 printf("pdma %s: count = %d (0x%x) (pending "
435 "count %d)\n",
436 (pdma_5380_dir == 2) ? "in" : "out",
437 count, count, pending_5380_count);
438 panic("something is wrong");
439 }
440
441 pending_5380_data += count;
442 pending_5380_count -= count;
443
444 PID("end drq early");
445 mac68k_buserr_addr = 0;
446 return;
447 }
448
449 if (pdma_5380_dir == 2) { /* Data In */
450 int resid;
451
452 /*
453 * Get the dest address aligned.
454 */
455 resid = count = min(pending_5380_count,
456 4 - (((int) pending_5380_data) & 0x3));
457 if (count && (count < 4)) {
458 data = (u_int8_t *) pending_5380_data;
459 drq = (u_int8_t *) ncr_5380_with_drq;
460 while (count) {
461 #define R1 *data++ = *drq++
462 R1; count--;
463 #undef R1
464 }
465 pending_5380_data += resid;
466 pending_5380_count -= resid;
467 }
468
469 /*
470 * Get ready to start the transfer.
471 */
472 while (pending_5380_count) {
473 int dcount;
474
475 dcount = count = min(pending_5380_count, MIN_PHYS);
476 long_drq = (volatile u_int32_t *) ncr_5380_with_drq;
477 long_data = (u_int32_t *) pending_5380_data;
478
479 #define R4 *long_data++ = *long_drq++
480 while ( count >= 512 ) {
481 if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)) {
482 nofault = (int *) 0;
483
484 pending_5380_data += (dcount - count);
485 pending_5380_count -= (dcount - count);
486 return;
487 }
488 R4; R4; R4; R4; R4; R4; R4; R4;
489 R4; R4; R4; R4; R4; R4; R4; R4; /* 64 */
490 R4; R4; R4; R4; R4; R4; R4; R4;
491 R4; R4; R4; R4; R4; R4; R4; R4; /* 128 */
492 R4; R4; R4; R4; R4; R4; R4; R4;
493 R4; R4; R4; R4; R4; R4; R4; R4;
494 R4; R4; R4; R4; R4; R4; R4; R4;
495 R4; R4; R4; R4; R4; R4; R4; R4; /* 256 */
496 R4; R4; R4; R4; R4; R4; R4; R4;
497 R4; R4; R4; R4; R4; R4; R4; R4;
498 R4; R4; R4; R4; R4; R4; R4; R4;
499 R4; R4; R4; R4; R4; R4; R4; R4;
500 R4; R4; R4; R4; R4; R4; R4; R4;
501 R4; R4; R4; R4; R4; R4; R4; R4;
502 R4; R4; R4; R4; R4; R4; R4; R4;
503 R4; R4; R4; R4; R4; R4; R4; R4; /* 512 */
504 count -= 512;
505 }
506 while (count >= 4) {
507 R4; count -= 4;
508 }
509 #undef R4
510 data = (u_int8_t *) long_data;
511 drq = (u_int8_t *) long_drq;
512 while (count) {
513 #define R1 *data++ = *drq++
514 R1; count--;
515 #undef R1
516 }
517 pending_5380_count -= dcount;
518 pending_5380_data += dcount;
519 }
520 } else {
521 int resid;
522
523 /*
524 * Get the source address aligned.
525 */
526 resid = count = min(pending_5380_count,
527 4 - (((int) pending_5380_data) & 0x3));
528 if (count && (count < 4)) {
529 data = (u_int8_t *) pending_5380_data;
530 drq = (u_int8_t *) ncr_5380_with_drq;
531 while (count) {
532 #define W1 *drq++ = *data++
533 W1; count--;
534 #undef W1
535 }
536 pending_5380_data += resid;
537 pending_5380_count -= resid;
538 }
539
540 /*
541 * Get ready to start the transfer.
542 */
543 while (pending_5380_count) {
544 int dcount;
545
546 dcount = count = min(pending_5380_count, MIN_PHYS);
547 long_drq = (volatile u_int32_t *) ncr_5380_with_drq;
548 long_data = (u_int32_t *) pending_5380_data;
549
550 #define W4 *long_drq++ = *long_data++
551 while ( count >= 64 ) {
552 W4; W4; W4; W4; W4; W4; W4; W4;
553 W4; W4; W4; W4; W4; W4; W4; W4; /* 64 */
554 count -= 64;
555 }
556 while (count >= 4) {
557 W4; count -= 4;
558 }
559 #undef W4
560 data = (u_int8_t *) long_data;
561 drq = (u_int8_t *) long_drq;
562 while (count) {
563 #define W1 *drq++ = *data++
564 W1; count--;
565 #undef W1
566 }
567 pending_5380_count -= dcount;
568 pending_5380_data += dcount;
569 }
570 }
571
572 /*
573 * OK. No bus error occurred above. Clear the nofault flag
574 * so we no longer short-circuit bus errors.
575 */
576 nofault = (int *) 0;
577
578 PID("end drq");
579 #endif /* if USE_PDMA */
580 }
581
582 #if USE_PDMA
583
584 #define SCSI_TIMEOUT_VAL 10000000
585
586 static int
587 transfer_pdma(phasep, data, count)
588 u_char *phasep;
589 u_char *data;
590 u_long *count;
591 {
592 SC_REQ *reqp = connected;
593 int len = *count, i, scsi_timeout = SCSI_TIMEOUT_VAL;
594 int s, err;
595
596 if (pdma_5380_dir) {
597 panic("ncrscsi: transfer_pdma called when operation already "
598 "pending.\n");
599 }
600 PID("transfer_pdma0")
601
602 /*
603 * Don't bother with PDMA if we can't sleep or for small transfers.
604 */
605 if (reqp->dr_flag & DRIVER_NOINT) {
606 PID("pdma, falling back to transfer_pio.")
607 transfer_pio(phasep, data, count, 0);
608 return -1;
609 }
610
611 /*
612 * We are probably already at spl2(), so this is likely a no-op.
613 * Paranoia.
614 */
615 s = splbio();
616
617 scsi_idisable();
618
619 /*
620 * Match phases with target.
621 */
622 SET_5380_REG(NCR5380_TCOM, *phasep);
623
624 /*
625 * Clear pending interrupts.
626 */
627 scsi_clr_ipend();
628
629 /*
630 * Wait until target asserts BSY.
631 */
632 while ( ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0)
633 && (--scsi_timeout) );
634 if (!scsi_timeout) {
635 #if DIAGNOSTIC
636 printf("scsi timeout: waiting for BSY in %s.\n",
637 (*phasep == PH_DATAOUT) ? "pdma_out" : "pdma_in");
638 #endif
639 goto scsi_timeout_error;
640 }
641
642 /*
643 * Tell the driver that we're in DMA mode.
644 */
645 reqp->dr_flag |= DRIVER_IN_DMA;
646
647 /*
648 * Load transfer values for DRQ interrupt handlers.
649 */
650 pending_5380_data = data;
651 pending_5380_count = len;
652
653 /*
654 * Set the transfer function to be called on DRQ interrupts.
655 * And note that we're waiting.
656 */
657 switch (*phasep) {
658 default:
659 panic("Unexpected phase in transfer_pdma.\n");
660 case PH_DATAOUT:
661 pdma_5380_dir = 1;
662 SET_5380_REG(NCR5380_ICOM, GET_5380_REG(NCR5380_ICOM)|SC_ADTB);
663 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE)|SC_M_DMA);
664 SET_5380_REG(NCR5380_DMSTAT, 0);
665 break;
666 case PH_DATAIN:
667 pdma_5380_dir = 2;
668 SET_5380_REG(NCR5380_ICOM, 0);
669 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE)|SC_M_DMA);
670 SET_5380_REG(NCR5380_IRCV, 0);
671 break;
672 }
673
674 PID("waiting for interrupt.")
675
676 /*
677 * Now that we're set up, enable interrupts and drop processor
678 * priority back down.
679 */
680 scsi_ienable();
681 splx(s);
682 return 0;
683
684 scsi_timeout_error:
685 /*
686 * Clear the DMA mode.
687 */
688 SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
689 return -1;
690 }
691 #endif /* if USE_PDMA */
692
693 /* Include general routines. */
694 #include <mac68k/dev/ncr5380.c>
695