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