atari5380.c revision 1.35 1 /* $NetBSD: atari5380.c,v 1.35 2003/05/03 18:10:45 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
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 Leo Weppelman.
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
33 #include "opt_atariscsi.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/buf.h>
40 #include <dev/scsipi/scsi_all.h>
41 #include <dev/scsipi/scsipi_all.h>
42 #include <dev/scsipi/scsi_message.h>
43 #include <dev/scsipi/scsiconf.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <m68k/asm_single.h>
48 #include <m68k/cpu.h>
49 #include <m68k/cacheops.h>
50
51 #include <atari/atari/stalloc.h>
52
53 /*
54 * Include the driver definitions
55 */
56 #include <atari/dev/ncr5380reg.h>
57
58 #include <machine/stdarg.h>
59 #include <machine/iomap.h>
60 #include <machine/mfp.h>
61
62 #include <atari/atari/intr.h>
63
64 #if defined(FALCON_SCSI)
65 #include <machine/dma.h>
66 #endif
67
68 /*
69 * Set the various driver options
70 */
71 #define NREQ 18 /* Size of issue queue */
72 #define AUTO_SENSE 1 /* Automatically issue a request-sense */
73
74 #define DRNAME ncrscsi /* used in various prints */
75 #undef DBG_SEL /* Show the selection process */
76 #undef DBG_REQ /* Show enqueued/ready requests */
77 #undef DBG_ERR_RET /* Show requests with != 0 return code */
78 #undef DBG_NOWRITE /* Do not allow writes to the targets */
79 #undef DBG_PIO /* Show the polled-I/O process */
80 #undef DBG_INF /* Show information transfer process */
81 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
82 #define DBG_PID 15 /* Keep track of driver */
83 #define REAL_DMA /* Use DMA if sensible */
84 #if defined(FALCON_SCSI)
85 #define REAL_DMA_POLL 1 /* 1: Poll for end of DMA-transfer */
86 #else
87 #define REAL_DMA_POLL 0 /* 1: Poll for end of DMA-transfer */
88 #endif
89 #undef USE_PDMA /* Use special pdma-transfer function */
90 #define MIN_PHYS 65536 /*BARF!!!!*/
91
92 /*
93 * Include more driver definitions
94 */
95 #include <atari/dev/ncr5380var.h>
96
97 /*
98 * The atari specific driver options
99 */
100 #undef NO_TTRAM_DMA /* Do not use DMA to TT-ram. This */
101 /* fails on older atari's */
102 #define ENABLE_NCR5380(sc) cur_softc = sc;
103
104 static u_char *alloc_bounceb __P((u_long));
105 static void free_bounceb __P((u_char *));
106 static int machine_match __P((struct device *, void *, void *,
107 struct cfdriver *));
108 void scsi_ctrl __P((int));
109 void scsi_dma __P((int));
110
111 /*
112 * Functions that do nothing on the atari
113 */
114 #define pdma_ready() 0
115
116 #if defined(TT_SCSI)
117
118 void ncr5380_drq_intr __P((int));
119
120 /*
121 * Define all the things we need of the DMA-controller
122 */
123 #define SCSI_DMA ((struct scsi_dma *)AD_SCSI_DMA)
124 #define SCSI_5380 ((struct scsi_5380 *)AD_NCR5380)
125
126 struct scsi_dma {
127 volatile u_char s_dma_ptr[8]; /* use only the odd bytes */
128 volatile u_char s_dma_cnt[8]; /* use only the odd bytes */
129 volatile u_char s_dma_res[4]; /* data residue register */
130 volatile u_char s_dma_gap; /* not used */
131 volatile u_char s_dma_ctrl; /* control register */
132 volatile u_char s_dma_gap2; /* not used */
133 volatile u_char s_hdma_ctrl; /* Hades control register */
134 };
135
136 #define set_scsi_dma(addr, val) (void)( \
137 { \
138 u_char *address = (u_char*)addr+1; \
139 u_long nval = (u_long)val; \
140 __asm("movepl %0, %1@(0)": :"d" (nval), "a" (address)); \
141 })
142
143 #define get_scsi_dma(addr, res) ( \
144 { \
145 u_char *address = (u_char*)addr+1; \
146 u_long nval; \
147 __asm("movepl %1@(0), %0": "=d" (nval) : "a" (address)); \
148 res = (u_long)nval; \
149 })
150
151 /*
152 * Defines for TT-DMA control register
153 */
154 #define SD_BUSERR 0x80 /* 1 = transfer caused bus error*/
155 #define SD_ZERO 0x40 /* 1 = byte counter is zero */
156 #define SD_ENABLE 0x02 /* 1 = Enable DMA */
157 #define SD_OUT 0x01 /* Direction: memory to SCSI */
158 #define SD_IN 0x00 /* Direction: SCSI to memory */
159
160 /*
161 * Defines for Hades-DMA control register
162 */
163 #define SDH_BUSERR 0x02 /* 1 = Bus error */
164 #define SDH_EOP 0x01 /* 1 = Signal EOP on 5380 */
165 #define SDH_ZERO 0x40 /* 1 = Byte counter is zero */
166
167 /*
168 * Define the 5380 register set
169 */
170 struct scsi_5380 {
171 volatile u_char scsi_5380[16]; /* use only the odd bytes */
172 };
173 #endif /* TT_SCSI */
174
175 /**********************************************
176 * Variables present for both TT and Falcon. *
177 **********************************************/
178
179 /*
180 * Softc of currently active controller (a bit of fake; we only have one)
181 */
182 static struct ncr_softc *cur_softc;
183
184 #if defined(TT_SCSI) && !defined(FALCON_SCSI)
185 /*
186 * We can be more efficient for some functions when only TT_SCSI is selected
187 */
188 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
189 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
190
191 #define scsi_mach_init(sc) scsi_tt_init(sc)
192 #define scsi_ienable() scsi_tt_ienable()
193 #define scsi_idisable() scsi_tt_idisable()
194 #define scsi_clr_ipend() scsi_tt_clr_ipend()
195 #define scsi_ipending() (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
196 #define scsi_dma_setup(r,p,m) scsi_tt_dmasetup(r, p, m)
197 #define wrong_dma_range(r,d) tt_wrong_dma_range(r, d)
198 #define poll_edma(reqp) tt_poll_edma(reqp)
199 #define get_dma_result(r, b) tt_get_dma_result(r, b)
200 #define can_access_5380() 1
201 #define emulated_dma() ((machineid & ATARI_HADES) ? 1 : 0)
202
203 #define fair_to_keep_dma() 1
204 #define claimed_dma() 1
205 #define reconsider_dma()
206
207 #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
208
209 #if defined(TT_SCSI)
210
211 /*
212 * Prototype functions defined below
213 */
214 #ifdef NO_TTRAM_DMA
215 static int tt_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
216 #endif
217 static void scsi_tt_init __P((struct ncr_softc *));
218 static u_char get_tt_5380_reg __P((u_short));
219 static void set_tt_5380_reg __P((u_short, u_short));
220 static void scsi_tt_dmasetup __P((SC_REQ *, u_int, u_char));
221 static int tt_poll_edma __P((SC_REQ *));
222 static u_char *ptov __P((SC_REQ *, u_long*));
223 static int tt_get_dma_result __P((SC_REQ *, u_long *));
224 void scsi_tt_ienable __P((void));
225 void scsi_tt_idisable __P((void));
226 void scsi_tt_clr_ipend __P((void));
227
228 /*
229 * Define these too, so we can use them locally...
230 */
231 #define GET_TT_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
232 #define SET_TT_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
233
234 #ifdef NO_TTRAM_DMA
235 static int
236 tt_wrong_dma_range(reqp, dm)
237 SC_REQ *reqp;
238 struct dma_chain *dm;
239 {
240 if (dm->dm_addr & 0xff000000) {
241 reqp->dr_flag |= DRIVER_BOUNCING;
242 return(1);
243 }
244 return(0);
245 }
246 #else
247 #define tt_wrong_dma_range(reqp, dm) 0
248 #endif
249
250 static void
251 scsi_tt_init(struct ncr_softc *sc)
252 {
253 /*
254 * Enable SCSI-related interrupts
255 */
256 MFP2->mf_aer |= 0x80; /* SCSI IRQ goes HIGH!!!!! */
257
258 if (machineid & ATARI_TT) {
259 /* SCSI-DMA interrupts */
260 MFP2->mf_ierb |= IB_SCDM;
261 MFP2->mf_iprb = (u_int8_t)~IB_SCDM;
262 MFP2->mf_imrb |= IB_SCDM;
263 }
264 else if (machineid & ATARI_HADES) {
265 SCSI_DMA->s_hdma_ctrl = 0;
266
267 if (intr_establish(2, AUTO_VEC, 0,
268 (hw_ifun_t)ncr5380_drq_intr,
269 NULL) == NULL)
270 panic("scsi_tt_init: Can't establish drq-interrupt");
271 }
272 else panic("scsi_tt_init: should not come here");
273
274 MFP2->mf_iera |= IA_SCSI; /* SCSI-5380 interrupts */
275 MFP2->mf_ipra = (u_int8_t)~IA_SCSI;
276 MFP2->mf_imra |= IA_SCSI;
277
278 /*
279 * LWP: DMA transfers to TT-ram causes data to be garbeled
280 * without notice on some revisons of the TT-mainboard.
281 * When program's generate misterious Segmentations faults,
282 * try turning on NO_TTRAM_DMA.
283 */
284 #ifdef NO_TTRAM_DMA
285 printf(": DMA to TT-RAM is disabled!");
286 #endif
287 }
288
289 static u_char
290 get_tt_5380_reg(u_short rnum)
291 {
292 return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
293 }
294
295 static void
296 set_tt_5380_reg(u_short rnum, u_short val)
297 {
298 SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
299 }
300
301 extern __inline__ void
302 scsi_tt_ienable(void)
303 {
304 if (machineid & ATARI_TT)
305 single_inst_bset_b(MFP2->mf_imrb, IB_SCDM);
306 single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
307 }
308
309 extern __inline__ void
310 scsi_tt_idisable(void)
311 {
312 if (machineid & ATARI_TT)
313 single_inst_bclr_b(MFP2->mf_imrb, IB_SCDM);
314 single_inst_bclr_b(MFP2->mf_imra, IA_SCSI);
315 }
316
317 extern __inline__ void
318 scsi_tt_clr_ipend(void)
319 {
320 int tmp;
321
322 SCSI_DMA->s_dma_ctrl = 0;
323 tmp = GET_TT_REG(NCR5380_IRCV);
324 if (machineid & ATARI_TT)
325 MFP2->mf_iprb = (u_int8_t)~IB_SCDM;
326 MFP2->mf_ipra = (u_int8_t)~IA_SCSI;
327
328 /*
329 * Remove interrupts already scheduled.
330 */
331 rem_sicallback((si_farg)ncr_ctrl_intr);
332 rem_sicallback((si_farg)ncr_dma_intr);
333 }
334
335 static void
336 scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, u_char mode)
337 {
338 if (PH_IN(phase)) {
339 SCSI_DMA->s_dma_ctrl = SD_IN;
340 if (machineid & ATARI_HADES)
341 SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
342 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
343 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
344 SET_TT_REG(NCR5380_ICOM, 0);
345 SET_TT_REG(NCR5380_MODE, mode);
346 SCSI_DMA->s_dma_ctrl = SD_ENABLE;
347 SET_TT_REG(NCR5380_IRCV, 0);
348 }
349 else {
350 SCSI_DMA->s_dma_ctrl = SD_OUT;
351 if (machineid & ATARI_HADES)
352 SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
353 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
354 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
355 SET_TT_REG(NCR5380_MODE, mode);
356 SET_TT_REG(NCR5380_ICOM, SC_ADTB);
357 SET_TT_REG(NCR5380_DMSTAT, 0);
358 SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
359 }
360 }
361
362 static int
363 tt_poll_edma(SC_REQ *reqp)
364 {
365 u_char dmstat, dmastat;
366 int timeout = 9000; /* XXX */
367
368 /*
369 * We wait here until the DMA has finished. This can be
370 * achieved by checking the following conditions:
371 * - 5380:
372 * - End of DMA flag is set
373 * - We lost BSY (error!!)
374 * - A phase mismatch has occurred (partial transfer)
375 * - DMA-controller:
376 * - A bus error occurred (Kernel error!!)
377 * - All bytes are transferred
378 * If one of the terminating conditions was met, we call
379 * 'dma_ready' to check errors and perform the bookkeeping.
380 */
381
382 scsi_tt_idisable();
383 for (;;) {
384 delay(20);
385 if (--timeout <= 0) {
386 ncr_tprint(reqp, "timeout on polled transfer\n");
387 reqp->xs->error = XS_TIMEOUT;
388 scsi_tt_ienable();
389 return(0);
390 }
391 dmstat = GET_TT_REG(NCR5380_DMSTAT);
392
393 if ((machineid & ATARI_HADES) && (dmstat & SC_DMA_REQ)) {
394 ncr5380_drq_intr(1);
395 dmstat = GET_TT_REG(NCR5380_DMSTAT);
396 }
397
398 dmastat = SCSI_DMA->s_dma_ctrl;
399 if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
400 break;
401 if (!(dmstat & SC_PHS_MTCH))
402 break;
403 if (dmastat & (SD_BUSERR|SD_ZERO))
404 break;
405 }
406 scsi_tt_ienable();
407 return(1);
408 }
409
410 /*
411 * Convert physical DMA address to a virtual address.
412 */
413 static u_char *
414 ptov(SC_REQ *reqp, u_long *phaddr)
415 {
416 struct dma_chain *dm;
417 u_char *vaddr;
418
419 dm = reqp->dm_chain;
420 vaddr = reqp->xdata_ptr;
421 for(; dm < reqp->dm_cur; dm++)
422 vaddr += dm->dm_count;
423 vaddr += (u_long)phaddr - dm->dm_addr;
424 return(vaddr);
425 }
426
427 static int
428 tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
429 {
430 int dmastat, dmstat;
431 u_char *byte_p;
432 u_long leftover;
433
434 dmastat = SCSI_DMA->s_dma_ctrl;
435 dmstat = GET_TT_REG(NCR5380_DMSTAT);
436 get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover);
437 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p);
438
439 if (dmastat & SD_BUSERR) {
440 /*
441 * The DMA-controller seems to access 8 bytes beyond
442 * it's limits on output. Therefore check also the byte
443 * count. If it's zero, ignore the bus error.
444 */
445 if (leftover != 0) {
446 ncr_tprint(reqp,
447 "SCSI-DMA buserror - accessing 0x%x\n", byte_p);
448 reqp->xs->error = XS_DRIVER_STUFFUP;
449 }
450 }
451
452 /*
453 * We handle the following special condition below:
454 * -- The device disconnects in the middle of a write operation --
455 * In this case, the 5380 has already pre-fetched the next byte from
456 * the DMA-controller before the phase mismatch occurs. Therefore,
457 * leftover is 1 too low.
458 * This does not always happen! Therefore, we only do this when
459 * leftover is odd. This assumes that DMA transfers are _even_! This
460 * is normally the case on disks and types but might not always be.
461 * XXX: Check if ACK is consistently high on these occasions LWP
462 */
463 if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase))
464 leftover++;
465
466 /*
467 * Check if there are some 'restbytes' left in the DMA-controller.
468 */
469 if ((machineid & ATARI_TT) && ((u_long)byte_p & 3)
470 && PH_IN(reqp->phase)) {
471 u_char *p, *q;
472
473 p = ptov(reqp, (u_long *)((u_long)byte_p & ~3));
474 q = (u_char*)&(SCSI_DMA->s_dma_res);
475 switch ((u_long)byte_p & 3) {
476 case 3: *p++ = *q++;
477 case 2: *p++ = *q++;
478 case 1: *p++ = *q++;
479 }
480 }
481 *bytes_left = leftover;
482 return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0);
483 }
484
485 static u_char *dma_ptr;
486 void
487 ncr5380_drq_intr(poll)
488 int poll;
489 {
490 extern int *nofault;
491 label_t faultbuf;
492 int write;
493 u_long count;
494 u_char *data_p = (u_char*)(stio_addr+0x741);
495
496 /*
497 * Block SCSI interrupts while emulating DMA. They come
498 * at a higher priority.
499 */
500 single_inst_bclr_b(MFP2->mf_imra, IA_SCSI);
501
502 /*
503 * Setup for a possible bus error caused by SCSI controller
504 * switching out of DATA-IN/OUT before we're done with the
505 * current transfer.
506 */
507 nofault = (int *) &faultbuf;
508
509 if (setjmp((label_t *) nofault)) {
510 u_char *ptr;
511 u_long cnt, tmp;
512
513 PID("drq berr");
514 nofault = (int *) 0;
515
516 /*
517 * Determine number of bytes transferred
518 */
519 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)ptr);
520 cnt = dma_ptr - ptr;
521
522 if (cnt != 0) {
523 /*
524 * Update the DMA pointer/count fields
525 */
526 set_scsi_dma(SCSI_DMA->s_dma_ptr, dma_ptr);
527 get_scsi_dma(SCSI_DMA->s_dma_cnt, tmp);
528 set_scsi_dma(SCSI_DMA->s_dma_cnt, tmp - cnt);
529
530 if (tmp > cnt) {
531 /*
532 * Still more to transfer
533 */
534 if (!poll)
535 single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
536 return;
537 }
538
539 /*
540 * Signal EOP to 5380
541 */
542 SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
543 }
544 else {
545 nofault = (int *) &faultbuf;
546
547 /*
548 * Try to figure out if the byte-count was
549 * zero because there was no (more) data or
550 * because the dma_ptr is bogus.
551 */
552 if (setjmp((label_t *) nofault)) {
553 /*
554 * Set the bus-error bit
555 */
556 SCSI_DMA->s_hdma_ctrl |= SDH_BUSERR;
557 }
558 __asm __volatile ("tstb %0@(0)": : "a" (dma_ptr));
559 nofault = (int *)0;
560 }
561
562 /*
563 * Schedule an interrupt
564 */
565 if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE))
566 add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
567
568 /*
569 * Clear DMA-mode
570 */
571 SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
572 if (!poll)
573 single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
574
575 return;
576 }
577
578 write = (SCSI_DMA->s_dma_ctrl & SD_OUT) ? 1 : 0;
579 #if DBG_PID
580 if (write) {
581 PID("drq (in)");
582 } else {
583 PID("drq (out)");
584 }
585 #endif
586
587 get_scsi_dma(SCSI_DMA->s_dma_cnt, count);
588 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
589
590 /*
591 * Keep pushing bytes until we're done or a bus-error
592 * signals that the SCSI controller is not ready.
593 * NOTE: I tried some optimalizations in these loops,
594 * but they had no effect on transfer speed.
595 */
596 if (write) {
597 while(count--) {
598 *data_p = *dma_ptr++;
599 }
600 }
601 else {
602 while(count--) {
603 *dma_ptr++ = *data_p;
604 }
605 }
606
607 /*
608 * OK. No bus error occurred above. Clear the nofault flag
609 * so we no longer short-circuit bus errors.
610 */
611 nofault = (int *) 0;
612
613 /*
614 * Schedule an interrupt
615 */
616 if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE))
617 add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
618
619 /*
620 * Clear DMA-mode
621 */
622 SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
623
624 /*
625 * Update the DMA 'registers' to reflect that all bytes
626 * have been transfered and tell this to the 5380 too.
627 */
628 set_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
629 set_scsi_dma(SCSI_DMA->s_dma_cnt, 0);
630 SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
631
632 PID("end drq");
633 if (!poll)
634 single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
635
636 return;
637 }
638
639 #endif /* defined(TT_SCSI) */
640
641 #if defined(FALCON_SCSI) && !defined(TT_SCSI)
642
643 #define GET_5380_REG(rnum) get_falcon_5380_reg(rnum)
644 #define SET_5380_REG(rnum,val) set_falcon_5380_reg(rnum, val)
645 #define scsi_mach_init(sc) scsi_falcon_init(sc)
646 #define scsi_ienable() scsi_falcon_ienable()
647 #define scsi_idisable() scsi_falcon_idisable()
648 #define scsi_clr_ipend() scsi_falcon_clr_ipend()
649 #define scsi_ipending() scsi_falcon_ipending()
650 #define scsi_dma_setup(r,p,m) scsi_falcon_dmasetup(r, p, m)
651 #define wrong_dma_range(r,d) falcon_wrong_dma_range(r, d)
652 #define poll_edma(reqp) falcon_poll_edma(reqp)
653 #define get_dma_result(r, b) falcon_get_dma_result(r, b)
654 #define can_access_5380() falcon_can_access_5380()
655 #define emulated_dma() 0
656
657 #define fair_to_keep_dma() (!st_dmawanted())
658 #define claimed_dma() falcon_claimed_dma()
659 #define reconsider_dma() falcon_reconsider_dma()
660
661 #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
662
663 #if defined(FALCON_SCSI)
664
665 /*
666 * Prototype functions defined below
667 */
668 static void scsi_falcon_init __P((struct ncr_softc *));
669 static u_char get_falcon_5380_reg __P((u_short));
670 static void set_falcon_5380_reg __P((u_short, u_short));
671 static int falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
672 static void fal1_dma __P((u_int, u_int, SC_REQ *));
673 static void scsi_falcon_dmasetup __P((SC_REQ *, u_int, u_char));
674 static int falcon_poll_edma __P((SC_REQ *));
675 static int falcon_get_dma_result __P((SC_REQ *, u_long *));
676 int falcon_can_access_5380 __P((void));
677 void scsi_falcon_clr_ipend __P((void));
678 void scsi_falcon_idisable __P((void));
679 void scsi_falcon_ienable __P((void));
680 int scsi_falcon_ipending __P((void));
681 int falcon_claimed_dma __P((void));
682 void falcon_reconsider_dma __P((void));
683
684 static void
685 scsi_falcon_init(sc)
686 struct ncr_softc *sc;
687 {
688 /*
689 * Enable disk related interrupts
690 */
691 MFP->mf_ierb |= IB_DINT;
692 MFP->mf_iprb = (u_int8_t)~IB_DINT;
693 MFP->mf_imrb |= IB_DINT;
694 }
695
696 static u_char
697 get_falcon_5380_reg(rnum)
698 u_short rnum;
699 {
700 DMA->dma_mode = DMA_SCSI + rnum;
701 return(DMA->dma_data);
702 }
703
704 static void
705 set_falcon_5380_reg(rnum, val)
706 u_short rnum, val;
707 {
708 DMA->dma_mode = DMA_SCSI + rnum;
709 DMA->dma_data = val;
710 }
711
712 extern __inline__ void
713 scsi_falcon_ienable()
714 {
715 single_inst_bset_b(MFP->mf_imrb, IB_DINT);
716 }
717
718 extern __inline__ void
719 scsi_falcon_idisable()
720 {
721 single_inst_bclr_b(MFP->mf_imrb, IB_DINT);
722 }
723
724 extern __inline__ void
725 scsi_falcon_clr_ipend()
726 {
727 int tmp;
728
729 tmp = get_falcon_5380_reg(NCR5380_IRCV);
730 rem_sicallback((si_farg)ncr_ctrl_intr);
731 }
732
733 extern __inline__ int
734 scsi_falcon_ipending()
735 {
736 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
737 /*
738 * XXX: When DMA is running, we are only allowed to
739 * check the 5380 when DMA _might_ be finished.
740 */
741 if (MFP->mf_gpip & IO_DINT)
742 return (0); /* XXX: Actually: we're not allowed to check */
743
744 /* LWP: 28-06, must be a DMA interrupt! should the
745 * ST-DMA unit be taken out of DMA mode?????
746 */
747 DMA->dma_mode = 0x90;
748
749 }
750 return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
751 }
752
753 static int
754 falcon_wrong_dma_range(reqp, dm)
755 SC_REQ *reqp;
756 struct dma_chain *dm;
757 {
758 /*
759 * Do not allow chains yet! See also comment with
760 * falcon_poll_edma() !!!
761 */
762 if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
763 reqp->dr_flag |= DRIVER_BOUNCING;
764 return(1);
765 }
766 /*
767 * Never allow DMA to happen on a Falcon when the transfer
768 * size is no multiple of 512. This is the transfer unit of the
769 * ST DMA-controller.
770 */
771 if(dm->dm_count & 511)
772 return(1);
773 return(0);
774 }
775
776 static int falcon_lock = 0;
777
778 extern __inline__ int
779 falcon_claimed_dma()
780 {
781 if (falcon_lock != DMA_LOCK_GRANT) {
782 if (falcon_lock == DMA_LOCK_REQ) {
783 /*
784 * DMA access is being claimed.
785 */
786 return(0);
787 }
788 if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
789 cur_softc, &falcon_lock, 1))
790 return(0);
791 }
792 return(1);
793 }
794
795 extern __inline__ void
796 falcon_reconsider_dma()
797 {
798 if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
799 /*
800 * No need to keep DMA locked by us as we are not currently
801 * connected and no disconnected jobs are pending.
802 */
803 rem_sicallback((si_farg)ncr_ctrl_intr);
804 st_dmafree(cur_softc, &falcon_lock);
805 }
806
807 if (!falcon_lock && (issue_q != NULL)) {
808 /*
809 * We must (re)claim DMA access as there are jobs
810 * waiting in the issue queue.
811 */
812 st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
813 cur_softc, &falcon_lock, 0);
814 }
815 }
816
817 static void
818 fal1_dma(dir, nsects, reqp)
819 u_int dir, nsects;
820 SC_REQ *reqp;
821 {
822 dir <<= 8;
823 st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
824 DMA->dma_mode = 0x90 | dir;
825 DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
826 DMA->dma_mode = 0x90 | dir;
827 DMA->dma_data = nsects;
828 delay(2); /* _really_ needed (Thomas Gerner) */
829 DMA->dma_mode = 0x10 | dir;
830 }
831
832 static void
833 scsi_falcon_dmasetup(reqp, phase, mode)
834 SC_REQ *reqp;
835 u_int phase;
836 u_char mode;
837 {
838 int nsects = reqp->dm_cur->dm_count / 512; /* XXX */
839
840 /*
841 * XXX: We should probably clear the fifo before putting the
842 * 5380 into DMA-mode.
843 */
844 if (PH_IN(phase)) {
845 set_falcon_5380_reg(NCR5380_ICOM, 0);
846 set_falcon_5380_reg(NCR5380_MODE, mode);
847 set_falcon_5380_reg(NCR5380_IRCV, 0);
848 fal1_dma(0, nsects, reqp);
849 }
850 else {
851 set_falcon_5380_reg(NCR5380_MODE, mode);
852 set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
853 set_falcon_5380_reg(NCR5380_DMSTAT, 0);
854 fal1_dma(1, nsects, reqp);
855 }
856 }
857
858 static int
859 falcon_poll_edma(reqp)
860 SC_REQ *reqp;
861 {
862 int timeout = 9000; /* XXX */
863
864 /*
865 * Because of the Falcon hardware, it is impossible to reach
866 * the 5380 while doing DMA-transfers. So we have to rely on
867 * the interrupt line to determine if DMA-has finished. the
868 * DMA-controller itself will never fire an interrupt. This means
869 * that 'broken-up' DMA transfers are not (yet) possible on the
870 * Falcon.
871 */
872 for (;;) {
873 delay(20);
874 if (--timeout <= 0) {
875 ncr_tprint(reqp, "Timeout on polled transfer\n");
876 reqp->xs->error = XS_TIMEOUT;
877 return(0);
878 }
879 if (!(MFP->mf_gpip & IO_DINT))
880 break;
881 }
882 return(1);
883 }
884
885 static int
886 falcon_get_dma_result(reqp, bytes_left)
887 SC_REQ *reqp;
888 u_long *bytes_left;
889 {
890 int rv = 0;
891 int st_dmastat;
892 u_long bytes_done;
893
894 /*
895 * Select sector counter register first (See Atari docu.)
896 */
897 DMA->dma_mode = 0x90;
898 if (!(st_dmastat = DMA->dma_stat) & 0x01) {
899 /*
900 * Misc. DMA-error according to Atari...
901 */
902 ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
903 st_dmaaddr_get());
904 reqp->xs->error = XS_DRIVER_STUFFUP;
905 rv = 1;
906 }
907 /*
908 * Because we NEVER start DMA on the Falcon when the data size
909 * is not a multiple of 512 bytes, we can safely round down the
910 * byte count on writes. We need to because in case of a disconnect,
911 * the DMA has already prefetched the next couple of bytes.
912 * On read, these byte counts are an error. They are logged and
913 * should be handled by the mi-part of the driver.
914 * NOTE: We formerly did this by using the 'byte-count-zero' bit
915 * of the DMA controller, but this didn't seem to work???
916 * [lwp 29/06/96]
917 */
918 bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
919 if (bytes_done & 511) {
920 if (PH_IN(reqp->phase)) {
921 ncr_tprint(reqp, "Byte count on read not a multiple "
922 "of 512 (%ld)\n", bytes_done);
923 }
924 bytes_done &= ~511;
925 }
926 if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
927 rv = 1;
928 return(rv);
929 }
930
931 static int
932 falcon_can_access_5380()
933 {
934 if (connected && (connected->dr_flag & DRIVER_IN_DMA)
935 && (MFP->mf_gpip & IO_DINT))
936 return(0);
937 return(1);
938 }
939
940 #endif /* defined(FALCON_SCSI) */
941
942 #if defined(TT_SCSI) && defined(FALCON_SCSI)
943 /*
944 * Define some functions to support _both_ TT and Falcon SCSI
945 */
946
947 /*
948 * The prototypes first...
949 */
950 static void scsi_mach_init __P((struct ncr_softc *));
951 void scsi_ienable __P((void));
952 void scsi_idisable __P((void));
953 void scsi_clr_ipend __P((void));
954 int scsi_ipending __P((void));
955 void scsi_dma_setup __P((SC_REQ *, u_int, u_char));
956 int wrong_dma_range __P((SC_REQ *, struct dma_chain *));
957 int poll_edma __P((SC_REQ *));
958 int get_dma_result __P((SC_REQ *, u_long *));
959 int can_access_5380 __P((void));
960
961 /*
962 * Register access will be done through the following 2 function pointers.
963 */
964 static u_char (*get_5380_reg) __P((u_short));
965 static void (*set_5380_reg) __P((u_short, u_short));
966
967 #define GET_5380_REG (*get_5380_reg)
968 #define SET_5380_REG (*set_5380_reg)
969
970 static void
971 scsi_mach_init(sc)
972 struct ncr_softc *sc;
973 {
974 if (machineid & ATARI_FALCON) {
975 get_5380_reg = get_falcon_5380_reg;
976 set_5380_reg = set_falcon_5380_reg;
977 scsi_falcon_init(sc);
978 }
979 else {
980 get_5380_reg = get_tt_5380_reg;
981 set_5380_reg = set_tt_5380_reg;
982 scsi_tt_init(sc);
983 }
984 }
985
986 extern __inline__ void
987 scsi_ienable()
988 {
989 if (machineid & ATARI_FALCON)
990 scsi_falcon_ienable();
991 else scsi_tt_ienable();
992 }
993
994 extern __inline__ void
995 scsi_idisable()
996 {
997 if (machineid & ATARI_FALCON)
998 scsi_falcon_idisable();
999 else scsi_tt_idisable();
1000 }
1001
1002 extern __inline__ void
1003 scsi_clr_ipend()
1004 {
1005 if (machineid & ATARI_FALCON)
1006 scsi_falcon_clr_ipend();
1007 else scsi_tt_clr_ipend();
1008 }
1009
1010 extern __inline__ int
1011 scsi_ipending()
1012 {
1013 if (machineid & ATARI_FALCON)
1014 return(scsi_falcon_ipending());
1015 else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
1016 }
1017
1018 extern __inline__ void
1019 scsi_dma_setup(reqp, phase, mbase)
1020 SC_REQ *reqp;
1021 u_int phase;
1022 u_char mbase;
1023 {
1024 if (machineid & ATARI_FALCON)
1025 scsi_falcon_dmasetup(reqp, phase, mbase);
1026 else scsi_tt_dmasetup(reqp, phase, mbase);
1027 }
1028
1029 extern __inline__ int
1030 wrong_dma_range(reqp, dm)
1031 SC_REQ *reqp;
1032 struct dma_chain *dm;
1033 {
1034 if (machineid & ATARI_FALCON)
1035 return(falcon_wrong_dma_range(reqp, dm));
1036 else return(tt_wrong_dma_range(reqp, dm));
1037 }
1038
1039 extern __inline__ int
1040 poll_edma(reqp)
1041 SC_REQ *reqp;
1042 {
1043 if (machineid & ATARI_FALCON)
1044 return(falcon_poll_edma(reqp));
1045 else return(tt_poll_edma(reqp));
1046 }
1047
1048 extern __inline__ int
1049 get_dma_result(reqp, bytes_left)
1050 SC_REQ *reqp;
1051 u_long *bytes_left;
1052 {
1053 if (machineid & ATARI_FALCON)
1054 return(falcon_get_dma_result(reqp, bytes_left));
1055 else return(tt_get_dma_result(reqp, bytes_left));
1056 }
1057
1058 extern __inline__ int
1059 can_access_5380()
1060 {
1061 if (machineid & ATARI_FALCON)
1062 return(falcon_can_access_5380());
1063 return(1);
1064 }
1065
1066 #define emulated_dma() ((machineid & ATARI_HADES) ? 1 : 0)
1067
1068 /*
1069 * Locking stuff. All turns into NOP's on the TT.
1070 */
1071 #define fair_to_keep_dma() ((machineid & ATARI_FALCON) ? \
1072 !st_dmawanted() : 1)
1073 #define claimed_dma() ((machineid & ATARI_FALCON) ? \
1074 falcon_claimed_dma() : 1)
1075 #define reconsider_dma() { \
1076 if(machineid & ATARI_FALCON) \
1077 falcon_reconsider_dma();\
1078 }
1079 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
1080
1081 /**********************************************
1082 * Functions present for both TT and Falcon. *
1083 **********************************************/
1084 /*
1085 * Our autoconfig matching function
1086 */
1087 static int
1088 machine_match(struct device *pdp, void *match, void *auxp,
1089 struct cfdriver *cd)
1090 {
1091 static int we_matched = 0; /* Only one unit */
1092
1093 if (strcmp(auxp, cd->cd_name) || we_matched)
1094 return(0);
1095
1096 we_matched = 1;
1097 return(1);
1098 }
1099
1100 /*
1101 * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
1102 * pool. Allocation here is both contiguous and in the lower 16Mb of
1103 * the address space. Thus being DMA-able for all controllers.
1104 */
1105 static u_char *
1106 alloc_bounceb(u_long len)
1107 {
1108 void *tmp;
1109
1110 return((u_char *)alloc_stmem(len, &tmp));
1111 }
1112
1113 static void
1114 free_bounceb(u_char *bounceb)
1115 {
1116 free_stmem(bounceb);
1117 }
1118
1119 /*
1120 * 5380 interrupt.
1121 */
1122 void
1123 scsi_ctrl(int sr)
1124 {
1125 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
1126 scsi_idisable();
1127 if (!BASEPRI(sr))
1128 add_sicallback((si_farg)ncr_ctrl_intr,
1129 (void *)cur_softc, 0);
1130 else {
1131 spl1();
1132 ncr_ctrl_intr(cur_softc);
1133 spl0();
1134 }
1135 }
1136 }
1137
1138 /*
1139 * DMA controller interrupt
1140 */
1141 void
1142 scsi_dma(int sr)
1143 {
1144 SC_REQ *reqp;
1145
1146 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
1147 scsi_idisable();
1148 if (!BASEPRI(sr))
1149 add_sicallback((si_farg)ncr_dma_intr,
1150 (void *)cur_softc, 0);
1151 else {
1152 spl1();
1153 ncr_dma_intr(cur_softc);
1154 spl0();
1155 }
1156 }
1157 }
1158
1159 /*
1160 * Last but not least... Include the general driver code
1161 */
1162 #include <atari/dev/ncr5380.c>
1163