atari5380.c revision 1.15 1 /* $NetBSD: atari5380.c,v 1.15 1996/06/18 11:10:04 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 15 /* Keep track of driver */
72 #define REAL_DMA /* Use DMA if sensible */
73 #if defined(notdef) && 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 scsi_falcon_init __P((struct ncr_softc *));
465 static u_char get_falcon_5380_reg __P((u_short));
466 static void set_falcon_5380_reg __P((u_short, u_short));
467 static int falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
468 static void fal1_dma __P((u_int, u_int, SC_REQ *));
469 static void scsi_falcon_dmasetup __P((SC_REQ *, u_int, u_char));
470 static int falcon_poll_edma __P((SC_REQ *));
471 static int falcon_get_dma_result __P((SC_REQ *, u_long *));
472 void scsi_falcon_clr_ipend __P((void));
473 void scsi_falcon_idisable __P((void));
474 void scsi_falcon_ienable __P((void));
475 int scsi_falcon_ipending __P((void));
476 int falcon_claimed_dma __P((void));
477 void falcon_reconsider_dma __P((void));
478
479 static void
480 scsi_falcon_init(sc)
481 struct ncr_softc *sc;
482 {
483 /*
484 * Enable disk related interrupts
485 */
486 MFP->mf_ierb |= IB_DINT;
487 MFP->mf_iprb &= ~IB_DINT;
488 MFP->mf_imrb |= IB_DINT;
489 }
490
491 static u_char
492 get_falcon_5380_reg(rnum)
493 u_short rnum;
494 {
495 DMA->dma_mode = DMA_SCSI + rnum;
496 return(DMA->dma_data);
497 }
498
499 static void
500 set_falcon_5380_reg(rnum, val)
501 u_short rnum, val;
502 {
503 DMA->dma_mode = DMA_SCSI + rnum;
504 DMA->dma_data = val;
505 }
506
507 extern __inline__ void
508 scsi_falcon_ienable()
509 {
510 MFP->mf_ierb |= IB_DINT;
511 }
512
513 extern __inline__ void
514 scsi_falcon_idisable()
515 {
516 MFP->mf_ierb &= ~IB_DINT;
517 }
518
519 extern __inline__ void
520 scsi_falcon_clr_ipend()
521 {
522 int tmp;
523
524 tmp = get_falcon_5380_reg(NCR5380_IRCV);
525 rem_sicallback((si_farg)ncr_ctrl_intr);
526 }
527
528 extern __inline__ int
529 scsi_falcon_ipending()
530 {
531 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
532 /*
533 * XXX: When DMA is running, we are only allowed to
534 * check the 5380 when DMA _might_ be finished.
535 */
536 if (MFP->mf_gpip & IO_DINT)
537 return (0); /* XXX: Actually: we're not allowed to check */
538 }
539 return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
540 }
541
542 static int
543 falcon_wrong_dma_range(reqp, dm)
544 SC_REQ *reqp;
545 struct dma_chain *dm;
546 {
547 /*
548 * Do not allow chains yet! See also comment with
549 * falcon_poll_edma() !!!
550 */
551 if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
552 reqp->dr_flag |= DRIVER_BOUNCING;
553 return(1);
554 }
555 /*
556 * Never allow DMA to happen on a Falcon when the transfer
557 * size is no multiple of 512. This is the transfer unit of the
558 * ST DMA-controller.
559 */
560 if(dm->dm_count & 511)
561 return(1);
562 return(0);
563 }
564
565 static int falcon_lock = 0;
566
567 extern __inline__ int
568 falcon_claimed_dma()
569 {
570 if (falcon_lock != DMA_LOCK_GRANT) {
571 if (falcon_lock == DMA_LOCK_REQ) {
572 /*
573 * DMA access is being claimed.
574 */
575 return(0);
576 }
577 if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
578 cur_softc, &falcon_lock, 1))
579 return(0);
580 }
581 return(1);
582 }
583
584 extern __inline__ void
585 falcon_reconsider_dma()
586 {
587 if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
588 /*
589 * No need to keep DMA locked by us as we are not currently
590 * connected and no disconnected jobs are pending.
591 */
592 rem_sicallback((si_farg)ncr_ctrl_intr);
593 st_dmafree(cur_softc, &falcon_lock);
594 }
595
596 if (!falcon_lock && (issue_q != NULL)) {
597 /*
598 * We must (re)claim DMA access as there are jobs
599 * waiting in the issue queue.
600 */
601 st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
602 cur_softc, &falcon_lock, 0);
603 }
604 }
605
606 static void
607 fal1_dma(dir, nsects, reqp)
608 u_int dir, nsects;
609 SC_REQ *reqp;
610 {
611 dir <<= 8;
612 st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
613 DMA->dma_mode = 0x90 | dir;
614 DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
615 DMA->dma_mode = 0x90 | dir;
616 DMA->dma_data = nsects;
617 delay(2); /* _really_ needed (Thomas Gerner) */
618 DMA->dma_mode = 0x10 | dir;
619 }
620
621 static void
622 scsi_falcon_dmasetup(reqp, phase, mode)
623 SC_REQ *reqp;
624 u_int phase;
625 u_char mode;
626 {
627 int nsects = reqp->dm_cur->dm_count / 512; /* XXX */
628
629 /*
630 * XXX: We should probably clear the fifo before putting the
631 * 5380 into DMA-mode.
632 */
633 if (PH_IN(phase)) {
634 set_falcon_5380_reg(NCR5380_ICOM, 0);
635 set_falcon_5380_reg(NCR5380_MODE, mode);
636 set_falcon_5380_reg(NCR5380_IRCV, 0);
637 fal1_dma(0, nsects, reqp);
638 }
639 else {
640 set_falcon_5380_reg(NCR5380_MODE, mode);
641 set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
642 set_falcon_5380_reg(NCR5380_DMSTAT, 0);
643 fal1_dma(1, nsects, reqp);
644 }
645 }
646
647 static int
648 falcon_poll_edma(reqp)
649 SC_REQ *reqp;
650 {
651 int timeout = 9000; /* XXX */
652
653 /*
654 * Because of the Falcon hardware, it is impossible to reach
655 * the 5380 while doing DMA-transfers. So we have to rely on
656 * the interrupt line to determine if DMA-has finished. the
657 * DMA-controller itself will never fire an interrupt. This means
658 * that 'broken-up' DMA transfers are not (yet) possible on the
659 * Falcon.
660 */
661 for (;;) {
662 delay(20);
663 if (--timeout <= 0) {
664 ncr_tprint(reqp, "Timeout on polled transfer\n");
665 reqp->xs->error = XS_TIMEOUT;
666 return(0);
667 }
668 if (!(MFP->mf_gpip & IO_DINT))
669 break;
670 }
671 return(1);
672 }
673
674 static int
675 falcon_get_dma_result(reqp, bytes_left)
676 SC_REQ *reqp;
677 u_long *bytes_left;
678 {
679 int rv = 0;
680 int st_dmastat;
681 u_long bytes_done;
682
683 /*
684 * Select sector counter register first (See Atari docu.)
685 */
686 DMA->dma_mode = 0x90;
687 if (!(st_dmastat = DMA->dma_stat) & 0x01) {
688 /*
689 * Misc. DMA-error according to Atari...
690 */
691 ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
692 st_dmaaddr_get());
693 reqp->xs->error = XS_DRIVER_STUFFUP;
694 rv = 1;
695 }
696 if (st_dmastat & 0x02) {
697 /*
698 * Bytecount not zero.... As the fifo loads in 16 byte
699 * chunks, check if bytes are stuck in fifo.
700 * As we don't use DMA on chunks less than 512 bytes
701 * on the Falcon, report any residual not a multiple of
702 * 512 as an error...
703 */
704 bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
705 if (bytes_done & 511) {
706 ncr_tprint(reqp, "%ld bytes of %ld stuck in fifo\n",
707 bytes_done & 15, bytes_done & ~511);
708 bytes_done &= ~511;
709 reqp->xs->error = XS_DRIVER_STUFFUP;
710 }
711 *bytes_left = reqp->dm_cur->dm_count - bytes_done;
712 }
713 else {
714 *bytes_left = 0;
715 rv = 1;
716 }
717 return(rv);
718 }
719
720 #endif /* defined(FALCON_SCSI) */
721
722 #if defined(TT_SCSI) && defined(FALCON_SCSI)
723 /*
724 * Define some functions to support _both_ TT and Falcon SCSI
725 */
726
727 /*
728 * The prototypes first...
729 */
730 static void scsi_mach_init __P((struct ncr_softc *));
731 void scsi_ienable __P((void));
732 void scsi_idisable __P((void));
733 void scsi_clr_ipend __P((void));
734 int scsi_ipending __P((void));
735 void scsi_dma_setup __P((SC_REQ *, u_int, u_char));
736 int wrong_dma_range __P((SC_REQ *, struct dma_chain *));
737 int poll_edma __P((SC_REQ *));
738 int get_dma_result __P((SC_REQ *, u_long *));
739
740 /*
741 * Register access will be done through the following 2 function pointers.
742 */
743 static u_char (*get_5380_reg) __P((u_short));
744 static void (*set_5380_reg) __P((u_short, u_short));
745
746 #define GET_5380_REG (*get_5380_reg)
747 #define SET_5380_REG (*set_5380_reg)
748
749 static void
750 scsi_mach_init(sc)
751 struct ncr_softc *sc;
752 {
753 if (machineid & ATARI_FALCON) {
754 get_5380_reg = get_falcon_5380_reg;
755 set_5380_reg = set_falcon_5380_reg;
756 scsi_falcon_init(sc);
757 }
758 else {
759 get_5380_reg = get_tt_5380_reg;
760 set_5380_reg = set_tt_5380_reg;
761 scsi_tt_init(sc);
762 }
763 }
764
765 extern __inline__ void
766 scsi_ienable()
767 {
768 if (machineid & ATARI_FALCON)
769 scsi_falcon_ienable();
770 else scsi_tt_ienable();
771 }
772
773 extern __inline__ void
774 scsi_idisable()
775 {
776 if (machineid & ATARI_FALCON)
777 scsi_falcon_idisable();
778 else scsi_tt_idisable();
779 }
780
781 extern __inline__ void
782 scsi_clr_ipend()
783 {
784 if (machineid & ATARI_FALCON)
785 scsi_falcon_clr_ipend();
786 else scsi_tt_clr_ipend();
787 }
788
789 extern __inline__ int
790 scsi_ipending()
791 {
792 if (machineid & ATARI_FALCON)
793 return(scsi_falcon_ipending());
794 else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
795 }
796
797 extern __inline__ void
798 scsi_dma_setup(reqp, phase, mbase)
799 SC_REQ *reqp;
800 u_int phase;
801 u_char mbase;
802 {
803 if (machineid & ATARI_FALCON)
804 scsi_falcon_dmasetup(reqp, phase, mbase);
805 else scsi_tt_dmasetup(reqp, phase, mbase);
806 }
807
808 extern __inline__ int
809 wrong_dma_range(reqp, dm)
810 SC_REQ *reqp;
811 struct dma_chain *dm;
812 {
813 if (machineid & ATARI_FALCON)
814 return(falcon_wrong_dma_range(reqp, dm));
815 else return(tt_wrong_dma_range(reqp, dm));
816 }
817
818 extern __inline__ int
819 poll_edma(reqp)
820 SC_REQ *reqp;
821 {
822 if (machineid & ATARI_FALCON)
823 return(falcon_poll_edma(reqp));
824 else return(tt_poll_edma(reqp));
825 }
826
827 extern __inline__ int
828 get_dma_result(reqp, bytes_left)
829 SC_REQ *reqp;
830 u_long *bytes_left;
831 {
832 if (machineid & ATARI_FALCON)
833 return(falcon_get_dma_result(reqp, bytes_left));
834 else return(tt_get_dma_result(reqp, bytes_left));
835 }
836
837 /*
838 * Locking stuff. All turns into NOP's on the TT.
839 */
840 #define fair_to_keep_dma() ((machineid & ATARI_FALCON) ? \
841 !st_dmawanted() : 1)
842 #define claimed_dma() ((machineid & ATARI_FALCON) ? \
843 falcon_claimed_dma() : 1)
844 #define reconsider_dma() { \
845 if(machineid & ATARI_FALCON) \
846 falcon_reconsider_dma();\
847 }
848 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
849
850 /**********************************************
851 * Functions present for both TT and Falcon. *
852 **********************************************/
853 /*
854 * Our autoconfig matching function
855 */
856 static int
857 machine_match(struct device *pdp, void *match, void *auxp,
858 struct cfdriver *cd)
859 {
860 struct cfdata *cdp = match;
861
862 if (strcmp(auxp, cd->cd_name))
863 return(0);
864 if (cdp->cf_unit != 0) /* Only one unit */
865 return(0);
866 return(1);
867 }
868
869 /*
870 * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
871 * pool. Allocation here is both contiguous and in the lower 16Mb of
872 * the address space. Thus being DMA-able for all controllers.
873 */
874 static u_char *
875 alloc_bounceb(u_long len)
876 {
877 void *tmp;
878
879 return((u_char *)alloc_stmem(len, &tmp));
880 }
881
882 static void
883 free_bounceb(u_char *bounceb)
884 {
885 free_stmem(bounceb);
886 }
887
888 /*
889 * 5380 interrupt.
890 */
891 void
892 scsi_ctrl(int sr)
893 {
894 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
895 scsi_idisable();
896 if (!BASEPRI(sr))
897 add_sicallback((si_farg)ncr_ctrl_intr,
898 (void *)cur_softc, 0);
899 else {
900 spl1();
901 ncr_ctrl_intr(cur_softc);
902 }
903 }
904 }
905
906 /*
907 * DMA controller interrupt
908 */
909 void
910 scsi_dma(int sr)
911 {
912 SC_REQ *reqp;
913
914 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
915 scsi_idisable();
916 if (!BASEPRI(sr))
917 add_sicallback((si_farg)ncr_dma_intr,
918 (void *)cur_softc, 0);
919 else {
920 spl1();
921 ncr_dma_intr(cur_softc);
922 }
923 }
924 }
925
926 /*
927 * Last but not least... Include the general driver code
928 */
929 #include <atari/dev/ncr5380.c>
930