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/slab.h>
22 #include <linux/blkdev.h>
23 #include <linux/proc_fs.h>
24 #include <linux/stat.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
29 #include <scsi/scsi_host.h>
34 #include <asm/setup.h>
36 #include <asm/macints.h>
37 #include <asm/machw.h>
38 #include <asm/mac_via.h>
40 #include <asm/pgtable.h>
42 #include <asm/macintosh.h>
44 /* #define DEBUG_MAC_ESP */
46 #define mac_turnon_irq(x) mac_enable_irq(x)
47 #define mac_turnoff_irq(x) mac_disable_irq(x)
49 extern void esp_handle(struct NCR_ESP *esp);
50 extern void mac_esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
52 static int dma_bytes_sent(struct NCR_ESP * esp, int fifo_count);
53 static int dma_can_transfer(struct NCR_ESP * esp, Scsi_Cmnd *sp);
54 static void dma_dump_state(struct NCR_ESP * esp);
55 static void dma_init_read(struct NCR_ESP * esp, char * vaddress, int length);
56 static void dma_init_write(struct NCR_ESP * esp, char * vaddress, int length);
57 static void dma_ints_off(struct NCR_ESP * esp);
58 static void dma_ints_on(struct NCR_ESP * esp);
59 static int dma_irq_p(struct NCR_ESP * esp);
60 static int dma_irq_p_quick(struct NCR_ESP * esp);
61 static void dma_led_off(struct NCR_ESP * esp);
62 static void dma_led_on(struct NCR_ESP *esp);
63 static int dma_ports_p(struct NCR_ESP *esp);
64 static void dma_setup(struct NCR_ESP * esp, __u32 addr, int count, int write);
65 static void dma_setup_quick(struct NCR_ESP * esp, __u32 addr, int count, int write);
67 static int esp_dafb_dma_irq_p(struct NCR_ESP * espdev);
68 static int esp_iosb_dma_irq_p(struct NCR_ESP * espdev);
70 static volatile unsigned char cmd_buffer[16];
71 /* This is where all commands are put
72 * before they are transferred to the ESP chip
76 static int esp_initialized = 0;
78 static int setup_num_esps = -1;
79 static int setup_disconnect = -1;
80 static int setup_nosync = -1;
81 static int setup_can_queue = -1;
82 static int setup_cmd_per_lun = -1;
83 static int setup_sg_tablesize = -1;
85 static int setup_use_tagged_queuing = -1;
87 static int setup_hostid = -1;
90 * Experimental ESP inthandler; check macints.c to make sure dev_id is
94 void mac_esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
96 struct NCR_ESP *esp = (struct NCR_ESP *) dev_id;
99 /* Handle the one ESP interrupt showing at this IRQ level. */
100 if(((esp)->irq & 0xff) == irq) {
104 irq_p = esp->dma_irq_p(esp);
105 printk("mac_esp: irq_p %x current %p disconnected %p\n",
106 irq_p, esp->current_SC, esp->disconnected_SC);
109 * Mac: if we're here, it's an ESP interrupt for sure!
111 if((esp->current_SC || esp->disconnected_SC)) {
112 esp->dma_ints_off(esp);
114 ESPIRQ(("I%d(", esp->esp_id));
118 esp->dma_ints_on(esp);
124 * Debug hooks; use for playing with the interrupt flag testing and interrupt
125 * acknowledge on the various machines
128 void scsi_esp_polled(int irq, void *dev_id, struct pt_regs *pregs)
130 if (esp_initialized == 0)
133 mac_esp_intr(irq, dev_id, pregs);
136 void fake_intr(int irq, void *dev_id, struct pt_regs *pregs)
139 printk("mac_esp: got irq\n");
142 mac_esp_intr(irq, dev_id, pregs);
145 irqreturn_t fake_drq(int irq, void *dev_id, struct pt_regs *pregs)
147 printk("mac_esp: got drq\n");
154 * Function : mac_esp_setup(char *str)
156 * Purpose : booter command line initialization of the overrides array,
158 * Inputs : str - parameters, separated by commas.
160 * Currently unused in the new driver; need to add settable parameters to the
165 static int __init mac_esp_setup(char *str) {
167 /* Format of mac53c9x parameter is:
168 * mac53c9x=<num_esps>,<disconnect>,<nosync>,<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
169 * Negative values mean don't change.
175 this_opt = strsep (&str, ",");
177 opt = simple_strtol( this_opt, NULL, 0 );
179 if (opt >= 0 && opt <= 2)
180 setup_num_esps = opt;
182 printk( "mac_esp_setup: invalid number of hosts %ld !\n", opt );
184 this_opt = strsep (&str, ",");
187 opt = simple_strtol( this_opt, NULL, 0 );
190 setup_disconnect = opt;
192 this_opt = strsep (&str, ",");
195 opt = simple_strtol( this_opt, NULL, 0 );
200 this_opt = strsep (&str, ",");
203 opt = simple_strtol( this_opt, NULL, 0 );
206 setup_can_queue = opt;
208 this_opt = strsep (&str, ",");
211 opt = simple_strtol( this_opt, NULL, 0 );
214 setup_cmd_per_lun = opt;
216 this_opt = strsep (&str, ",");
219 opt = simple_strtol( this_opt, NULL, 0 );
222 setup_sg_tablesize = opt;
223 /* Must be <= SG_ALL (255) */
224 if (setup_sg_tablesize > SG_ALL)
225 setup_sg_tablesize = SG_ALL;
228 this_opt = strsep (&str, ",");
231 opt = simple_strtol( this_opt, NULL, 0 );
233 /* Must be between 0 and 7 */
234 if (opt >= 0 && opt <= 7)
237 printk( "mac_esp_setup: invalid host ID %ld !\n", opt);
239 this_opt = strsep (&str, ",");
243 opt = simple_strtol( this_opt, NULL, 0 );
245 setup_use_tagged_queuing = !!opt;
252 __setup("mac53c9x=", mac_esp_setup);
256 * ESP address 'detection'
259 unsigned long get_base(int chip_num)
262 * using the chip_num and mac model, figure out where the
266 unsigned long io_base = 0x50f00000;
267 unsigned int second_offset = 0x402;
268 unsigned long scsi_loc = 0;
270 switch (macintosh_config->scsi_type) {
273 case MAC_SCSI_QUADRA2:
274 scsi_loc = io_base + 0xf000 + ((chip_num == 0) ? 0 : second_offset);
278 case MAC_SCSI_QUADRA3:
279 scsi_loc = io_base + 0x18000 + ((chip_num == 0) ? 0 : second_offset);
282 /* most quadra/centris models are like this */
283 case MAC_SCSI_QUADRA:
284 scsi_loc = io_base + 0x10000;
288 printk("mac_esp: get_base: hit default!\n");
289 scsi_loc = io_base + 0x10000;
294 printk("mac_esp: io base at 0x%lx\n", scsi_loc);
300 * Model dependent ESP setup
303 int mac_esp_detect(struct scsi_host_template * tpnt)
306 int chipnum, chipspresent = 0;
308 unsigned long timeout;
311 if (esp_initialized > 0)
314 /* what do we have in this machine... */
315 if (MACHW_PRESENT(MAC_SCSI_96)) {
319 if (MACHW_PRESENT(MAC_SCSI_96_2)) {
323 /* number of ESPs present ? */
324 if (setup_num_esps >= 0) {
325 if (chipspresent >= setup_num_esps)
326 chipspresent = setup_num_esps;
328 printk("mac_esp_detect: num_hosts detected %d setup %d \n",
329 chipspresent, setup_num_esps);
332 /* TODO: add disconnect / nosync flags */
334 /* setup variables */
336 (setup_can_queue > 0) ? setup_can_queue : 7;
338 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : 1;
340 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_ALL;
342 if (setup_hostid >= 0)
343 tpnt->this_id = setup_hostid;
345 /* use 7 as default */
350 if (setup_use_tagged_queuing < 0)
351 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
354 for (chipnum = 0; chipnum < chipspresent; chipnum ++) {
355 struct NCR_ESP * esp;
357 esp = esp_allocate(tpnt, (void *) NULL);
358 esp->eregs = (struct ESP_regs *) get_base(chipnum);
360 esp->dma_irq_p = &esp_dafb_dma_irq_p;
363 if (macintosh_config->scsi_type == MAC_SCSI_QUADRA) {
364 /* most machines except those below :-) */
366 esp->dma_irq_p = &esp_iosb_dma_irq_p;
367 } else if (macintosh_config->scsi_type == MAC_SCSI_QUADRA3) {
373 out_be32(0xf9800024, 0x1d1);
374 esp->dregs = (void *) 0xf9800024;
377 } else { /* chipnum */
380 out_be32(0xf9800028, 0x1d1);
381 esp->dregs = (void *) 0xf9800028;
385 /* use pio for command bytes; pio for message/data: TBI */
386 esp->do_pio_cmds = 1;
388 /* Set the command buffer */
389 esp->esp_command = (volatile unsigned char*) cmd_buffer;
390 esp->esp_command_dvma = (__u32) cmd_buffer;
392 /* various functions */
393 esp->dma_bytes_sent = &dma_bytes_sent;
394 esp->dma_can_transfer = &dma_can_transfer;
395 esp->dma_dump_state = &dma_dump_state;
396 esp->dma_init_read = NULL;
397 esp->dma_init_write = NULL;
398 esp->dma_ints_off = &dma_ints_off;
399 esp->dma_ints_on = &dma_ints_on;
401 esp->dma_ports_p = &dma_ports_p;
404 /* Optional functions */
405 esp->dma_barrier = NULL;
406 esp->dma_drain = NULL;
407 esp->dma_invalidate = NULL;
408 esp->dma_irq_entry = NULL;
409 esp->dma_irq_exit = NULL;
410 esp->dma_led_on = NULL;
411 esp->dma_led_off = NULL;
412 esp->dma_poll = NULL;
413 esp->dma_reset = NULL;
415 /* SCSI chip speed */
416 /* below esp->cfreq = 40000000; */
420 /* 'quick' means there's handshake glue logic like in the 5380 case */
421 esp->dma_setup = &dma_setup_quick;
423 esp->dma_setup = &dma_setup;
428 esp->irq = IRQ_MAC_SCSI;
430 request_irq(IRQ_MAC_SCSI, esp_intr, 0, "Mac ESP SCSI", esp->ehost);
431 #if 0 /* conflicts with IOP ADB */
432 request_irq(IRQ_MAC_SCSIDRQ, fake_drq, 0, "Mac ESP DRQ", esp->ehost);
435 if (macintosh_config->scsi_type == MAC_SCSI_QUADRA) {
436 esp->cfreq = 16500000;
438 esp->cfreq = 25000000;
442 } else { /* chipnum == 1 */
444 esp->irq = IRQ_MAC_SCSIDRQ;
445 #if 0 /* conflicts with IOP ADB */
446 request_irq(IRQ_MAC_SCSIDRQ, esp_intr, 0, "Mac ESP SCSI 2", esp->ehost);
449 esp->cfreq = 25000000;
454 printk("esp: using quick version\n");
457 printk("esp: addr at 0x%p\n", esp->eregs);
467 printk("\nmac_esp: %d esp controllers found\n", chipspresent);
469 esp_initialized = chipspresent;
474 static int mac_esp_release(struct Scsi_Host *shost)
477 free_irq(shost->irq, NULL);
478 if (shost->io_port && shost->n_io_port)
479 release_region(shost->io_port, shost->n_io_port);
480 scsi_unregister(shost);
485 * I've been wondering what this is supposed to do, for some time. Talking
486 * to Allen Briggs: These machines have an extra register someplace where the
487 * DRQ pin of the ESP can be monitored. That isn't useful for determining
488 * anything else (such as reselect interrupt or other magic) though.
489 * Maybe make the semantics should be changed like
490 * if (esp->current_SC)
491 * ... check DRQ flag ...
493 * ... disconnected, check pending VIA interrupt ...
495 * There's a problem with using the dabf flag or mac_irq_pending() here: both
496 * seem to return 1 even though no interrupt is currently pending, resulting
497 * in esp_exec_cmd() holding off the next command, and possibly infinite loops
499 * Short term fix: just use esp_status & ESP_STAT_INTR here, as long as we
500 * use simple PIO. The DRQ status will be important when implementing pseudo
501 * DMA mode (set up ESP transfer count, return, do a batch of bytes in PIO or
502 * 'hardware handshake' mode upon DRQ).
503 * If you plan on changing this (i.e. to save the esp_status register access in
504 * favor of a VIA register access or a shadow register for the IFR), make sure
505 * to try a debug version of this first to monitor what registers would be a good
506 * indicator of the ESP interrupt.
509 static int esp_dafb_dma_irq_p(struct NCR_ESP * esp)
512 int sreg = esp_read(esp->eregs->esp_status);
515 printk("mac_esp: esp_dafb_dma_irq_p dafb %d irq %d\n",
516 readl(esp->dregs), mac_irq_pending(IRQ_MAC_SCSI));
519 sreg &= ESP_STAT_INTR;
522 * maybe working; this is essentially what's used for iosb_dma_irq_p
534 ret = readl(esp->dregs) & 0x200;
535 else if (esp->disconnected_SC)
536 ret = 1; /* sreg ?? */
538 ret = mac_irq_pending(IRQ_MAC_SCSI);
546 * See above: testing mac_irq_pending always returned 8 (SCSI IRQ) regardless
547 * of the actual ESP status.
550 static int esp_iosb_dma_irq_p(struct NCR_ESP * esp)
552 int ret = mac_irq_pending(IRQ_MAC_SCSI) || mac_irq_pending(IRQ_MAC_SCSIDRQ);
553 int sreg = esp_read(esp->eregs->esp_status);
556 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
557 mac_irq_pending(IRQ_MAC_SCSIDRQ), mac_irq_pending(IRQ_MAC_SCSI),
558 sreg, esp->current_SC, esp->disconnected_SC);
561 sreg &= ESP_STAT_INTR;
570 * This seems to be OK for PIO at least ... usually 0 after PIO.
573 static int dma_bytes_sent(struct NCR_ESP * esp, int fifo_count)
577 printk("mac_esp: dma bytes sent = %x\n", fifo_count);
584 * dma_can_transfer is used to switch between DMA and PIO, if DMA (pseudo)
585 * is ever implemented. Returning 0 here will use PIO.
588 static int dma_can_transfer(struct NCR_ESP * esp, Scsi_Cmnd * sp)
590 unsigned long sz = sp->SCp.this_residual;
591 #if 0 /* no DMA yet; make conditional */
592 if (sz > 0x10000000) {
595 printk("mac_esp: dma can transfer = 0lx%x\n", sz);
599 printk("mac_esp: pio to transfer = %ld\n", sz);
611 static void dma_dump_state(struct NCR_ESP * esp)
614 printk("mac_esp: dma_dump_state: called\n");
617 ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
618 esp->esp_id, ((struct mac_dma_registers *)
619 (esp->dregs))->cond_reg));
624 * DMA setup: should be used to set up the ESP transfer count for pseudo
625 * DMA transfers; need a DRQ transfer function to do the actual transfer
628 static void dma_init_read(struct NCR_ESP * esp, char * vaddress, int length)
630 printk("mac_esp: dma_init_read\n");
634 static void dma_init_write(struct NCR_ESP * esp, char * vaddress, int length)
636 printk("mac_esp: dma_init_write\n");
640 static void dma_ints_off(struct NCR_ESP * esp)
642 mac_turnoff_irq(esp->irq);
646 static void dma_ints_on(struct NCR_ESP * esp)
648 mac_turnon_irq(esp->irq);
652 * generic dma_irq_p(), unused
655 static int dma_irq_p(struct NCR_ESP * esp)
657 int i = esp_read(esp->eregs->esp_status);
660 printk("mac_esp: dma_irq_p status %d\n", i);
663 return (i & ESP_STAT_INTR);
666 static int dma_irq_p_quick(struct NCR_ESP * esp)
669 * Copied from iosb_dma_irq_p()
671 int ret = mac_irq_pending(IRQ_MAC_SCSI) || mac_irq_pending(IRQ_MAC_SCSIDRQ);
672 int sreg = esp_read(esp->eregs->esp_status);
675 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
676 mac_irq_pending(IRQ_MAC_SCSIDRQ), mac_irq_pending(IRQ_MAC_SCSI),
677 sreg, esp->current_SC, esp->disconnected_SC);
680 sreg &= ESP_STAT_INTR;
689 static void dma_led_off(struct NCR_ESP * esp)
692 printk("mac_esp: dma_led_off: called\n");
697 static void dma_led_on(struct NCR_ESP * esp)
700 printk("mac_esp: dma_led_on: called\n");
705 static int dma_ports_p(struct NCR_ESP * esp)
711 static void dma_setup(struct NCR_ESP * esp, __u32 addr, int count, int write)
715 printk("mac_esp: dma_setup\n");
719 dma_init_read(esp, (char *) addr, count);
721 dma_init_write(esp, (char *) addr, count);
726 static void dma_setup_quick(struct NCR_ESP * esp, __u32 addr, int count, int write)
729 printk("mac_esp: dma_setup_quick\n");
733 static struct scsi_host_template driver_template = {
734 .proc_name = "mac_esp",
735 .name = "Mac 53C9x SCSI",
736 .detect = mac_esp_detect,
737 .slave_alloc = esp_slave_alloc,
738 .slave_destroy = esp_slave_destroy,
739 .release = mac_esp_release,
741 .queuecommand = esp_queue,
742 .eh_abort_handler = esp_abort,
743 .eh_bus_reset_handler = esp_reset,
746 .sg_tablesize = SG_ALL,
748 .use_clustering = DISABLE_CLUSTERING
752 #include "scsi_module.c"
754 MODULE_LICENSE("GPL");