atari5380.c revision 1.11 1 /* $NetBSD: atari5380.c,v 1.11 1996/04/12 09:05:31 leo 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/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/buf.h>
38 #include <scsi/scsi_all.h>
39 #include <scsi/scsi_message.h>
40 #include <scsi/scsiconf.h>
41
42 #include <atari/atari/stalloc.h>
43
44 /*
45 * Include the driver definitions
46 */
47 #include <atari/dev/ncr5380reg.h>
48
49 #include <machine/stdarg.h>
50 #include <machine/iomap.h>
51 #include <machine/mfp.h>
52
53 #if defined(FALCON_SCSI)
54 #include <machine/dma.h>
55 #endif
56
57 /*
58 * Set the various driver options
59 */
60 #define NREQ 18 /* Size of issue queue */
61 #define AUTO_SENSE 1 /* Automatically issue a request-sense */
62
63 #define DRNAME ncrscsi /* used in various prints */
64 #undef DBG_SEL /* Show the selection process */
65 #undef DBG_REQ /* Show enqueued/ready requests */
66 #undef DBG_ERR_RET /* Show requests with != 0 return code */
67 #undef DBG_NOWRITE /* Do not allow writes to the targets */
68 #undef DBG_PIO /* Show the polled-I/O process */
69 #undef DBG_INF /* Show information transfer process */
70 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/
71 #define DBG_PID 25 /* Keep track of driver */
72 #define REAL_DMA /* Use DMA if sensible */
73 #if defined(FALCON_SCSI)
74 #define REAL_DMA_POLL 1 /* 1: Poll for end of DMA-transfer */
75 #else
76 #define REAL_DMA_POLL 0 /* 1: Poll for end of DMA-transfer */
77 #endif
78 #undef USE_PDMA /* Use special pdma-transfer function */
79 #define MIN_PHYS 65536 /*BARF!!!!*/
80
81 /*
82 * Include more driver definitions
83 */
84 #include <atari/dev/ncr5380var.h>
85
86
87 /*
88 * This is crap, but because the interrupts now run at MFP spl-level (6),
89 * splbio() is not enough at some places. The code should be checked to
90 * find out where splhigh() is needed and where splbio() should be used.
91 * Now that I use this interrupt sceme, the spl values are fake!
92 */
93 #undef splbio()
94 #define splbio() splhigh()
95
96 /*
97 * The atari specific driver options
98 */
99 #undef NO_TTRAM_DMA /* Do not use DMA to TT-ram. This */
100 /* fails on older atari's */
101 #define ENABLE_NCR5380(sc) cur_softc = sc;
102
103 static u_char *alloc_bounceb __P((u_long));
104 static void free_bounceb __P((u_char *));
105 static int machine_match __P((struct device *, void *, void *,
106 struct cfdriver *));
107 void scsi_ctrl __P((int));
108 void scsi_dma __P((int));
109
110 /*
111 * Functions that do nothing on the atari
112 */
113 #define pdma_ready() 0
114
115 #if defined(TT_SCSI)
116 /*
117 * Define all the things we need of the DMA-controller
118 */
119 #define SCSI_DMA ((struct scsi_dma *)AD_SCSI_DMA)
120 #define SCSI_5380 ((struct scsi_5380 *)AD_NCR5380)
121
122 struct scsi_dma {
123 volatile u_char s_dma_ptr[8]; /* use only the odd bytes */
124 volatile u_char s_dma_cnt[8]; /* use only the odd bytes */
125 volatile u_char s_dma_res[4]; /* data residue register */
126 volatile u_char s_dma_gap; /* not used */
127 volatile u_char s_dma_ctrl; /* control register */
128 };
129
130 #define set_scsi_dma(addr, val) (void)( \
131 { \
132 u_char *address = (u_char*)addr+1; \
133 u_long nval = (u_long)val; \
134 __asm("movepl %0, %1@(0)": :"d" (nval), "a" (address)); \
135 })
136
137 #define get_scsi_dma(addr, res) ( \
138 { \
139 u_char *address = (u_char*)addr+1; \
140 u_long nval; \
141 __asm("movepl %1@(0), %0": "=d" (nval) : "a" (address)); \
142 res = (u_long)nval; \
143 })
144
145 /*
146 * Defines for TT-DMA control register
147 */
148 #define SD_BUSERR 0x80 /* 1 = transfer caused bus error*/
149 #define SD_ZERO 0x40 /* 1 = byte counter is zero */
150 #define SD_ENABLE 0x02 /* 1 = Enable DMA */
151 #define SD_OUT 0x01 /* Direction: memory to SCSI */
152 #define SD_IN 0x00 /* Direction: SCSI to memory */
153
154 /*
155 * Define the 5380 register set
156 */
157 struct scsi_5380 {
158 volatile u_char scsi_5380[16]; /* use only the odd bytes */
159 };
160 #endif /* TT_SCSI */
161
162 /**********************************************
163 * Variables present for both TT and Falcon. *
164 **********************************************/
165
166 /*
167 * Softc of currently active controller (a bit of fake; we only have one)
168 */
169 static struct ncr_softc *cur_softc;
170
171 #if defined(TT_SCSI) && !defined(FALCON_SCSI)
172 /*
173 * We can be more efficient for some functions when only TT_SCSI is selected
174 */
175 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
176 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
177
178 #define scsi_mach_init(sc) scsi_tt_init(sc)
179 #define scsi_ienable() scsi_tt_ienable()
180 #define scsi_idisable() scsi_tt_idisable()
181 #define scsi_clr_ipend() scsi_tt_clr_ipend()
182 #define scsi_ipending() (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
183 #define scsi_dma_setup(r,p,m) scsi_tt_dmasetup(r, p, m)
184 #define wrong_dma_range(r,d) tt_wrong_dma_range(r, d)
185 #define poll_edma(reqp) tt_poll_edma(reqp)
186 #define get_dma_result(r, b) tt_get_dma_result(r, b)
187
188 #define fair_to_keep_dma() 1
189 #define claimed_dma() 1
190 #define reconsider_dma()
191
192 #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
193
194 #if defined(TT_SCSI)
195
196 /*
197 * Prototype functions defined below
198 */
199 #ifdef NO_TTRAM_DMA
200 static int tt_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
201 #endif
202 static void scsi_tt_init __P((struct ncr_softc *));
203 static u_char get_tt_5380_reg __P((u_short));
204 static void set_tt_5380_reg __P((u_short, u_short));
205 static void scsi_tt_dmasetup __P((SC_REQ *, u_int, u_char));
206 static int tt_poll_edma __P((SC_REQ *));
207 static u_char *ptov __P((SC_REQ *, u_long*));
208 static int tt_get_dma_result __P((SC_REQ *, u_long *));
209 void scsi_tt_ienable __P((void));
210 void scsi_tt_idisable __P((void));
211 void scsi_tt_clr_ipend __P((void));
212
213 /*
214 * Define these too, so we can use them locally...
215 */
216 #define GET_TT_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1]
217 #define SET_TT_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
218
219 #ifdef NO_TTRAM_DMA
220 static int
221 tt_wrong_dma_range(reqp, dm)
222 SC_REQ *reqp;
223 struct dma_chain *dm;
224 {
225 if (dm->dm_addr & 0xff000000) {
226 reqp->dr_flag |= DRIVER_BOUNCING;
227 return(1);
228 }
229 return(0);
230 }
231 #else
232 #define tt_wrong_dma_range(reqp, dm) 0
233 #endif
234
235 static void
236 scsi_tt_init(struct ncr_softc *sc)
237 {
238 /*
239 * Enable SCSI-related interrupts
240 */
241 MFP2->mf_aer |= 0x80; /* SCSI IRQ goes HIGH!!!!! */
242
243 MFP2->mf_ierb |= IB_SCDM; /* SCSI-dma interrupts */
244 MFP2->mf_iprb &= ~IB_SCDM;
245 MFP2->mf_imrb |= IB_SCDM;
246
247 MFP2->mf_iera |= IA_SCSI; /* SCSI-5380 interrupts */
248 MFP2->mf_ipra &= ~IA_SCSI;
249 MFP2->mf_imra |= IA_SCSI;
250
251 /*
252 * LWP: DMA transfers to TT-ram causes data to be garbeled
253 * without notice on some revisons of the TT-mainboard.
254 * When program's generate misterious Segmentations faults,
255 * try turning on NO_TTRAM_DMA.
256 */
257 #ifdef NO_TTRAM_DMA
258 printf(": DMA to TT-RAM is disabled!");
259 #endif
260 }
261
262 static u_char
263 get_tt_5380_reg(u_short rnum)
264 {
265 return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
266 }
267
268 static void
269 set_tt_5380_reg(u_short rnum, u_short val)
270 {
271 SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
272 }
273
274 extern __inline__ void
275 scsi_tt_ienable(void)
276 {
277 int sps = splbio();
278 MFP2->mf_ierb |= IB_SCDM;
279 MFP2->mf_iera |= IA_SCSI;
280 splx(sps);
281 }
282
283 extern __inline__ void
284 scsi_tt_idisable(void)
285 {
286 int sps = splbio();
287 MFP2->mf_ierb &= ~IB_SCDM;
288 MFP2->mf_iera &= ~IA_SCSI;
289 splx(sps);
290 }
291
292 extern __inline__ void
293 scsi_tt_clr_ipend(void)
294 {
295 int tmp;
296
297 SCSI_DMA->s_dma_ctrl = 0;
298 tmp = GET_TT_REG(NCR5380_IRCV);
299 }
300
301 static void
302 scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, u_char mode)
303 {
304 if (PH_IN(phase)) {
305 SCSI_DMA->s_dma_ctrl = SD_IN;
306 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
307 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
308 SET_TT_REG(NCR5380_ICOM, 0);
309 SET_TT_REG(NCR5380_MODE, mode);
310 SCSI_DMA->s_dma_ctrl = SD_ENABLE;
311 SET_TT_REG(NCR5380_IRCV, 0);
312 }
313 else {
314 SCSI_DMA->s_dma_ctrl = SD_OUT;
315 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
316 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
317 SET_TT_REG(NCR5380_MODE, mode);
318 SET_TT_REG(NCR5380_ICOM, SC_ADTB);
319 SET_TT_REG(NCR5380_DMSTAT, 0);
320 SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
321 }
322 }
323
324 static int
325 tt_poll_edma(SC_REQ *reqp)
326 {
327 u_char dmstat, dmastat;
328 int timeout = 9000; /* XXX */
329
330 /*
331 * We wait here until the DMA has finished. This can be
332 * achieved by checking the following conditions:
333 * - 5380:
334 * - End of DMA flag is set
335 * - We lost BSY (error!!)
336 * - A phase mismatch has occured (partial transfer)
337 * - DMA-controller:
338 * - A bus error occurred (Kernel error!!)
339 * - All bytes are transferred
340 * If one of the terminating conditions was met, we call
341 * 'dma_ready' to check errors and perform the bookkeeping.
342 */
343
344 for (;;) {
345 delay(20);
346 if (--timeout <= 0) {
347 ncr_tprint(reqp, "timeout on polled transfer\n");
348 reqp->xs->error = XS_TIMEOUT;
349 return(0);
350 }
351 dmstat = GET_TT_REG(NCR5380_DMSTAT);
352 dmastat = SCSI_DMA->s_dma_ctrl;
353 if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
354 break;
355 if (!(dmstat & SC_PHS_MTCH))
356 break;
357 if (dmastat & (SD_BUSERR|SD_ZERO))
358 break;
359 }
360 return(1);
361 }
362
363 /*
364 * Convert physical DMA address to a virtual address.
365 */
366 static u_char *
367 ptov(SC_REQ *reqp, u_long *phaddr)
368 {
369 struct dma_chain *dm;
370 u_char *vaddr;
371
372 dm = reqp->dm_chain;
373 vaddr = reqp->xdata_ptr;
374 for(; dm < reqp->dm_cur; dm++)
375 vaddr += dm->dm_count;
376 vaddr += (u_long)phaddr - dm->dm_addr;
377 return(vaddr);
378 }
379
380 static int
381 tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
382 {
383 int dmastat, dmstat;
384 u_char *byte_p;
385 u_long leftover;
386
387 dmastat = SCSI_DMA->s_dma_ctrl;
388 dmstat = GET_TT_REG(NCR5380_DMSTAT);
389 get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover);
390 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p);
391
392 if (dmastat & SD_BUSERR) {
393 /*
394 * The DMA-controller seems to access 8 bytes beyond
395 * it's limits on output. Therefore check also the byte
396 * count. If it's zero, ignore the bus error.
397 */
398 if (leftover != 0) {
399 ncr_tprint(reqp,
400 "SCSI-DMA buserror - accessing 0x%x\n", byte_p);
401 reqp->xs->error = XS_DRIVER_STUFFUP;
402 }
403 }
404
405 /*
406 * We handle the following special condition below:
407 * -- The device disconnects in the middle of a write operation --
408 * In this case, the 5380 has already pre-fetched the next byte from
409 * the DMA-controller before the phase mismatch occurs. Therefore,
410 * leftover is 1 too low.
411 * This does not always happen! Therefore, we only do this when
412 * leftover is odd. This assumes that DMA transfers are _even_! This
413 * is normally the case on disks and types but might not always be.
414 * XXX: Check if ACK is consistently high on these occasions LWP
415 */
416 if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase))
417 leftover++;
418
419 /*
420 * Check if there are some 'restbytes' left in the DMA-controller.
421 */
422 if (((u_long)byte_p & 3) && PH_IN(reqp->phase)) {
423 u_char *p, *q;
424
425 p = ptov(reqp, (u_long *)((u_long)byte_p & ~3));
426 q = (u_char*)&(SCSI_DMA->s_dma_res);
427 switch ((u_long)byte_p & 3) {
428 case 3: *p++ = *q++;
429 case 2: *p++ = *q++;
430 case 1: *p++ = *q++;
431 }
432 }
433 *bytes_left = leftover;
434 return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0);
435 }
436
437 #endif /* defined(TT_SCSI) */
438
439 #if defined(FALCON_SCSI) && !defined(TT_SCSI)
440
441 #define GET_5380_REG(rnum) get_falcon_5380_reg(rnum)
442 #define SET_5380_REG(rnum,val) set_falcon_5380_reg(rnum, val)
443 #define scsi_mach_init(sc) scsi_falcon_init(sc)
444 #define scsi_ienable() scsi_falcon_ienable()
445 #define scsi_idisable() scsi_falcon_idisable()
446 #define scsi_clr_ipend() scsi_falcon_clr_ipend()
447 #define scsi_ipending() scsi_falcon_ipending()
448 #define scsi_dma_setup(r,p,m) scsi_falcon_dmasetup(r, p, m)
449 #define wrong_dma_range(r,d) falcon_wrong_dma_range(r, d)
450 #define poll_edma(reqp) falcon_poll_edma(reqp)
451 #define get_dma_result(r, b) falcon_get_dma_result(r, b)
452
453 #define fair_to_keep_dma() (!st_dmawanted())
454 #define claimed_dma() falcon_claimed_dma()
455 #define reconsider_dma() falcon_reconsider_dma()
456
457 #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
458
459 #if defined(FALCON_SCSI)
460
461 /*
462 * Prototype functions defined below
463 */
464 static void fscsi_int __P((void));
465 static void scsi_falcon_init __P((struct ncr_softc *));
466 static u_char get_falcon_5380_reg __P((u_short));
467 static void set_falcon_5380_reg __P((u_short, u_short));
468 static int falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
469 static void fal1_dma __P((u_int, u_int, SC_REQ *));
470 static void scsi_falcon_dmasetup __P((SC_REQ *, u_int, u_char));
471 static int falcon_poll_edma __P((SC_REQ *));
472 static int falcon_get_dma_result __P((SC_REQ *, u_long *));
473 void scsi_falcon_ienable __P((void));
474 void scsi_falcon_idisable __P((void));
475 void scsi_falcon_clr_ipend __P((void));
476 int scsi_falcon_ipending __P((void));
477 int falcon_claimed_dma __P((void));
478 void falcon_reconsider_dma __P((void));
479
480 static void
481 scsi_falcon_init(sc)
482 struct ncr_softc *sc;
483 {
484 /*
485 * Enable disk related interrupts
486 */
487 MFP->mf_ierb |= IB_DINT;
488 MFP->mf_iprb &= ~IB_DINT;
489 MFP->mf_imrb |= IB_DINT;
490 }
491
492 static u_char
493 get_falcon_5380_reg(rnum)
494 u_short rnum;
495 {
496 DMA->dma_mode = DMA_SCSI + rnum;
497 return(DMA->dma_data);
498 }
499
500 static void
501 set_falcon_5380_reg(rnum, val)
502 u_short rnum, val;
503 {
504 DMA->dma_mode = DMA_SCSI + rnum;
505 DMA->dma_data = val;
506 }
507
508 extern __inline__ void
509 scsi_falcon_ienable()
510 {
511 MFP->mf_ierb |= IB_DINT;
512 }
513
514 extern __inline__ void
515 scsi_falcon_idisable()
516 {
517 MFP->mf_ierb &= ~IB_DINT;
518 }
519
520 extern __inline__ void
521 scsi_falcon_clr_ipend()
522 {
523 int tmp;
524
525 tmp = get_falcon_5380_reg(NCR5380_IRCV);
526 }
527
528 extern __inline__ int
529 scsi_falcon_ipending()
530 {
531 return(!(MFP->mf_gpip & IO_DINT)
532 && (get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET));
533 }
534
535 static int
536 falcon_wrong_dma_range(reqp, dm)
537 SC_REQ *reqp;
538 struct dma_chain *dm;
539 {
540 /*
541 * Do not allow chains yet! See also comment with
542 * falcon_poll_edma() !!!
543 */
544 if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
545 reqp->dr_flag |= DRIVER_BOUNCING;
546 return(1);
547 }
548 /*
549 * Never allow DMA to happen on a Falcon when the transfer
550 * size is no multiple of 512. This is the transfer unit of the
551 * ST DMA-controller.
552 */
553 if(dm->dm_count & 511)
554 return(1);
555 return(0);
556 }
557
558 static int falcon_lock = 0;
559
560 extern __inline__ int
561 falcon_claimed_dma()
562 {
563 if (falcon_lock != DMA_LOCK_GRANT) {
564 if (falcon_lock == DMA_LOCK_REQ) {
565 /*
566 * DMA access is being claimed.
567 */
568 return(0);
569 }
570 if (!st_dmagrab((dma_farg)fscsi_int, (dma_farg)run_main,
571 &connected, &falcon_lock, 1))
572 return(0);
573 }
574 return(1);
575 }
576
577 extern __inline__ void
578 falcon_reconsider_dma()
579 {
580 if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
581 /*
582 * No need to keep DMA locked by us as we are not currently
583 * connected and no disconnected jobs are pending.
584 */
585 st_dmafree(&connected, &falcon_lock);
586 }
587
588 if (!falcon_lock && (issue_q != NULL)) {
589 /*
590 * We must (re)claim DMA access as there are jobs
591 * waiting in the issue queue.
592 */
593 st_dmagrab((dma_farg)fscsi_int, (dma_farg)run_main,
594 &connected, &falcon_lock, 0);
595 }
596 }
597
598 static void
599 fal1_dma(dir, nsects, reqp)
600 u_int dir, nsects;
601 SC_REQ *reqp;
602 {
603 dir <<= 8;
604 st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
605 DMA->dma_mode = 0x90 | dir;
606 DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
607 DMA->dma_mode = 0x90 | dir;
608 DMA->dma_data = nsects;
609 delay(2); /* _really_ needed (Thomas Gerner) */
610 DMA->dma_mode = 0x10 | dir;
611 }
612
613 static void
614 scsi_falcon_dmasetup(reqp, phase, mode)
615 SC_REQ *reqp;
616 u_int phase;
617 u_char mode;
618 {
619 int nsects = reqp->dm_cur->dm_count / 512; /* XXX */
620
621 /*
622 * XXX: We should probably clear the fifo before putting the
623 * 5380 into DMA-mode.
624 */
625 if (PH_IN(phase)) {
626 set_falcon_5380_reg(NCR5380_ICOM, 0);
627 set_falcon_5380_reg(NCR5380_MODE, mode);
628 set_falcon_5380_reg(NCR5380_IRCV, 0);
629 fal1_dma(0, nsects, reqp);
630 }
631 else {
632 set_falcon_5380_reg(NCR5380_MODE, mode);
633 set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
634 set_falcon_5380_reg(NCR5380_DMSTAT, 0);
635 fal1_dma(1, nsects, reqp);
636 }
637 }
638
639 /*
640 * Falcon SCSI interrupt. _Always_ called at spl1!
641 */
642 static void
643 fscsi_int()
644 {
645 int itype;
646 int dma_done;
647
648 if (scsi_falcon_ipending()) {
649 scsi_falcon_idisable();
650 ncr_ctrl_intr(cur_softc);
651 }
652 }
653
654 static int
655 falcon_poll_edma(reqp)
656 SC_REQ *reqp;
657 {
658 int timeout = 9000; /* XXX */
659
660 /*
661 * Because of the Falcon hardware, it is impossible to reach
662 * the 5380 while doing DMA-transfers. So we have to rely on
663 * the interrupt line to determine if DMA-has finished. the
664 * DMA-controller itself will never fire an interrupt. This means
665 * that 'broken-up' DMA transfers are not (yet) possible on the
666 * Falcon.
667 */
668 for (;;) {
669 delay(20);
670 if (--timeout <= 0) {
671 ncr_tprint(reqp, "Timeout on polled transfer\n");
672 reqp->xs->error = XS_TIMEOUT;
673 return(0);
674 }
675 if (!(MFP->mf_gpip & IO_DINT))
676 break;
677 }
678 return(1);
679 }
680
681 static int
682 falcon_get_dma_result(reqp, bytes_left)
683 SC_REQ *reqp;
684 u_long *bytes_left;
685 {
686 int rv = 0;
687 int st_dmastat;
688 u_long bytes_done;
689
690 /*
691 * Select sector counter register first (See Atari docu.)
692 */
693 DMA->dma_mode = 0x90;
694 if (!(st_dmastat = DMA->dma_stat) & 0x01) {
695 /*
696 * Misc. DMA-error according to Atari...
697 */
698 ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
699 st_dmaaddr_get());
700 reqp->xs->error = XS_DRIVER_STUFFUP;
701 rv = 1;
702 }
703 if (st_dmastat & 0x02) {
704 /*
705 * Bytecount not zero.... As the fifo loads in 16 byte
706 * chunks, check if bytes are stuck in fifo.
707 * As we don't use DMA on chunks less than 512 bytes
708 * on the Falcon, report any residual not a multiple of
709 * 512 as an error...
710 */
711 bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
712 if (bytes_done & 511) {
713 ncr_tprint(reqp, "Some bytes stuck in fifo\n");
714 bytes_done &= ~511;
715 reqp->xs->error = XS_DRIVER_STUFFUP;
716 }
717 *bytes_left = reqp->dm_cur->dm_count - bytes_done;
718 }
719 else {
720 *bytes_left = 0;
721 rv = 1;
722 }
723 return(rv);
724 }
725
726 #endif /* defined(FALCON_SCSI) */
727
728 #if defined(TT_SCSI) && defined(FALCON_SCSI)
729 /*
730 * Define some functions to support _both_ TT and Falcon SCSI
731 */
732
733 /*
734 * The prototypes first...
735 */
736 static void scsi_mach_init __P((struct ncr_softc *));
737 void scsi_ienable __P((void));
738 void scsi_idisable __P((void));
739 void scsi_clr_ipend __P((void));
740 int scsi_ipending __P((void));
741 void scsi_dma_setup __P((SC_REQ *, u_int, u_char));
742 int wrong_dma_range __P((SC_REQ *, struct dma_chain *));
743 int poll_edma __P((SC_REQ *));
744 int get_dma_result __P((SC_REQ *, u_long *));
745
746 /*
747 * Register access will be done through the following 2 function pointers.
748 */
749 static u_char (*get_5380_reg) __P((u_short));
750 static void (*set_5380_reg) __P((u_short, u_short));
751
752 #define GET_5380_REG (*get_5380_reg)
753 #define SET_5380_REG (*set_5380_reg)
754
755 static void
756 scsi_mach_init(sc)
757 struct ncr_softc *sc;
758 {
759 if (machineid & ATARI_FALCON) {
760 get_5380_reg = get_falcon_5380_reg;
761 set_5380_reg = set_falcon_5380_reg;
762 scsi_falcon_init(sc);
763 }
764 else {
765 get_5380_reg = get_tt_5380_reg;
766 set_5380_reg = set_tt_5380_reg;
767 scsi_tt_init(sc);
768 }
769 }
770
771 extern __inline__ void
772 scsi_ienable()
773 {
774 if (machineid & ATARI_FALCON)
775 scsi_falcon_ienable();
776 else scsi_tt_ienable();
777 }
778
779 extern __inline__ void
780 scsi_idisable()
781 {
782 if (machineid & ATARI_FALCON)
783 scsi_falcon_idisable();
784 else scsi_tt_idisable();
785 }
786
787 extern __inline__ void
788 scsi_clr_ipend()
789 {
790 if (machineid & ATARI_FALCON)
791 scsi_falcon_clr_ipend();
792 else scsi_tt_clr_ipend();
793 }
794
795 extern __inline__ int
796 scsi_ipending()
797 {
798 if (machineid & ATARI_FALCON)
799 return(scsi_falcon_ipending());
800 else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
801 }
802
803 extern __inline__ void
804 scsi_dma_setup(reqp, phase, mbase)
805 SC_REQ *reqp;
806 u_int phase;
807 u_char mbase;
808 {
809 if (machineid & ATARI_FALCON)
810 scsi_falcon_dmasetup(reqp, phase, mbase);
811 else scsi_tt_dmasetup(reqp, phase, mbase);
812 }
813
814 extern __inline__ int
815 wrong_dma_range(reqp, dm)
816 SC_REQ *reqp;
817 struct dma_chain *dm;
818 {
819 if (machineid & ATARI_FALCON)
820 return(falcon_wrong_dma_range(reqp, dm));
821 else return(tt_wrong_dma_range(reqp, dm));
822 }
823
824 extern __inline__ int
825 poll_edma(reqp)
826 SC_REQ *reqp;
827 {
828 if (machineid & ATARI_FALCON)
829 return(falcon_poll_edma(reqp));
830 else return(tt_poll_edma(reqp));
831 }
832
833 extern __inline__ int
834 get_dma_result(reqp, bytes_left)
835 SC_REQ *reqp;
836 u_long *bytes_left;
837 {
838 if (machineid & ATARI_FALCON)
839 return(falcon_get_dma_result(reqp, bytes_left));
840 else return(tt_get_dma_result(reqp, bytes_left));
841 }
842
843 /*
844 * Locking stuff. All turns into NOP's on the TT.
845 */
846 #define fair_to_keep_dma() ((machineid & ATARI_FALCON) ? \
847 !st_dmawanted() : 1)
848 #define claimed_dma() ((machineid & ATARI_FALCON) ? \
849 falcon_claimed_dma() : 1)
850 #define reconsider_dma() { \
851 if(machineid & ATARI_FALCON) \
852 falcon_reconsider_dma();\
853 }
854 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
855
856 /**********************************************
857 * Functions present for both TT and Falcon. *
858 **********************************************/
859 /*
860 * Our autoconfig matching function
861 */
862 static int
863 machine_match(struct device *pdp, void *match, void *auxp,
864 struct cfdriver *cd)
865 {
866 struct cfdata *cdp = match;
867
868 if (strcmp(auxp, cd->cd_name))
869 return(0);
870 if (cdp->cf_unit != 0) /* Only one unit */
871 return(0);
872 return(1);
873 }
874
875 /*
876 * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
877 * pool. Allocation here is both contiguous and in the lower 16Mb of
878 * the address space. Thus being DMA-able for all controllers.
879 */
880 static u_char *
881 alloc_bounceb(u_long len)
882 {
883 u_long tmp;
884
885 return((u_char *)alloc_stmem(len, &tmp));
886 }
887
888 static void
889 free_bounceb(u_char *bounceb)
890 {
891 free_stmem(bounceb);
892 }
893
894 /*
895 * 5380 interrupt.
896 */
897 void
898 scsi_ctrl(int sr)
899 {
900 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
901 scsi_idisable();
902 if (!BASEPRI(sr))
903 add_sicallback(ncr_ctrl_intr, cur_softc, 0);
904 else {
905 spl1();
906 ncr_ctrl_intr(cur_softc);
907 }
908 }
909 }
910
911 /*
912 * DMA controller interrupt
913 */
914 void
915 scsi_dma(int sr)
916 {
917 SC_REQ *reqp;
918
919 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
920 scsi_idisable();
921 if (!BASEPRI(sr))
922 add_sicallback(ncr_dma_intr, cur_softc, 0);
923 else {
924 spl1();
925 ncr_dma_intr(cur_softc);
926 }
927 }
928 }
929
930 /*
931 * Last but not least... Include the general driver code
932 */
933 #include <atari/dev/ncr5380.c>
934