Import 2.3.18pre1
[davej-history.git] / drivers / scsi / mac_scsi.c
blob79009810706d83b3075b80f3a263c0c804441c96
1 /*
2 * mac_scsi.c -- Device dependent functions for the Macintosh NCR5380 SCSI
3 * port (MacII style machines, no DMA).
5 * based on:
6 */
8 /*
9 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
11 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
13 * Loosely based on the work of Robert De Vries' team and added:
14 * - working real DMA
15 * - Falcon support (untested yet!) ++bjoern fixed and now it works
16 * - lots of extensions and bug fixes.
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file COPYING in the main directory of this archive
20 * for more details.
25 #include <linux/config.h>
26 #include <linux/module.h>
28 #define NDEBUG_ABORT 0x800000
29 #define NDEBUG_TAGS 0x1000000
30 #define NDEBUG_MERGING 0x2000000
32 #define NDEBUG (0)
34 #define AUTOSENSE
35 /* MSch: Tested the pseudo-DMA code on Atari for the Mac68k port ... */
36 /* Defining neither PSEUDO_DMA nor REAL_DMA -> PIO transfer, sloooow ! */
37 /*#define REAL_DMA*//* never supported on NCR5380 Macs */
39 * Usage: define PSEUDO_DMA to use hardware-handshaked PIO mode (TBI)
40 * undef PSEUDO_DMA to use pure PIO mode
43 /*#define PSEUDO_DMA*//* currently gives trouble on some Macs */
45 #ifdef PSEUDO_DMA
46 #define EMULATE_PSEUDO_DMA
47 #define DMA_WORKS_RIGHT
48 #define UNSAFE
49 #endif
51 /* Support tagged queuing? (on devices that are able to... :-) */
52 #define SUPPORT_TAGS
53 #define MAX_TAGS 32
55 #include <linux/types.h>
56 #include <linux/stddef.h>
57 #include <linux/ctype.h>
58 #include <linux/delay.h>
59 #include <linux/mm.h>
60 #include <linux/blk.h>
61 #include <linux/sched.h>
62 #include <linux/interrupt.h>
64 #include <asm/setup.h>
65 #include <asm/machw.h>
66 #include <asm/macints.h>
67 #include <asm/macintosh.h>
68 #include <asm/page.h>
69 #include <asm/pgtable.h>
70 #include <asm/irq.h>
71 #include <asm/traps.h>
72 #include <asm/bitops.h>
74 #include"scsi.h"
75 #include"hosts.h"
76 #include"mac_scsi.h"
77 #include"NCR5380.h"
78 #include"constants.h"
79 #include <asm/io.h>
81 #include <linux/stat.h>
83 struct proc_dir_entry proc_scsi_mac = {
84 PROC_SCSI_MAC,8,"mac_5380",
85 S_IFDIR | S_IRUGO | S_IXUGO,2
89 * Define RBV_HACK to run the SCSI driver on RBV Macs; undefine to
90 * try getting better interrupt latency
92 #define RBV_HACK
94 #ifdef RBV_HACK
95 #define ENABLE_IRQ() mac_turnon_irq( IRQ_MAC_SCSI );
96 #define DISABLE_IRQ() mac_turnoff_irq( IRQ_MAC_SCSI );
97 #else
98 #define ENABLE_IRQ() mac_enable_irq( IRQ_MAC_SCSI );
99 #define DISABLE_IRQ() mac_disable_irq( IRQ_MAC_SCSI );
100 #endif
102 #define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
103 (mac_scsi_host->hostdata))->dma_len)
105 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
106 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
107 * need ten times the standard value... */
108 #ifndef CONFIG_MAC_SCSI_TOSHIBA_DELAY
109 #define AFTER_RESET_DELAY (HZ/2)
110 #else
111 #define AFTER_RESET_DELAY (5*HZ/2)
112 #endif
114 /***************************** Prototypes *****************************/
116 #ifdef REAL_DMA
117 static intscsi_dma_is_ignored_buserr(unsigned char dma_stat );
118 static voidmac_scsi_fetch_restbytes(void);
119 static longmac_scsi_dma_residual(struct Scsi_Host *instance );
120 static intmac_classify_cmd( Scsi_Cmnd *cmd );
121 static unsigned longmac_dma_xfer_len(unsigned long wanted_len,
122 Scsi_Cmnd *cmd,int write_flag );
123 #endif
124 #ifdef PSEUDO_DMA
125 static intmac_pdma_read(struct Scsi_Host *instance,unsigned char*dst,
126 int len);
127 static intmac_pdma_write(struct Scsi_Host *instance,unsigned char*src,
128 int len);
129 static unsigned longmac_dma_xfer_len(unsigned long wanted_len,
130 Scsi_Cmnd *cmd,int write_flag );
131 #endif
132 static voidscsi_mac_intr(int irq,void*dummy,struct pt_regs *fp);
133 static voidmac_scsi_reset_boot(void);
134 static unsigned charmac_scsi_reg_read(unsigned char reg );
135 static voidmac_scsi_reg_write(unsigned char reg,unsigned char value);
137 /************************* End of Prototypes **************************/
140 static struct Scsi_Host *mac_scsi_host = NULL;
141 #if 0
142 static unsigned char(*mac_scsi_reg_read)(unsigned char reg );
143 static void(*mac_scsi_reg_write)(unsigned char reg,unsigned char value );
144 #endif
146 #ifdef REAL_DMA
147 static unsigned long mac_dma_residual, mac_dma_startaddr;
148 static short mac_dma_active;
149 /* pointer to the dribble buffer */
150 static char*mac_dma_buffer = NULL;
151 /* precalculated physical address of the dribble buffer */
152 static unsigned long mac_dma_phys_buffer;
153 /* != 0 tells the int handler to copy data from the dribble buffer */
154 static char*mac_dma_orig_addr;
155 /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
156 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
157 * cases where requests to physical contiguous buffers have been merged, this
158 * request is <= 4k (one page). So I don't think we have to split transfers
159 * just due to this buffer size...
161 #define MAC_BUFFER_SIZE (4096)
162 #if 1/* FIXME: is that an issue for Macs?? */
163 /* mask for address bits that can't be used with the ST-DMA */
164 static unsigned long mac_dma_stram_mask;
165 #define STRAM_ADDR(a) (((a) & mac_dma_stram_mask) == 0)
166 #endif
167 /* number of bytes to cut from a transfer to handle NCR overruns */
168 static int mac_read_overruns =0;
169 #endif
170 #ifdef PSEUDO_DMA
171 static unsigned long mac_pdma_residual, mac_pdma_startaddr, mac_pdma_current;
172 static short mac_pdma_active;
173 /* FIXME: is that an issue for Macs?? */
174 /* mask for address bits that can't be used with the ST-DMA */
175 static unsigned long mac_dma_stram_mask;
176 #define STRAM_ADDR(a) (((a) & mac_dma_stram_mask) == 0)
177 static int mac_read_overruns =0;
178 #endif
179 static int setup_can_queue = -1;
180 static int setup_cmd_per_lun = -1;
181 static int setup_sg_tablesize = -1;
182 #ifdef SUPPORT_TAGS
183 static int setup_use_tagged_queuing = -1;
184 #endif
185 static int setup_hostid = -1;
188 #if defined(REAL_DMA)
190 #if 0/* FIXME */
191 static intscsi_dma_is_ignored_buserr(unsigned char dma_stat )
193 int i;
194 unsigned long addr =SCSI_DMA_READ_P( dma_addr ), end_addr;
196 if(dma_stat &0x01) {
198 /* A bus error happens when DMA-ing from the last page of a
199 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
200 * Check for this case:
203 for( i =0; i < boot_info.num_memory; ++i ) {
204 end_addr = boot_info.memory[i].addr +
205 boot_info.memory[i].size;
206 if(end_addr <= addr && addr <= end_addr +4)
207 return(1);
210 return(0);
214 /* Dead code... wasn't called anyway :-) and causes some trouble, because at
215 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
216 * to clear the DMA int pending bit before it allows other level 6 interrupts.
218 static voidscsi_dma_buserr(int irq,void*dummy,struct pt_regs *fp)
220 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
222 /* Don't do anything if a NCR interrupt is pending. Probably it's just
223 * masked... */
224 if(mac_irq_pending( IRQ_MAC_SCSI ))
225 return;
227 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
228 SCSI_DMA_READ_P(dma_addr), dma_stat,SCSI_DMA_READ_P(dma_cnt));
229 if(dma_stat &0x80) {
230 if(!scsi_dma_is_ignored_buserr( dma_stat ))
231 printk("SCSI DMA bus error -- bad DMA programming!\n");
233 else{
234 /* Under normal circumstances we never should get to this point,
235 * since both interrupts are triggered simultaneously and the 5380
236 * int has higher priority. When this irq is handled, that DMA
237 * interrupt is cleared. So a warning message is printed here.
239 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
242 #endif
244 #endif
246 voidrestore_irq(struct pt_regs *regs)
248 unsigned long flags;
250 save_flags(flags);
251 flags = (flags & ~0x0700) | (regs->sr &0x0700);
252 restore_flags(flags);
255 static int polled_scsi_on =0;
256 static unsigned char*mac_scsi_regp = NULL;
258 voidscsi_mac_polled(void)
260 if(polled_scsi_on)
262 if(NCR5380_read(BUS_AND_STATUS_REG)&BASR_IRQ)
264 printk("SCSI poll\n");
265 scsi_mac_intr(IRQ_MAC_SCSI, NULL, NULL);
270 static voidscsi_mac_intr(int irq,void*dummy,struct pt_regs *fp)
272 unsigned long flags;
273 #ifdef REAL_DMA
274 int dma_stat;
276 dma_stat = mac_scsi_dma.dma_ctrl;
278 INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n",
279 mac_scsi_host->host_no, dma_stat &0xff);
281 /* Look if it was the DMA that has interrupted: First possibility
282 * is that a bus error occurred...
284 if(dma_stat &0x80) {
285 if(!scsi_dma_is_ignored_buserr( dma_stat )) {
286 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
287 SCSI_DMA_READ_P(dma_addr));
288 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
292 /* If the DMA is active but not finished, we have the the case
293 * that some other 5380 interrupt occurred within the DMA transfer.
294 * This means we have residual bytes, if the desired end address
295 * is not yet reached. Maybe we have to fetch some bytes from the
296 * rest data register, too. The residual must be calculated from
297 * the address pointer, not the counter register, because only the
298 * addr reg counts bytes not yet written and pending in the rest
299 * data reg!
301 if((dma_stat &0x02) && !(dma_stat &0x40)) {
302 mac_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P( dma_addr ) -
303 mac_dma_startaddr);
305 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
306 mac_dma_residual);
308 if((signed int)mac_dma_residual <0)
309 mac_dma_residual =0;
310 if((dma_stat &1) ==0) {
311 /* After read operations, we maybe have to
312 transport some rest bytes */
313 mac_scsi_fetch_restbytes();
315 else{
316 /* There seems to be a nasty bug in some SCSI-DMA/NCR
317 combinations: If a target disconnects while a write
318 operation is going on, the address register of the
319 DMA may be a few bytes farer than it actually read.
320 This is probably due to DMA prefetching and a delay
321 between DMA and NCR. Experiments showed that the
322 dma_addr is 9 bytes to high, but this could vary.
323 The problem is, that the residual is thus calculated
324 wrong and the next transfer will start behind where
325 it should. So we round up the residual to the next
326 multiple of a sector size, if it isn't already a
327 multiple and the originally expected transfer size
328 was. The latter condition is there to ensure that
329 the correction is taken only for "real" data
330 transfers and not for, e.g., the parameters of some
331 other command. These shouldn't disconnect anyway.
333 if(mac_dma_residual &0x1ff) {
334 DMA_PRINTK("SCSI DMA: DMA bug corrected, "
335 "difference %ld bytes\n",
336 512- (mac_dma_residual &0x1ff));
337 mac_dma_residual = (mac_dma_residual +511) & ~0x1ff;
340 mac_scsi_dma.dma_ctrl =0;
343 /* If the DMA is finished, fetch the rest bytes and turn it off */
344 if(dma_stat &0x40) {
345 atari_dma_residual =0;
346 if((dma_stat &1) ==0)
347 atari_scsi_fetch_restbytes();
348 tt_scsi_dma.dma_ctrl =0;
351 #endif/* REAL_DMA */
353 #ifdef PSEUDO_DMA
354 /* determine if there is any residual for the current transfer */
355 if(mac_pdma_active) {
356 /* probably EOP interrupt, signaling i.e. target disconnect.
357 * We must figure out the residual from the source/destination
358 * pointers here ... */
359 /* Should check bus status here to make sure it wasn't reselect or reset */
360 mac_pdma_residual = HOSTDATA_DMALEN - (mac_pdma_current - mac_pdma_startaddr);
361 mac_pdma_active =0;
363 #endif
365 #ifdef RBV_HACK
366 mac_turnoff_irq( IRQ_MAC_SCSI );
367 #else
368 mac_disable_irq( IRQ_MAC_SCSI );
369 #endif
371 save_flags(flags);
372 #ifndef RBV_HACK/* interferes with level triggered RBV IRQs ?? */
373 restore_irq(fp);
374 #endif
376 if( irq ==IRQ_IDX(IRQ_MAC_SCSI) )
377 NCR5380_intr(irq, dummy, fp);
379 restore_flags(flags);
381 /* To be sure the int is not masked */
382 #ifdef RBV_HACK
383 mac_turnon_irq( IRQ_MAC_SCSI );
384 #else
385 mac_enable_irq( IRQ_MAC_SCSI );
386 #endif
388 /* Clear the IRQ */
389 via_scsi_clear();
393 #ifdef REAL_DMA
394 static voidmac_scsi_fetch_restbytes(void)
396 int nr;
397 char*src, *dst;
399 /* fetch rest bytes in the DMA register */
400 dst = (char*)SCSI_DMA_READ_P( dma_addr );
401 if((nr = ((long)dst &3))) {
402 /* there are 'nr' bytes left for the last long address before the
403 DMA pointer */
404 dst = (char*)( (unsigned long)dst & ~3);
405 DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
406 nr, (long)dst);
407 dst = (char*)PTOV(dst);/* The content of the DMA pointer
408 * is a physical address! */
409 DMA_PRINTK(" = virt addr 0x%08lx\n", (long)dst);
410 for( src = (char*)&mac_scsi_dma.dma_restdata; nr >0; --nr )
411 *dst++ = *src++;
414 #endif/* REAL_DMA */
416 #if 0/* FIXME : how is the host ID determined on a Mac? */
417 #define RTC_READ(reg) \
418 ({ unsigned char __val; \
419 outb(reg,&tt_rtc.regsel); \
420 __val = tt_rtc.data; \
421 __val; \
424 #define RTC_WRITE(reg,val) \
425 do { \
426 outb(reg,&tt_rtc.regsel); \
427 tt_rtc.data = (val); \
428 } while(0)
429 #endif
431 intmac_scsi_detect(Scsi_Host_Template *host)
433 static int called =0;
434 struct Scsi_Host *instance;
436 if(!MACH_IS_MAC || called)
437 return(0);
439 if(macintosh_config->scsi_type != MAC_SCSI_OLD)
440 return(0);
442 host->proc_dir = &proc_scsi_mac;
444 /* testing: IIfx SCSI without IOP ?? */
445 if(macintosh_config->ident == MAC_MODEL_IIFX)
446 mac_scsi_regp = via1_regp+0x8000;
447 else
448 mac_scsi_regp = via1_regp+0x10000;
450 #if 0/* maybe if different SCSI versions show up ? */
451 mac_scsi_reg_read =IS_A_TT() ? atari_scsi_tt_reg_read :
452 atari_scsi_falcon_reg_read;
453 mac_scsi_reg_write =IS_A_TT() ? atari_scsi_tt_reg_write :
454 #endif atari_scsi_falcon_reg_write;
456 /* setup variables */
457 host->can_queue =
458 (setup_can_queue >0) ? setup_can_queue :
459 MAC_SCSI_CAN_QUEUE;
460 host->cmd_per_lun =
461 (setup_cmd_per_lun >0) ? setup_cmd_per_lun :
462 MAC_SCSI_CMD_PER_LUN;
463 host->sg_tablesize =
464 (setup_sg_tablesize >=0) ? setup_sg_tablesize : MAC_SCSI_SG_TABLESIZE;
466 if(setup_hostid >=0)
467 host->this_id = setup_hostid;
468 else{
469 /* use 7 as default */
470 host->this_id =7;
473 #ifdef SUPPORT_TAGS
474 if(setup_use_tagged_queuing <0)
475 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
476 #endif
478 instance =scsi_register(host,sizeof(struct NCR5380_hostdata));
479 mac_scsi_host = instance;
481 /* truncation of machspec bit not critical as instance->irq never used */
482 #if 0/* Might work; problem was only with Falcon lock */
483 instance->irq = IRQ_MAC_SCSI;
484 #else
485 instance->irq =0;
486 #endif
487 mac_scsi_reset_boot();
488 NCR5380_init(instance,0);
490 /* This int is actually "pseudo-slow", i.e. it acts like a slow
491 * interrupt after having cleared the pending flag for the DMA
492 * interrupt. */
493 request_irq(IRQ_MAC_SCSI, scsi_mac_intr, IRQ_TYPE_SLOW,
494 "SCSI NCR5380", scsi_mac_intr);
495 #ifdef REAL_DMA
496 tt_scsi_dma.dma_ctrl =0;
497 atari_dma_residual =0;
499 if(is_brokenscsi) {
500 /* While the read overruns (described by Drew Eckhardt in
501 * NCR5380.c) never happened on TTs, they do in fact on the Medusa
502 * (This was the cause why SCSI didn't work right for so long
503 * there.) Since handling the overruns slows down a bit, I turned
504 * the #ifdef's into a runtime condition.
506 * In principle it should be sufficient to do max. 1 byte with
507 * PIO, but there is another problem on the Medusa with the DMA
508 * rest data register. So 'atari_read_overruns' is currently set
509 * to 4 to avoid having transfers that aren't a multiple of 4. If
510 * the rest data bug is fixed, this can be lowered to 1.
512 mac_read_overruns =4;
514 #endif/* REAL_DMA */
516 #ifdef PSEUDO_DMA
517 mac_pdma_residual =0;
518 mac_pdma_active =0;
519 #endif
521 printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
522 #ifdef SUPPORT_TAGS
523 "TAGGED-QUEUING=%s "
524 #endif
525 "HOSTID=%d",
526 instance->host_no, instance->hostt->can_queue,
527 instance->hostt->cmd_per_lun,
528 instance->hostt->sg_tablesize,
529 #ifdef SUPPORT_TAGS
530 setup_use_tagged_queuing ?"yes":"no",
531 #endif
532 instance->hostt->this_id );
533 NCR5380_print_options(instance);
534 printk("\n");
536 called =1;
537 return(1);
540 #ifdef MODULE
541 intmac_scsi_release(struct Scsi_Host *sh)
543 free_irq(IRQ_MAC_SCSI, scsi_mac_intr);
544 if(mac_dma_buffer)
545 scsi_init_free(mac_dma_buffer, MAC_BUFFER_SIZE);
546 return1;
548 #endif
550 voidmac_scsi_setup(char*str,int*ints )
552 /* Format of mac5380 parameter is:
553 * mac5380=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
554 * Negative values mean don't change.
557 /* Grmbl... the standard parameter parsing can't handle negative numbers
558 * :-( So let's do it ourselves!
561 int i = ints[0]+1, fact;
563 while( str && (isdigit(*str) || *str =='-') && i <=10) {
564 if(*str =='-')
565 fact = -1, ++str;
566 else
567 fact =1;
568 ints[i++] =simple_strtoul( str, NULL,0) * fact;
569 if((str =strchr( str,',')) != NULL)
570 ++str;
572 ints[0] = i-1;
574 if(ints[0] <1) {
575 printk("mac_scsi_setup: no arguments!\n");
576 return;
579 if(ints[0] >=1) {
580 if(ints[1] >0)
581 /* no limits on this, just > 0 */
582 setup_can_queue = ints[1];
584 if(ints[0] >=2) {
585 if(ints[2] >0)
586 setup_cmd_per_lun = ints[2];
588 if(ints[0] >=3) {
589 if(ints[3] >=0) {
590 setup_sg_tablesize = ints[3];
591 /* Must be <= SG_ALL (255) */
592 if(setup_sg_tablesize > SG_ALL)
593 setup_sg_tablesize = SG_ALL;
596 if(ints[0] >=4) {
597 /* Must be between 0 and 7 */
598 if(ints[4] >=0&& ints[4] <=7)
599 setup_hostid = ints[4];
600 else if(ints[4] >7)
601 printk("mac_scsi_setup: invalid host ID %d !\n", ints[4] );
603 #ifdef SUPPORT_TAGS
604 if(ints[0] >=5) {
605 if(ints[5] >=0)
606 setup_use_tagged_queuing = !!ints[5];
608 #endif
611 intmac_scsi_reset( Scsi_Cmnd *cmd,unsigned int reset_flags)
613 int rv;
614 struct NCR5380_hostdata *hostdata =
615 (struct NCR5380_hostdata *)cmd->host->hostdata;
617 /* For doing the reset, SCSI interrupts must be disabled first,
618 * since the 5380 raises its IRQ line while _RST is active and we
619 * can't disable interrupts completely, since we need the timer.
621 /* And abort a maybe active DMA transfer */
622 mac_turnoff_irq( IRQ_MAC_SCSI );
623 #ifdef REAL_DMA
624 mac_scsi_dma.dma_ctrl =0;
625 #endif/* REAL_DMA */
626 #ifdef PSEUDO_DMA
627 mac_pdma_active =0;
628 #endif
630 rv =NCR5380_reset(cmd, reset_flags);
632 /* Re-enable ints */
633 mac_turnon_irq( IRQ_MAC_SCSI );
635 return( rv );
639 static voidmac_scsi_reset_boot(void)
641 unsigned long end;
644 * Do a SCSI reset to clean up the bus during initialization. No messing
645 * with the queues, interrupts, or locks necessary here.
648 printk("Macintosh SCSI: resetting the SCSI bus...");
650 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
651 mac_turnoff_irq( IRQ_MAC_SCSI );
653 /* get in phase */
654 NCR5380_write( TARGET_COMMAND_REG,
655 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) ));
657 /* assert RST */
658 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
659 /* The min. reset hold time is 25us, so 40us should be enough */
660 udelay(50);
661 /* reset RST and interrupt */
662 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
663 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
665 for( end = jiffies + AFTER_RESET_DELAY;time_before(jiffies, end); )
666 barrier();
668 /* switch on SCSI IRQ again */
669 mac_turnon_irq( IRQ_MAC_SCSI );
671 printk(" done\n");
675 const char*mac_scsi_info(struct Scsi_Host *host)
677 /* mac_scsi_detect() is verbose enough... */
678 static const char string[] ="Macintosh NCR5380 SCSI";
679 return string;
683 #if defined(REAL_DMA)
685 unsigned longmac_scsi_dma_setup(struct Scsi_Host *instance,void*data,
686 unsigned long count,int dir )
688 unsigned long addr =virt_to_phys( data );
690 DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
691 "dir = %d\n", instance->host_no, data, addr, count, dir);
693 if(!STRAM_ADDR(addr)) {
694 /* If we have a non-DMAable address on a Falcon, use the dribble
695 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
696 * handler to copy data from the dribble buffer to the originally
697 * wanted address.
699 if(dir)
700 memcpy( mac_dma_buffer, data, count );
701 else
702 mac_dma_orig_addr = data;
703 addr = mac_dma_phys_buffer;
706 mac_dma_startaddr = addr;/* Needed for calculating residual later. */
708 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
709 * it to the peripheral. (Must be done before DMA setup, since at least
710 * the ST-DMA begins to fill internal buffers right after setup. For
711 * reads, invalidate any cache, may be altered after DMA without CPU
712 * knowledge.
714 * ++roman: For the Medusa, there's no need at all for that cache stuff,
715 * because the hardware does bus snooping (fine!).
717 dma_cache_maintenance( addr, count, dir );
719 if(count ==0)
720 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
722 mac_scsi_dma.dma_ctrl = dir;
723 SCSI_DMA_WRITE_P( dma_addr, addr );
724 SCSI_DMA_WRITE_P( dma_cnt, count );
725 mac_scsi_dma.dma_ctrl = dir |2;
727 return( count );
731 static longmac_scsi_dma_residual(struct Scsi_Host *instance )
733 return( mac_dma_residual );
737 #define CMD_SURELY_BLOCK_MODE 0
738 #define CMD_SURELY_BYTE_MODE 1
739 #define CMD_MODE_UNKNOWN 2
741 static intmac_classify_cmd( Scsi_Cmnd *cmd )
743 unsigned char opcode = cmd->cmnd[0];
745 if(opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
746 opcode == READ_BUFFER)
747 return( CMD_SURELY_BYTE_MODE );
748 else if(opcode == READ_6 || opcode == READ_10 ||
749 opcode ==0xa8/* READ_12 */|| opcode == READ_REVERSE ||
750 opcode == RECOVER_BUFFERED_DATA) {
751 /* In case of a sequential-access target (tape), special care is
752 * needed here: The transfer is block-mode only if the 'fixed' bit is
753 * set! */
754 if(cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] &1))
755 return( CMD_SURELY_BYTE_MODE );
756 else
757 return( CMD_SURELY_BLOCK_MODE );
759 else
760 return( CMD_MODE_UNKNOWN );
763 #endif/* REAL_DMA */
765 #if defined(REAL_DMA) || defined(PSEUDO_DMA)
766 /* This function calculates the number of bytes that can be transferred via
767 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
768 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
769 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
770 * possible on the Falcon, since that would require to program the DMA for
771 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
772 * the overrun problem, so this question is academic :-)
775 static unsigned longmac_dma_xfer_len(unsigned long wanted_len,
776 Scsi_Cmnd *cmd,
777 int write_flag )
779 unsigned long possible_len, limit;
781 #if defined(REAL_DMA)
782 if(IS_A_TT())
783 /* TT SCSI DMA can transfer arbitrary #bytes */
784 return( wanted_len );
786 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
787 * 255*512 bytes, but this should be enough)
789 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
790 * that return a number of bytes which cannot be known beforehand. In this
791 * case, the given transfer length is an "allocation length". Now it
792 * can happen that this allocation length is a multiple of 512 bytes and
793 * the DMA is used. But if not n*512 bytes really arrive, some input data
794 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
795 * between commands that do block transfers and those that do byte
796 * transfers. But this isn't easy... there are lots of vendor specific
797 * commands, and the user can issue any command via the
798 * SCSI_IOCTL_SEND_COMMAND.
800 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
801 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
802 * and 3), the thing to do is obvious: allow any number of blocks via DMA
803 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
804 * the transfer (allocation) length is < 1024, hoping that no cmd. not
805 * explicitly known as byte mode have such big allocation lengths...
806 * BTW, all the discussion above applies only to reads. DMA writes are
807 * unproblematic anyways, since the targets aborts the transfer after
808 * receiving a sufficient number of bytes.
810 * Another point: If the transfer is from/to an non-ST-RAM address, we
811 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
814 if(write_flag) {
815 /* Write operation can always use the DMA, but the transfer size must
816 * be rounded up to the next multiple of 512 (atari_dma_setup() does
817 * this).
819 possible_len = wanted_len;
821 else{
822 /* Read operations: if the wanted transfer length is not a multiple of
823 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
824 * (no interrupt on DMA finished!)
826 if(wanted_len &0x1ff)
827 possible_len =0;
828 else{
829 /* Now classify the command (see above) and decide whether it is
830 * allowed to do DMA at all */
831 switch(falcon_classify_cmd( cmd )) {
832 case CMD_SURELY_BLOCK_MODE:
833 possible_len = wanted_len;
834 break;
835 case CMD_SURELY_BYTE_MODE:
836 possible_len =0;/* DMA prohibited */
837 break;
838 case CMD_MODE_UNKNOWN:
839 default:
840 /* For unknown commands assume block transfers if the transfer
841 * size/allocation length is >= 1024 */
842 possible_len = (wanted_len <1024) ?0: wanted_len;
843 break;
848 /* Last step: apply the hard limit on DMA transfers */
849 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr) )) ?
850 STRAM_BUFFER_SIZE :255*512;
851 if(possible_len > limit)
852 possible_len = limit;
854 if(possible_len != wanted_len)
855 DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes "
856 "instead of %ld\n", possible_len, wanted_len);
858 #else/* REAL_DMA */
859 possible_len = wanted_len;
860 #endif
862 return( possible_len );
864 #endif/* REAL_DMA || PSEUDO_DMA */
868 * FIXME !!!
871 /* NCR5380 register access functions
874 static unsigned charmac_scsi_reg_read(unsigned char reg )
876 return( mac_scsi_regp[reg <<4] );
879 static voidmac_scsi_reg_write(unsigned char reg,unsigned char value )
881 mac_scsi_regp[reg <<4] = value;
884 #include"mac_NCR5380.c"
886 #ifdef PSEUDO_DMA
889 * slightly optimized PIO transfer routines, experimental!
890 * command may time out if interrupts are left enabled
893 staticinlineintmac_pdma_read(struct Scsi_Host *instance,unsigned char*dst,int len)
895 registerunsigned char*d = dst;
896 register i = len;
897 registerunsigned char p, tmp;
899 #if (NDEBUG & NDEBUG_PSEUDO_DMA)
900 printk("pdma_read: reading %d bytes to %p\n", len, dst);
901 #endif
903 mac_pdma_residual = len;
904 if(mac_pdma_active) {
905 printk("pseudo-DMA already active in pread!\n");
906 return-1;
908 mac_pdma_active =1;
909 mac_pdma_startaddr = (unsigned long) dst;
910 mac_pdma_current = (unsigned long) dst;
913 * Get the phase from the bus (sanity check)
914 * Hopefully, the phase bits are valid here ...
916 p =NCR5380_read(STATUS_REG) & PHASE_MASK;
917 if(!(p & SR_IO)) {
918 PDMA_PRINTK("NCR5380_pread: initial phase mismatch!\n");
919 NCR_PRINT_PHASE(NDEBUG_ANY);
920 return-1;
924 * The NCR5380 chip will only drive the SCSI bus when the
925 * phase specified in the appropriate bits of the TARGET COMMAND
926 * REGISTER match the STATUS REGISTER
929 #if 0/* done in transfer_dma */
930 p = PHASE_DATAIN;
931 NCR5380_write(TARGET_COMMAND_REG,PHASE_SR_TO_TCR(p));
932 #endif
934 for(; i; --i) {
935 HSH_PRINTK(" read %d ..", i);
936 /* check if we were interrupted ... */
937 if(!mac_pdma_active) {
938 printk("pwrite: interrupt detected!\n");
939 break;
943 * Wait for assertion of REQ, after which the phase bits will be
944 * valid
946 while(!((tmp =NCR5380_read(STATUS_REG)) & SR_REQ))
947 barrier();
949 HSH_PRINTK(" REQ ..");
951 /* Check for phase mismatch */
952 if((tmp & PHASE_MASK) != p) {
953 if(!mac_pdma_active)
954 printk("scsi%d : phase mismatch after interrupt\n", instance->host_no);
955 else
956 printk("scsi%d : phase mismatch w/o interrupt\n", instance->host_no);
957 NCR_PRINT_PHASE(NDEBUG_ANY);
958 break;
961 /* Do actual transfer from SCSI bus to memory */
962 *d =NCR5380_read(CURRENT_SCSI_DATA_REG);
964 d++;
966 /* Handshake ... */
968 /* Assert ACK */
969 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
970 HSH_PRINTK(" ACK ..");
972 /* Wait for REQ to be dropped */
973 while(NCR5380_read(STATUS_REG) & SR_REQ)
974 barrier();
975 HSH_PRINTK(" /REQ ..");
977 /* Drop ACK */
978 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
979 HSH_PRINTK(" /ACK !\n");
981 mac_pdma_current = (unsigned long) d;
982 mac_pdma_residual--;
985 #if (NDEBUG & NDEBUG_PSEUDO_DMA)
986 printk("pdma_read: read at %d bytes to %p\n", i, dst);
987 #endif
989 if(mac_pdma_residual)
990 printk("pread: leaving with residual %ld of %ld\n",
991 mac_pdma_residual, len);
992 mac_pdma_active =0;
994 /* ?? */
995 #if 0
996 NCR5380_write(MODE_REG, MR_BASE);
997 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
998 #endif
999 return0;
1002 staticinlineintmac_pdma_write(struct Scsi_Host *instance,unsigned char*src,int len)
1004 registerunsigned char*s = src;
1005 register i = len;
1006 registerunsigned char p, tmp;
1008 #if (NDEBUG & NDEBUG_PSEUDO_DMA)
1009 printk("pdma_write: writing %d bytes from %p\n", len, src);
1010 #endif
1012 mac_pdma_residual = len;
1013 if(mac_pdma_active) {
1014 printk("pseudo-DMA already active in pwrite!\n");
1015 return-1;
1017 mac_pdma_active =1;
1018 mac_pdma_startaddr = (unsigned long) src;
1019 mac_pdma_current = (unsigned long) src;
1022 * Get the phase from the bus (sanity check)
1024 p =NCR5380_read(STATUS_REG) & PHASE_MASK;
1025 if(p & SR_IO) {
1026 printk("NCR5380_pwrite: initial phase mismatch!\n");
1027 NCR_PRINT_PHASE(NDEBUG_ANY);
1028 return-1;
1032 * The NCR5380 chip will only drive the SCSI bus when the
1033 * phase specified in the appropriate bits of the TARGET COMMAND
1034 * REGISTER match the STATUS REGISTER
1037 #if 0/* already done in transfer_dma */
1038 p = PHASE_DATAOUT;
1039 NCR5380_write(TARGET_COMMAND_REG,PHASE_SR_TO_TCR(p));
1040 #endif
1042 for(; i; --i) {
1043 /* check if we were interrupted ... */
1044 if(!mac_pdma_active) {
1045 printk("pwrite: interrupt detected!\n");
1046 break;
1050 * Wait for assertion of REQ, after which the phase bits will be
1051 * valid
1053 while(!((tmp =NCR5380_read(STATUS_REG)) & SR_REQ));
1055 /* Check for phase mismatch */
1056 if((tmp & PHASE_MASK) != p) {
1057 if(!mac_pdma_active)
1058 printk("scsi%d : phase mismatch after interrupt\n", instance->host_no);
1059 else
1060 printk("scsi%d : phase mismatch w/o interrupt\n", instance->host_no);
1061 NCR_PRINT_PHASE(NDEBUG_ANY);
1062 /* should we signal an error here?? */
1063 break;
1066 /* Do actual transfer to SCSI bus from memory */
1068 NCR5380_write(OUTPUT_DATA_REG, *s);
1070 s++;
1072 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1073 ICR_ASSERT_DATA);
1075 /* Handshake ... assert ACK */
1076 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
1077 ICR_ASSERT_DATA | ICR_ASSERT_ACK);
1079 /* ... wait for REQ to be dropped */
1080 while(NCR5380_read(STATUS_REG) & SR_REQ);
1082 /* and drop ACK (and DATA) ! */
1083 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
1085 mac_pdma_current = (unsigned long) s;
1086 mac_pdma_residual--;
1089 #if (NDEBUG & NDEBUG_PSEUDO_DMA)
1090 printk("pdma_write: write at %d bytes from %p\n", i, src);
1091 #endif
1093 if(mac_pdma_residual)
1094 printk("pwrite: leaving with residual %ld of len %ld\n",
1095 mac_pdma_residual, len);
1096 mac_pdma_active =0;
1098 return0;
1101 #endif/* PSEUDO_DMA */
1103 #ifdef MODULE
1104 Scsi_Host_Template driver_template = MAC_SCSI;
1106 #include"scsi_module.c"
1107 #endif
close