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