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