2 * 68k mac 53c9[46] scsi driver
4 * copyright (c) 1998, David Weis weisd3458@uni.edu
6 * debugging on Quadra 800 and 660AV Michael Schmitz, Dave Kilzer 7/98
8 * based loosely on cyber_esp.c
11 /* these are unused for now */
12 #define myreadl(addr) (*(volatile unsigned int *) (addr))
13 #define mywritel(b, addr) ((*(volatile unsigned int *) (addr)) = (b))
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/types.h>
19 #include <linux/ctype.h>
20 #include <linux/string.h>
21 #include <linux/malloc.h>
22 #include <linux/blk.h>
23 #include <linux/proc_fs.h>
24 #include <linux/stat.h>
31 #include "../../arch/m68k/mac/via6522.h" /* huh? */
35 #include <asm/setup.h>
37 #include <asm/macints.h>
38 #include <asm/machw.h>
40 #include <asm/pgtable.h>
42 #include <asm/macintosh.h>
44 extern inline void esp_handle(struct NCR_ESP
*esp
);
45 extern void mac_esp_intr(int irq
, void *dev_id
, struct pt_regs
*pregs
);
47 static int dma_bytes_sent(struct NCR_ESP
* esp
, int fifo_count
);
48 static int dma_can_transfer(struct NCR_ESP
* esp
, Scsi_Cmnd
*sp
);
49 static void dma_dump_state(struct NCR_ESP
* esp
);
50 static void dma_init_read(struct NCR_ESP
* esp
, char * vaddress
, int length
);
51 static void dma_init_write(struct NCR_ESP
* esp
, char * vaddress
, int length
);
52 static void dma_ints_off(struct NCR_ESP
* esp
);
53 static void dma_ints_on(struct NCR_ESP
* esp
);
54 static int dma_irq_p(struct NCR_ESP
* esp
);
55 static int dma_irq_p_quick(struct NCR_ESP
* esp
);
56 static void dma_led_off(struct NCR_ESP
* esp
);
57 static void dma_led_on(struct NCR_ESP
*esp
);
58 static int dma_ports_p(struct NCR_ESP
*esp
);
59 static void dma_setup(struct NCR_ESP
* esp
, __u32 addr
, int count
, int write
);
60 static void dma_setup_quick(struct NCR_ESP
* esp
, __u32 addr
, int count
, int write
);
62 static int esp_dafb_dma_irq_p(struct NCR_ESP
* espdev
);
63 static int esp_iosb_dma_irq_p(struct NCR_ESP
* espdev
);
65 volatile unsigned char cmd_buffer
[16];
66 /* This is where all commands are put
67 * before they are transfered to the ESP chip
71 static int esp_initialized
= 0;
73 static int setup_num_esps
= -1;
74 static int setup_disconnect
= -1;
75 static int setup_nosync
= -1;
76 static int setup_can_queue
= -1;
77 static int setup_cmd_per_lun
= -1;
78 static int setup_sg_tablesize
= -1;
80 static int setup_use_tagged_queuing
= -1;
82 static int setup_hostid
= -1;
85 * Experimental ESP inthandler; check macints.c to make sure dev_id is
89 void mac_esp_intr(int irq
, void *dev_id
, struct pt_regs
*pregs
)
91 struct NCR_ESP
*esp
= (struct NCR_ESP
*) dev_id
;
94 /* Handle the one ESP interrupt showing at this IRQ level. */
95 if(((esp
)->irq
& 0xff) == irq
) {
99 irq_p
= esp
->dma_irq_p(esp
);
100 printk("mac_esp: irq_p %x current %p disconnected %p\n",
101 irq_p
, esp
->current_SC
, esp
->disconnected_SC
);
104 * Mac: if we're here, it's an ESP interrupt for sure!
106 if((esp
->current_SC
|| esp
->disconnected_SC
)) {
107 esp
->dma_ints_off(esp
);
109 ESPIRQ(("I%d(", esp
->esp_id
));
113 esp
->dma_ints_on(esp
);
119 * Debug hooks; use for playing with the interrupt flag testing and interrupt
120 * acknowledge on the various machines
123 void scsi_esp_polled(int irq
, void *dev_id
, struct pt_regs
*pregs
)
125 if (esp_initialized
== 0)
128 mac_esp_intr(irq
, dev_id
, pregs
);
131 void fake_intr(int irq
, void *dev_id
, struct pt_regs
*pregs
)
134 printk("mac_esp: got irq\n");
137 mac_esp_intr(irq
, dev_id
, pregs
);
140 void fake_drq(int irq
, void *dev_id
, struct pt_regs
*pregs
)
142 printk("mac_esp: got drq\n");
148 * Function : mac_scsi_setup(char *str, int *ints)
150 * Purpose : booter command line initialization of the overrides array,
152 * Inputs : str - unused, ints - array of integer parameters with ints[0]
153 * equal to the number of ints.
155 * Currently unused in the new driver; need to add settable parameters to the
160 void mac_esp_setup(char *str
, int *ints
) {
162 /* Format of mac53c9x parameter is:
163 * mac53c9x=<num_esps>,<disconnect>,<nosync>,<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
164 * Negative values mean don't change.
167 /* Grmbl... the standard parameter parsing can't handle negative numbers
168 * :-( So let's do it ourselves!
171 int i
= ints
[0]+1, fact
;
173 while( str
&& (isdigit(*str
) || *str
== '-') && i
<= 10) {
178 ints
[i
++] = simple_strtoul( str
, NULL
, 0 ) * fact
;
179 if ((str
= strchr( str
, ',' )) != NULL
)
185 printk( "mac_esp_setup: no arguments!\n" );
191 /* no limits on this, just > 0 */
192 if (ints
[1] >= 0 && ints
[1] <= 2)
193 setup_num_esps
= ints
[1];
194 else if (ints
[1] > 2)
195 printk( "mac_esp_setup: invalid number of hosts %d !\n", ints
[1] );
199 setup_disconnect
= ints
[2];
203 setup_nosync
= ints
[3];
208 /* no limits on this, just > 0 */
209 setup_can_queue
= ints
[4];
213 setup_cmd_per_lun
= ints
[5];
217 setup_sg_tablesize
= ints
[6];
218 /* Must be <= SG_ALL (255) */
219 if (setup_sg_tablesize
> SG_ALL
)
220 setup_sg_tablesize
= SG_ALL
;
224 /* Must be between 0 and 7 */
225 if (ints
[7] >= 0 && ints
[7] <= 7)
226 setup_hostid
= ints
[7];
227 else if (ints
[7] > 7)
228 printk( "mac_esp_setup: invalid host ID %d !\n", ints
[7] );
233 setup_use_tagged_queuing
= !!ints
[8];
240 * ESP address 'detection'
243 unsigned long get_base(int chip_num
)
246 * using the chip_num and mac model, figure out where the
250 unsigned long io_base
= 0x50f00000;
251 unsigned int second_offset
= 0x402;
252 unsigned long scsi_loc
= 0;
254 switch (macintosh_config
->scsi_type
) {
257 case MAC_SCSI_QUADRA2
:
258 scsi_loc
= io_base
+ 0xf000 + ((chip_num
== 0) ? 0 : second_offset
);
262 case MAC_SCSI_QUADRA3
:
263 scsi_loc
= io_base
+ 0x18000 + ((chip_num
== 0) ? 0 : second_offset
);
266 /* most quadra/centris models are like this */
267 case MAC_SCSI_QUADRA
:
268 scsi_loc
= io_base
+ 0x10000;
272 printk("mac_esp: get_base: hit default!\n");
273 scsi_loc
= io_base
+ 0x10000;
278 printk("mac_esp: io base at 0x%lx\n", scsi_loc
);
284 * Model dependent ESP setup
287 int mac_esp_detect(Scsi_Host_Template
* tpnt
)
290 int chipnum
, chipspresent
= 0;
292 unsigned long timeout
;
295 if (esp_initialized
> 0)
298 /* what do we have in this machine... */
299 if (MACHW_PRESENT(MAC_SCSI_96
)) {
303 if (MACHW_PRESENT(MAC_SCSI_96_2
)) {
307 /* number of ESPs present ? */
308 if (setup_num_esps
>= 0) {
309 if (chipspresent
>= setup_num_esps
)
310 chipspresent
= setup_num_esps
;
312 printk("mac_esp_detect: num_hosts detected %d setup %d \n",
313 chipspresent
, setup_num_esps
);
316 /* TODO: add disconnect / nosync flags */
318 /* setup variables */
320 (setup_can_queue
> 0) ? setup_can_queue
: 7;
322 (setup_cmd_per_lun
> 0) ? setup_cmd_per_lun
: 1;
324 (setup_sg_tablesize
>= 0) ? setup_sg_tablesize
: SG_ALL
;
326 if (setup_hostid
>= 0)
327 tpnt
->this_id
= setup_hostid
;
329 /* use 7 as default */
334 if (setup_use_tagged_queuing
< 0)
335 setup_use_tagged_queuing
= DEFAULT_USE_TAGGED_QUEUING
;
338 for (chipnum
= 0; chipnum
< chipspresent
; chipnum
++) {
339 struct NCR_ESP
* esp
;
341 esp
= esp_allocate(tpnt
, (void *) NULL
);
342 esp
->eregs
= (struct ESP_regs
*) get_base(chipnum
);
344 esp
->dma_irq_p
= &esp_dafb_dma_irq_p
;
347 if (macintosh_config
->scsi_type
== MAC_SCSI_QUADRA
) {
348 /* most machines except those below :-) */
350 esp
->dma_irq_p
= &esp_iosb_dma_irq_p
;
351 } else if (macintosh_config
->scsi_type
== MAC_SCSI_QUADRA3
) {
357 writel(0x1d1, 0xf9800024);
358 esp
->dregs
= (void *) 0xf9800024;
361 } else { /* chipnum */
364 writel(0x1d1, 0xf9800028);
365 esp
->dregs
= (void *) 0xf9800028;
369 /* use pio for command bytes; pio for message/data: TBI */
370 esp
->do_pio_cmds
= 1;
372 /* Set the command buffer */
373 esp
->esp_command
= (volatile unsigned char*) cmd_buffer
;
374 esp
->esp_command_dvma
= (volatile unsigned char*) cmd_buffer
;
376 /* various functions */
377 esp
->dma_bytes_sent
= &dma_bytes_sent
;
378 esp
->dma_can_transfer
= &dma_can_transfer
;
379 esp
->dma_dump_state
= &dma_dump_state
;
380 esp
->dma_init_read
= NULL
;
381 esp
->dma_init_write
= NULL
;
382 esp
->dma_ints_off
= &dma_ints_off
;
383 esp
->dma_ints_on
= &dma_ints_on
;
385 esp
->dma_ports_p
= &dma_ports_p
;
388 /* Optional functions */
389 esp
->dma_barrier
= NULL
;
390 esp
->dma_drain
= NULL
;
391 esp
->dma_invalidate
= NULL
;
392 esp
->dma_irq_entry
= NULL
;
393 esp
->dma_irq_exit
= NULL
;
394 esp
->dma_led_on
= NULL
;
395 esp
->dma_led_off
= NULL
;
396 esp
->dma_poll
= NULL
;
397 esp
->dma_reset
= NULL
;
399 /* SCSI chip speed */
400 /* below esp->cfreq = 40000000; */
404 /* 'quick' means there's handshake glue logic like in the 5380 case */
405 esp
->dma_setup
= &dma_setup_quick
;
407 esp
->dma_setup
= &dma_setup
;
412 esp
->irq
= IRQ_MAC_SCSI
;
414 request_irq(IRQ_MAC_SCSI
, esp_intr
, 0, "Mac ESP SCSI", esp
);
415 request_irq(IRQ_MAC_SCSIDRQ
, fake_drq
, 0, "Mac ESP DRQ", esp
);
417 if (macintosh_config
->scsi_type
== MAC_SCSI_QUADRA
) {
418 esp
->cfreq
= 16500000;
420 esp
->cfreq
= 25000000;
424 } else { /* chipnum == 1 */
426 esp
->irq
= IRQ_MAC_SCSIDRQ
;
428 request_irq(IRQ_MAC_SCSIDRQ
, esp_intr
, 0, "Mac ESP SCSI 2", esp
);
430 esp
->cfreq
= 25000000;
435 printk("esp: using quick version\n");
438 printk("esp: addr at 0x%p\n", esp
->eregs
);
448 printk("\nmac_esp: %d esp controllers found\n", chipspresent
);
450 esp_initialized
= chipspresent
;
456 * I've been wondering what this is supposed to do, for some time. Talking
457 * to Allen Briggs: These machines have an extra register someplace where the
458 * DRQ pin of the ESP can be monitored. That isn't useful for determining
459 * anything else (such as reselect interrupt or other magic) though.
460 * Maybe make the semantics should be changed like
461 * if (esp->current_SC)
462 * ... check DRQ flag ...
464 * ... disconnected, check pending VIA interrupt ...
466 * There's a problem with using the dabf flag or mac_irq_pending() here: both
467 * seem to return 1 even though no interrupt is currently pending, resulting
468 * in esp_exec_cmd() holding off the next command, and possibly infinite loops
470 * Short term fix: just use esp_status & ESP_STAT_INTR here, as long as we
471 * use simple PIO. The DRQ status will be important when implementing pseudo
472 * DMA mode (set up ESP transfer count, return, do a batch of bytes in PIO or
473 * 'hardware handshake' mode upon DRQ).
474 * If you plan on changing this (i.e. to save the esp_status register access in
475 * favor of a VIA register access or a shadow register for the IFR), make sure
476 * to try a debug version of this first to monitor what registers would be a good
477 * indicator of the ESP interrupt.
480 static int esp_dafb_dma_irq_p(struct NCR_ESP
* esp
)
483 int sreg
= esp_read(esp
->eregs
->esp_status
);
486 printk("mac_esp: esp_dafb_dma_irq_p dafb %d irq %d\n",
487 readl(esp
->dregs
), mac_irq_pending(IRQ_MAC_SCSI
));
490 sreg
&= ESP_STAT_INTR
;
493 * maybe working; this is essentially what's used for iosb_dma_irq_p
505 ret
= readl(esp
->dregs
) & 0x200;
506 else if (esp
->disconnected_SC
)
507 ret
= 1; /* sreg ?? */
509 ret
= mac_irq_pending(IRQ_MAC_SCSI
);
517 * See above: testing mac_irq_pending always returned 8 (SCSI IRQ) regardless
518 * of the actual ESP status.
521 static int esp_iosb_dma_irq_p(struct NCR_ESP
* esp
)
523 int ret
= mac_irq_pending(IRQ_MAC_SCSI
) || mac_irq_pending(IRQ_MAC_SCSIDRQ
);
524 int sreg
= esp_read(esp
->eregs
->esp_status
);
527 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
528 mac_irq_pending(IRQ_MAC_SCSIDRQ
), mac_irq_pending(IRQ_MAC_SCSI
),
529 sreg
, esp
->current_SC
, esp
->disconnected_SC
);
532 sreg
&= ESP_STAT_INTR
;
541 * This seems to be OK for PIO at least ... usually 0 after PIO.
544 static int dma_bytes_sent(struct NCR_ESP
* esp
, int fifo_count
)
548 printk("mac_esp: dma bytes sent = %x\n", fifo_count
);
555 * dma_can_transfer is used to switch between DMA and PIO, if DMA (pseudo)
556 * is ever implemented. Returning 0 here will use PIO.
559 static int dma_can_transfer(struct NCR_ESP
* esp
, Scsi_Cmnd
* sp
)
561 unsigned long sz
= sp
->SCp
.this_residual
;
562 #if 0 /* no DMA yet; make conditional */
563 if (sz
> 0x10000000) {
566 printk("mac_esp: dma can transfer = 0lx%x\n", sz
);
570 printk("mac_esp: pio to transfer = %ld\n", sz
);
582 static void dma_dump_state(struct NCR_ESP
* esp
)
585 printk("mac_esp: dma_dump_state: called\n");
588 ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
589 esp
->esp_id
, ((struct mac_dma_registers
*)
590 (esp
->dregs
))->cond_reg
));
595 * DMA setup: should be used to set up the ESP transfer count for pseudo
596 * DMA transfers; need a DRQ transfer function to do the actual transfer
599 static void dma_init_read(struct NCR_ESP
* esp
, char * vaddress
, int length
)
601 printk("mac_esp: dma_init_read\n");
605 static void dma_init_write(struct NCR_ESP
* esp
, char * vaddress
, int length
)
607 printk("mac_esp: dma_init_write\n");
611 static void dma_ints_off(struct NCR_ESP
* esp
)
613 mac_turnoff_irq(esp
->irq
);
617 static void dma_ints_on(struct NCR_ESP
* esp
)
619 mac_turnon_irq(esp
->irq
);
623 * generic dma_irq_p(), unused
626 static int dma_irq_p(struct NCR_ESP
* esp
)
628 int i
= esp_read(esp
->eregs
->esp_status
);
631 printk("mac_esp: dma_irq_p status %d\n", i
);
634 return (i
& ESP_STAT_INTR
);
637 static int dma_irq_p_quick(struct NCR_ESP
* esp
)
640 * Copied from iosb_dma_irq_p()
642 int ret
= mac_irq_pending(IRQ_MAC_SCSI
) || mac_irq_pending(IRQ_MAC_SCSIDRQ
);
643 int sreg
= esp_read(esp
->eregs
->esp_status
);
646 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
647 mac_irq_pending(IRQ_MAC_SCSIDRQ
), mac_irq_pending(IRQ_MAC_SCSI
),
648 sreg
, esp
->current_SC
, esp
->disconnected_SC
);
651 sreg
&= ESP_STAT_INTR
;
660 static void dma_led_off(struct NCR_ESP
* esp
)
663 printk("mac_esp: dma_led_off: called\n");
668 static void dma_led_on(struct NCR_ESP
* esp
)
671 printk("mac_esp: dma_led_on: called\n");
676 static int dma_ports_p(struct NCR_ESP
* esp
)
682 static void dma_setup(struct NCR_ESP
* esp
, __u32 addr
, int count
, int write
)
686 printk("mac_esp: dma_setup\n");
690 dma_init_read(esp
, (char *) addr
, count
);
692 dma_init_write(esp
, (char *) addr
, count
);
697 static void dma_setup_quick(struct NCR_ESP
* esp
, __u32 addr
, int count
, int write
)
700 printk("mac_esp: dma_setup_quick\n");