Import 2.3.18pre1
[davej-history.git] / drivers / scsi / mac_esp.c
blob726bf4107db64d708896182229355cb3e14f32b8
1 /*
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
9 */
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>
26 #include "scsi.h"
27 #include "hosts.h"
28 #include "NCR53C9x.h"
29 #include "mac_esp.h"
31 #include "../../arch/m68k/mac/via6522.h" /* huh? */
33 #include <asm/io.h>
35 #include <asm/setup.h>
36 #include <asm/irq.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
68 * via PIO.
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;
79 #ifdef SUPPORT_TAGS
80 static int setup_use_tagged_queuing = -1;
81 #endif
82 static int setup_hostid = -1;
85 * Experimental ESP inthandler; check macints.c to make sure dev_id is
86 * set up properly!
89 void mac_esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
91 struct NCR_ESP *esp = (struct NCR_ESP *) dev_id;
92 int irq_p = 0;
94 /* Handle the one ESP interrupt showing at this IRQ level. */
95 if(((esp)->irq & 0xff) == irq) {
97 * Debug ..
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));
110 esp_handle(esp);
111 ESPIRQ((")"));
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)
126 return;
128 mac_esp_intr(irq, dev_id, pregs);
131 void fake_intr(int irq, void *dev_id, struct pt_regs *pregs)
133 #ifdef DEBUG_MAC_ESP
134 printk("mac_esp: got irq\n");
135 #endif
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");
145 #define DRIVER_SETUP
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
156 * detect function.
160 void mac_esp_setup(char *str, int *ints) {
161 #ifdef DRIVER_SETUP
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) {
174 if (*str == '-')
175 fact = -1, ++str;
176 else
177 fact = 1;
178 ints[i++] = simple_strtoul( str, NULL, 0 ) * fact;
179 if ((str = strchr( str, ',' )) != NULL)
180 ++str;
182 ints[0] = i-1;
184 if (ints[0] < 1) {
185 printk( "mac_esp_setup: no arguments!\n" );
186 return;
189 if (ints[0] >= 1) {
190 if (ints[1] > 0)
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] );
197 if (ints[0] >= 2) {
198 if (ints[2] > 0)
199 setup_disconnect = ints[2];
201 if (ints[0] >= 3) {
202 if (ints[3] >= 0) {
203 setup_nosync = ints[3];
206 if (ints[0] >= 4) {
207 if (ints[4] > 0)
208 /* no limits on this, just > 0 */
209 setup_can_queue = ints[4];
211 if (ints[0] >= 5) {
212 if (ints[5] > 0)
213 setup_cmd_per_lun = ints[5];
215 if (ints[0] >= 6) {
216 if (ints[6] >= 0) {
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;
223 if (ints[0] >= 7) {
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] );
230 #ifdef SUPPORT_TAGS
231 if (ints[0] >= 8) {
232 if (ints[8] >= 0)
233 setup_use_tagged_queuing = !!ints[8];
235 #endif
236 #endif
240 * ESP address 'detection'
243 unsigned long get_base(int chip_num)
246 * using the chip_num and mac model, figure out where the
247 * chips are mapped
250 unsigned long io_base = 0x50f00000;
251 unsigned int second_offset = 0x402;
252 unsigned long scsi_loc = 0;
254 switch (macintosh_config->scsi_type) {
256 /* 950, 900, 700 */
257 case MAC_SCSI_QUADRA2:
258 scsi_loc = io_base + 0xf000 + ((chip_num == 0) ? 0 : second_offset);
259 break;
261 /* av's */
262 case MAC_SCSI_QUADRA3:
263 scsi_loc = io_base + 0x18000 + ((chip_num == 0) ? 0 : second_offset);
264 break;
266 /* most quadra/centris models are like this */
267 case MAC_SCSI_QUADRA:
268 scsi_loc = io_base + 0x10000;
269 break;
271 default:
272 printk("mac_esp: get_base: hit default!\n");
273 scsi_loc = io_base + 0x10000;
274 break;
276 } /* switch */
278 printk("mac_esp: io base at 0x%lx\n", scsi_loc);
280 return scsi_loc;
284 * Model dependent ESP setup
287 int mac_esp_detect(Scsi_Host_Template * tpnt)
289 int quick = 0;
290 int chipnum, chipspresent = 0;
291 #if 0
292 unsigned long timeout;
293 #endif
295 if (esp_initialized > 0)
296 return -ENODEV;
298 /* what do we have in this machine... */
299 if (MACHW_PRESENT(MAC_SCSI_96)) {
300 chipspresent ++;
303 if (MACHW_PRESENT(MAC_SCSI_96_2)) {
304 chipspresent ++;
307 /* number of ESPs present ? */
308 if (setup_num_esps >= 0) {
309 if (chipspresent >= setup_num_esps)
310 chipspresent = setup_num_esps;
311 else
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 */
319 tpnt->can_queue =
320 (setup_can_queue > 0) ? setup_can_queue : 7;
321 tpnt->cmd_per_lun =
322 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : 1;
323 tpnt->sg_tablesize =
324 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_ALL;
326 if (setup_hostid >= 0)
327 tpnt->this_id = setup_hostid;
328 else {
329 /* use 7 as default */
330 tpnt->this_id = 7;
333 #ifdef SUPPORT_TAGS
334 if (setup_use_tagged_queuing < 0)
335 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
336 #endif
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;
345 if (chipnum == 0) {
347 if (macintosh_config->scsi_type == MAC_SCSI_QUADRA) {
348 /* most machines except those below :-) */
349 quick = 1;
350 esp->dma_irq_p = &esp_iosb_dma_irq_p;
351 } else if (macintosh_config->scsi_type == MAC_SCSI_QUADRA3) {
352 /* mostly av's */
353 quick = 0;
354 } else {
355 /* q950, 900, 700 */
356 quick = 1;
357 writel(0x1d1, 0xf9800024);
358 esp->dregs = (void *) 0xf9800024;
361 } else { /* chipnum */
363 quick = 1;
364 writel(0x1d1, 0xf9800028);
365 esp->dregs = (void *) 0xf9800028;
367 } /* chipnum == 0 */
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; */
403 if (quick) {
404 /* 'quick' means there's handshake glue logic like in the 5380 case */
405 esp->dma_setup = &dma_setup_quick;
406 } else {
407 esp->dma_setup = &dma_setup;
410 if (chipnum == 0) {
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;
419 } else {
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;
434 if (quick) {
435 printk("esp: using quick version\n");
438 printk("esp: addr at 0x%p\n", esp->eregs);
440 esp->scsi_id = 7;
441 esp->diff = 0;
443 esp_initialize(esp);
445 } /* for chipnum */
447 if (chipspresent)
448 printk("\nmac_esp: %d esp controllers found\n", chipspresent);
450 esp_initialized = chipspresent;
452 return 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 ...
463 * else
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
469 * in esp_intr().
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)
482 unsigned int ret;
483 int sreg = esp_read(esp->eregs->esp_status);
485 #ifdef DEBUG_MAC_ESP
486 printk("mac_esp: esp_dafb_dma_irq_p dafb %d irq %d\n",
487 readl(esp->dregs), mac_irq_pending(IRQ_MAC_SCSI));
488 #endif
490 sreg &= ESP_STAT_INTR;
493 * maybe working; this is essentially what's used for iosb_dma_irq_p
495 if (sreg)
496 return 1;
497 else
498 return 0;
501 * didn't work ...
503 #if 0
504 if (esp->current_SC)
505 ret = readl(esp->dregs) & 0x200;
506 else if (esp->disconnected_SC)
507 ret = 1; /* sreg ?? */
508 else
509 ret = mac_irq_pending(IRQ_MAC_SCSI);
511 return(ret);
512 #endif
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);
526 #ifdef DEBUG_MAC_ESP
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);
530 #endif
532 sreg &= ESP_STAT_INTR;
534 if (sreg)
535 return (sreg);
536 else
537 return 0;
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)
547 #ifdef DEBUG_MAC_ESP
548 printk("mac_esp: dma bytes sent = %x\n", fifo_count);
549 #endif
551 return 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) {
564 sz = 0x10000000;
566 printk("mac_esp: dma can transfer = 0lx%x\n", sz);
567 #else
569 #ifdef DEBUG_MAC_ESP
570 printk("mac_esp: pio to transfer = %ld\n", sz);
571 #endif
573 sz = 0;
574 #endif
575 return sz;
579 * Not yet ...
582 static void dma_dump_state(struct NCR_ESP * esp)
584 #ifdef DEBUG_MAC_ESP
585 printk("mac_esp: dma_dump_state: called\n");
586 #endif
587 #if 0
588 ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
589 esp->esp_id, ((struct mac_dma_registers *)
590 (esp->dregs))->cond_reg));
591 #endif
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);
630 #ifdef DEBUG_MAC_ESP
631 printk("mac_esp: dma_irq_p status %d\n", i);
632 #endif
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);
645 #ifdef DEBUG_MAC_ESP
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);
649 #endif
651 sreg &= ESP_STAT_INTR;
653 if (sreg)
654 return (sreg);
655 else
656 return 0;
660 static void dma_led_off(struct NCR_ESP * esp)
662 #ifdef DEBUG_MAC_ESP
663 printk("mac_esp: dma_led_off: called\n");
664 #endif
668 static void dma_led_on(struct NCR_ESP * esp)
670 #ifdef DEBUG_MAC_ESP
671 printk("mac_esp: dma_led_on: called\n");
672 #endif
676 static int dma_ports_p(struct NCR_ESP * esp)
678 return 0;
682 static void dma_setup(struct NCR_ESP * esp, __u32 addr, int count, int write)
685 #ifdef DEBUG_MAC_ESP
686 printk("mac_esp: dma_setup\n");
687 #endif
689 if (write) {
690 dma_init_read(esp, (char *) addr, count);
691 } else {
692 dma_init_write(esp, (char *) addr, count);
697 static void dma_setup_quick(struct NCR_ESP * esp, __u32 addr, int count, int write)
699 #ifdef DEBUG_MAC_ESP
700 printk("mac_esp: dma_setup_quick\n");
701 #endif