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