File: /usr/src/linux/drivers/scsi/mac_esp.c
1 /*
2 * 68k mac 53c9[46] scsi driver
3 *
4 * copyright (c) 1998, David Weis weisd3458@uni.edu
5 *
6 * debugging on Quadra 800 and 660AV Michael Schmitz, Dave Kilzer 7/98
7 *
8 * based loosely on cyber_esp.c
9 */
10
11 /* these are unused for now */
12 #define myreadl(addr) (*(volatile unsigned int *) (addr))
13 #define mywritel(b, addr) ((*(volatile unsigned int *) (addr)) = (b))
14
15
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/blk.h>
23 #include <linux/proc_fs.h>
24 #include <linux/stat.h>
25 #include <linux/init.h>
26
27 #include "scsi.h"
28 #include "hosts.h"
29 #include "NCR53C9x.h"
30 #include "mac_esp.h"
31
32 #include <asm/io.h>
33
34 #include <asm/setup.h>
35 #include <asm/irq.h>
36 #include <asm/macints.h>
37 #include <asm/machw.h>
38 #include <asm/mac_via.h>
39
40 #include <asm/pgtable.h>
41
42 #include <asm/macintosh.h>
43
44 #define mac_turnon_irq(x) mac_enable_irq(x)
45 #define mac_turnoff_irq(x) mac_disable_irq(x)
46
47 extern inline void esp_handle(struct NCR_ESP *esp);
48 extern void mac_esp_intr(int irq, void *dev_id, struct pt_regs *pregs);
49
50 static int dma_bytes_sent(struct NCR_ESP * esp, int fifo_count);
51 static int dma_can_transfer(struct NCR_ESP * esp, Scsi_Cmnd *sp);
52 static void dma_dump_state(struct NCR_ESP * esp);
53 static void dma_init_read(struct NCR_ESP * esp, char * vaddress, int length);
54 static void dma_init_write(struct NCR_ESP * esp, char * vaddress, int length);
55 static void dma_ints_off(struct NCR_ESP * esp);
56 static void dma_ints_on(struct NCR_ESP * esp);
57 static int dma_irq_p(struct NCR_ESP * esp);
58 static int dma_irq_p_quick(struct NCR_ESP * esp);
59 static void dma_led_off(struct NCR_ESP * esp);
60 static void dma_led_on(struct NCR_ESP *esp);
61 static int dma_ports_p(struct NCR_ESP *esp);
62 static void dma_setup(struct NCR_ESP * esp, __u32 addr, int count, int write);
63 static void dma_setup_quick(struct NCR_ESP * esp, __u32 addr, int count, int write);
64
65 static int esp_dafb_dma_irq_p(struct NCR_ESP * espdev);
66 static int esp_iosb_dma_irq_p(struct NCR_ESP * espdev);
67
68 volatile unsigned char cmd_buffer[16];
69 /* This is where all commands are put
70 * before they are transferred to the ESP chip
71 * via PIO.
72 */
73
74 static int esp_initialized = 0;
75
76 static int setup_num_esps = -1;
77 static int setup_disconnect = -1;
78 static int setup_nosync = -1;
79 static int setup_can_queue = -1;
80 static int setup_cmd_per_lun = -1;
81 static int setup_sg_tablesize = -1;
82 #ifdef SUPPORT_TAGS
83 static int setup_use_tagged_queuing = -1;
84 #endif
85 static int setup_hostid = -1;
86
87 /*
88 * Experimental ESP inthandler; check macints.c to make sure dev_id is
89 * set up properly!
90 */
91
92 void mac_esp_intr(int irq, void *dev_id, struct pt_regs *pregs)
93 {
94 struct NCR_ESP *esp = (struct NCR_ESP *) dev_id;
95 int irq_p = 0;
96
97 /* Handle the one ESP interrupt showing at this IRQ level. */
98 if(((esp)->irq & 0xff) == irq) {
99 /*
100 * Debug ..
101 */
102 irq_p = esp->dma_irq_p(esp);
103 printk("mac_esp: irq_p %x current %p disconnected %p\n",
104 irq_p, esp->current_SC, esp->disconnected_SC);
105
106 /*
107 * Mac: if we're here, it's an ESP interrupt for sure!
108 */
109 if((esp->current_SC || esp->disconnected_SC)) {
110 esp->dma_ints_off(esp);
111
112 ESPIRQ(("I%d(", esp->esp_id));
113 esp_handle(esp);
114 ESPIRQ((")"));
115
116 esp->dma_ints_on(esp);
117 }
118 }
119 }
120
121 /*
122 * Debug hooks; use for playing with the interrupt flag testing and interrupt
123 * acknowledge on the various machines
124 */
125
126 void scsi_esp_polled(int irq, void *dev_id, struct pt_regs *pregs)
127 {
128 if (esp_initialized == 0)
129 return;
130
131 mac_esp_intr(irq, dev_id, pregs);
132 }
133
134 void fake_intr(int irq, void *dev_id, struct pt_regs *pregs)
135 {
136 #ifdef DEBUG_MAC_ESP
137 printk("mac_esp: got irq\n");
138 #endif
139
140 mac_esp_intr(irq, dev_id, pregs);
141 }
142
143 void fake_drq(int irq, void *dev_id, struct pt_regs *pregs)
144 {
145 printk("mac_esp: got drq\n");
146 }
147
148 #define DRIVER_SETUP
149
150 /*
151 * Function : mac_esp_setup(char *str, int *ints)
152 *
153 * Purpose : booter command line initialization of the overrides array,
154 *
155 * Inputs : str - unused, ints - array of integer parameters with ints[0]
156 * equal to the number of ints.
157 *
158 * Currently unused in the new driver; need to add settable parameters to the
159 * detect function.
160 *
161 */
162
163 static int __init mac_esp_setup(char *str, int *ints) {
164 #ifdef DRIVER_SETUP
165 /* Format of mac53c9x parameter is:
166 * mac53c9x=<num_esps>,<disconnect>,<nosync>,<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
167 * Negative values mean don't change.
168 */
169
170 /* Grmbl... the standard parameter parsing can't handle negative numbers
171 * :-( So let's do it ourselves!
172 */
173
174 int i = ints[0]+1, fact;
175
176 while( str && (isdigit(*str) || *str == '-') && i <= 10) {
177 if (*str == '-')
178 fact = -1, ++str;
179 else
180 fact = 1;
181 ints[i++] = simple_strtoul( str, NULL, 0 ) * fact;
182 if ((str = strchr( str, ',' )) != NULL)
183 ++str;
184 }
185 ints[0] = i-1;
186
187 if (ints[0] < 1) {
188 printk( "mac_esp_setup: no arguments!\n" );
189 return 0;
190 }
191
192 if (ints[0] >= 1) {
193 if (ints[1] > 0)
194 /* no limits on this, just > 0 */
195 if (ints[1] >= 0 && ints[1] <= 2)
196 setup_num_esps = ints[1];
197 else if (ints[1] > 2)
198 printk( "mac_esp_setup: invalid number of hosts %d !\n", ints[1] );
199 }
200 if (ints[0] >= 2) {
201 if (ints[2] > 0)
202 setup_disconnect = ints[2];
203 }
204 if (ints[0] >= 3) {
205 if (ints[3] >= 0) {
206 setup_nosync = ints[3];
207 }
208 }
209 if (ints[0] >= 4) {
210 if (ints[4] > 0)
211 /* no limits on this, just > 0 */
212 setup_can_queue = ints[4];
213 }
214 if (ints[0] >= 5) {
215 if (ints[5] > 0)
216 setup_cmd_per_lun = ints[5];
217 }
218 if (ints[0] >= 6) {
219 if (ints[6] >= 0) {
220 setup_sg_tablesize = ints[6];
221 /* Must be <= SG_ALL (255) */
222 if (setup_sg_tablesize > SG_ALL)
223 setup_sg_tablesize = SG_ALL;
224 }
225 }
226 if (ints[0] >= 7) {
227 /* Must be between 0 and 7 */
228 if (ints[7] >= 0 && ints[7] <= 7)
229 setup_hostid = ints[7];
230 else if (ints[7] > 7)
231 printk( "mac_esp_setup: invalid host ID %d !\n", ints[7] );
232 }
233 #ifdef SUPPORT_TAGS
234 if (ints[0] >= 8) {
235 if (ints[8] >= 0)
236 setup_use_tagged_queuing = !!ints[8];
237 }
238 #endif
239 #endif
240 return 1;
241 }
242
243 __setup("mac53c9x=", mac_esp_setup);
244
245 /*
246 * ESP address 'detection'
247 */
248
249 unsigned long get_base(int chip_num)
250 {
251 /*
252 * using the chip_num and mac model, figure out where the
253 * chips are mapped
254 */
255
256 unsigned long io_base = 0x50f00000;
257 unsigned int second_offset = 0x402;
258 unsigned long scsi_loc = 0;
259
260 switch (macintosh_config->scsi_type) {
261
262 /* 950, 900, 700 */
263 case MAC_SCSI_QUADRA2:
264 scsi_loc = io_base + 0xf000 + ((chip_num == 0) ? 0 : second_offset);
265 break;
266
267 /* av's */
268 case MAC_SCSI_QUADRA3:
269 scsi_loc = io_base + 0x18000 + ((chip_num == 0) ? 0 : second_offset);
270 break;
271
272 /* most quadra/centris models are like this */
273 case MAC_SCSI_QUADRA:
274 scsi_loc = io_base + 0x10000;
275 break;
276
277 default:
278 printk("mac_esp: get_base: hit default!\n");
279 scsi_loc = io_base + 0x10000;
280 break;
281
282 } /* switch */
283
284 printk("mac_esp: io base at 0x%lx\n", scsi_loc);
285
286 return scsi_loc;
287 }
288
289 /*
290 * Model dependent ESP setup
291 */
292
293 int mac_esp_detect(Scsi_Host_Template * tpnt)
294 {
295 int quick = 0;
296 int chipnum, chipspresent = 0;
297 #if 0
298 unsigned long timeout;
299 #endif
300
301 if (esp_initialized > 0)
302 return -ENODEV;
303
304 /* what do we have in this machine... */
305 if (MACHW_PRESENT(MAC_SCSI_96)) {
306 chipspresent ++;
307 }
308
309 if (MACHW_PRESENT(MAC_SCSI_96_2)) {
310 chipspresent ++;
311 }
312
313 /* number of ESPs present ? */
314 if (setup_num_esps >= 0) {
315 if (chipspresent >= setup_num_esps)
316 chipspresent = setup_num_esps;
317 else
318 printk("mac_esp_detect: num_hosts detected %d setup %d \n",
319 chipspresent, setup_num_esps);
320 }
321
322 /* TODO: add disconnect / nosync flags */
323
324 /* setup variables */
325 tpnt->can_queue =
326 (setup_can_queue > 0) ? setup_can_queue : 7;
327 tpnt->cmd_per_lun =
328 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : 1;
329 tpnt->sg_tablesize =
330 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_ALL;
331
332 if (setup_hostid >= 0)
333 tpnt->this_id = setup_hostid;
334 else {
335 /* use 7 as default */
336 tpnt->this_id = 7;
337 }
338
339 #ifdef SUPPORT_TAGS
340 if (setup_use_tagged_queuing < 0)
341 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
342 #endif
343
344 for (chipnum = 0; chipnum < chipspresent; chipnum ++) {
345 struct NCR_ESP * esp;
346
347 esp = esp_allocate(tpnt, (void *) NULL);
348 esp->eregs = (struct ESP_regs *) get_base(chipnum);
349
350 esp->dma_irq_p = &esp_dafb_dma_irq_p;
351 if (chipnum == 0) {
352
353 if (macintosh_config->scsi_type == MAC_SCSI_QUADRA) {
354 /* most machines except those below :-) */
355 quick = 1;
356 esp->dma_irq_p = &esp_iosb_dma_irq_p;
357 } else if (macintosh_config->scsi_type == MAC_SCSI_QUADRA3) {
358 /* mostly av's */
359 quick = 0;
360 } else {
361 /* q950, 900, 700 */
362 quick = 1;
363 writel(0x1d1, 0xf9800024);
364 esp->dregs = (void *) 0xf9800024;
365 }
366
367 } else { /* chipnum */
368
369 quick = 1;
370 writel(0x1d1, 0xf9800028);
371 esp->dregs = (void *) 0xf9800028;
372
373 } /* chipnum == 0 */
374
375 /* use pio for command bytes; pio for message/data: TBI */
376 esp->do_pio_cmds = 1;
377
378 /* Set the command buffer */
379 esp->esp_command = (volatile unsigned char*) cmd_buffer;
380 esp->esp_command_dvma = (volatile unsigned char*) cmd_buffer;
381
382 /* various functions */
383 esp->dma_bytes_sent = &dma_bytes_sent;
384 esp->dma_can_transfer = &dma_can_transfer;
385 esp->dma_dump_state = &dma_dump_state;
386 esp->dma_init_read = NULL;
387 esp->dma_init_write = NULL;
388 esp->dma_ints_off = &dma_ints_off;
389 esp->dma_ints_on = &dma_ints_on;
390
391 esp->dma_ports_p = &dma_ports_p;
392
393
394 /* Optional functions */
395 esp->dma_barrier = NULL;
396 esp->dma_drain = NULL;
397 esp->dma_invalidate = NULL;
398 esp->dma_irq_entry = NULL;
399 esp->dma_irq_exit = NULL;
400 esp->dma_led_on = NULL;
401 esp->dma_led_off = NULL;
402 esp->dma_poll = NULL;
403 esp->dma_reset = NULL;
404
405 /* SCSI chip speed */
406 /* below esp->cfreq = 40000000; */
407
408
409 if (quick) {
410 /* 'quick' means there's handshake glue logic like in the 5380 case */
411 esp->dma_setup = &dma_setup_quick;
412 } else {
413 esp->dma_setup = &dma_setup;
414 }
415
416 if (chipnum == 0) {
417
418 esp->irq = IRQ_MAC_SCSI;
419
420 request_irq(IRQ_MAC_SCSI, esp_intr, 0, "Mac ESP SCSI", esp);
421 #if 0 /* conflicts with IOP ADB */
422 request_irq(IRQ_MAC_SCSIDRQ, fake_drq, 0, "Mac ESP DRQ", esp);
423 #endif
424
425 if (macintosh_config->scsi_type == MAC_SCSI_QUADRA) {
426 esp->cfreq = 16500000;
427 } else {
428 esp->cfreq = 25000000;
429 }
430
431
432 } else { /* chipnum == 1 */
433
434 esp->irq = IRQ_MAC_SCSIDRQ;
435 #if 0 /* conflicts with IOP ADB */
436 request_irq(IRQ_MAC_SCSIDRQ, esp_intr, 0, "Mac ESP SCSI 2", esp);
437 #endif
438
439 esp->cfreq = 25000000;
440
441 }
442
443 if (quick) {
444 printk("esp: using quick version\n");
445 }
446
447 printk("esp: addr at 0x%p\n", esp->eregs);
448
449 esp->scsi_id = 7;
450 esp->diff = 0;
451
452 esp_initialize(esp);
453
454 } /* for chipnum */
455
456 if (chipspresent)
457 printk("\nmac_esp: %d esp controllers found\n", chipspresent);
458
459 esp_initialized = chipspresent;
460
461 return chipspresent;
462 }
463
464 /*
465 * I've been wondering what this is supposed to do, for some time. Talking
466 * to Allen Briggs: These machines have an extra register someplace where the
467 * DRQ pin of the ESP can be monitored. That isn't useful for determining
468 * anything else (such as reselect interrupt or other magic) though.
469 * Maybe make the semantics should be changed like
470 * if (esp->current_SC)
471 * ... check DRQ flag ...
472 * else
473 * ... disconnected, check pending VIA interrupt ...
474 *
475 * There's a problem with using the dabf flag or mac_irq_pending() here: both
476 * seem to return 1 even though no interrupt is currently pending, resulting
477 * in esp_exec_cmd() holding off the next command, and possibly infinite loops
478 * in esp_intr().
479 * Short term fix: just use esp_status & ESP_STAT_INTR here, as long as we
480 * use simple PIO. The DRQ status will be important when implementing pseudo
481 * DMA mode (set up ESP transfer count, return, do a batch of bytes in PIO or
482 * 'hardware handshake' mode upon DRQ).
483 * If you plan on changing this (i.e. to save the esp_status register access in
484 * favor of a VIA register access or a shadow register for the IFR), make sure
485 * to try a debug version of this first to monitor what registers would be a good
486 * indicator of the ESP interrupt.
487 */
488
489 static int esp_dafb_dma_irq_p(struct NCR_ESP * esp)
490 {
491 unsigned int ret;
492 int sreg = esp_read(esp->eregs->esp_status);
493
494 #ifdef DEBUG_MAC_ESP
495 printk("mac_esp: esp_dafb_dma_irq_p dafb %d irq %d\n",
496 readl(esp->dregs), mac_irq_pending(IRQ_MAC_SCSI));
497 #endif
498
499 sreg &= ESP_STAT_INTR;
500
501 /*
502 * maybe working; this is essentially what's used for iosb_dma_irq_p
503 */
504 if (sreg)
505 return 1;
506 else
507 return 0;
508
509 /*
510 * didn't work ...
511 */
512 #if 0
513 if (esp->current_SC)
514 ret = readl(esp->dregs) & 0x200;
515 else if (esp->disconnected_SC)
516 ret = 1; /* sreg ?? */
517 else
518 ret = mac_irq_pending(IRQ_MAC_SCSI);
519
520 return(ret);
521 #endif
522
523 }
524
525 /*
526 * See above: testing mac_irq_pending always returned 8 (SCSI IRQ) regardless
527 * of the actual ESP status.
528 */
529
530 static int esp_iosb_dma_irq_p(struct NCR_ESP * esp)
531 {
532 int ret = mac_irq_pending(IRQ_MAC_SCSI) || mac_irq_pending(IRQ_MAC_SCSIDRQ);
533 int sreg = esp_read(esp->eregs->esp_status);
534
535 #ifdef DEBUG_MAC_ESP
536 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
537 mac_irq_pending(IRQ_MAC_SCSIDRQ), mac_irq_pending(IRQ_MAC_SCSI),
538 sreg, esp->current_SC, esp->disconnected_SC);
539 #endif
540
541 sreg &= ESP_STAT_INTR;
542
543 if (sreg)
544 return (sreg);
545 else
546 return 0;
547 }
548
549 /*
550 * This seems to be OK for PIO at least ... usually 0 after PIO.
551 */
552
553 static int dma_bytes_sent(struct NCR_ESP * esp, int fifo_count)
554 {
555
556 #ifdef DEBUG_MAC_ESP
557 printk("mac_esp: dma bytes sent = %x\n", fifo_count);
558 #endif
559
560 return fifo_count;
561 }
562
563 /*
564 * dma_can_transfer is used to switch between DMA and PIO, if DMA (pseudo)
565 * is ever implemented. Returning 0 here will use PIO.
566 */
567
568 static int dma_can_transfer(struct NCR_ESP * esp, Scsi_Cmnd * sp)
569 {
570 unsigned long sz = sp->SCp.this_residual;
571 #if 0 /* no DMA yet; make conditional */
572 if (sz > 0x10000000) {
573 sz = 0x10000000;
574 }
575 printk("mac_esp: dma can transfer = 0lx%x\n", sz);
576 #else
577
578 #ifdef DEBUG_MAC_ESP
579 printk("mac_esp: pio to transfer = %ld\n", sz);
580 #endif
581
582 sz = 0;
583 #endif
584 return sz;
585 }
586
587 /*
588 * Not yet ...
589 */
590
591 static void dma_dump_state(struct NCR_ESP * esp)
592 {
593 #ifdef DEBUG_MAC_ESP
594 printk("mac_esp: dma_dump_state: called\n");
595 #endif
596 #if 0
597 ESPLOG(("esp%d: dma -- cond_reg<%02x>\n",
598 esp->esp_id, ((struct mac_dma_registers *)
599 (esp->dregs))->cond_reg));
600 #endif
601 }
602
603 /*
604 * DMA setup: should be used to set up the ESP transfer count for pseudo
605 * DMA transfers; need a DRQ transfer function to do the actual transfer
606 */
607
608 static void dma_init_read(struct NCR_ESP * esp, char * vaddress, int length)
609 {
610 printk("mac_esp: dma_init_read\n");
611 }
612
613
614 static void dma_init_write(struct NCR_ESP * esp, char * vaddress, int length)
615 {
616 printk("mac_esp: dma_init_write\n");
617 }
618
619
620 static void dma_ints_off(struct NCR_ESP * esp)
621 {
622 mac_turnoff_irq(esp->irq);
623 }
624
625
626 static void dma_ints_on(struct NCR_ESP * esp)
627 {
628 mac_turnon_irq(esp->irq);
629 }
630
631 /*
632 * generic dma_irq_p(), unused
633 */
634
635 static int dma_irq_p(struct NCR_ESP * esp)
636 {
637 int i = esp_read(esp->eregs->esp_status);
638
639 #ifdef DEBUG_MAC_ESP
640 printk("mac_esp: dma_irq_p status %d\n", i);
641 #endif
642
643 return (i & ESP_STAT_INTR);
644 }
645
646 static int dma_irq_p_quick(struct NCR_ESP * esp)
647 {
648 /*
649 * Copied from iosb_dma_irq_p()
650 */
651 int ret = mac_irq_pending(IRQ_MAC_SCSI) || mac_irq_pending(IRQ_MAC_SCSIDRQ);
652 int sreg = esp_read(esp->eregs->esp_status);
653
654 #ifdef DEBUG_MAC_ESP
655 printk("mac_esp: dma_irq_p drq %d irq %d sreg %x curr %p disc %p\n",
656 mac_irq_pending(IRQ_MAC_SCSIDRQ), mac_irq_pending(IRQ_MAC_SCSI),
657 sreg, esp->current_SC, esp->disconnected_SC);
658 #endif
659
660 sreg &= ESP_STAT_INTR;
661
662 if (sreg)
663 return (sreg);
664 else
665 return 0;
666
667 }
668
669 static void dma_led_off(struct NCR_ESP * esp)
670 {
671 #ifdef DEBUG_MAC_ESP
672 printk("mac_esp: dma_led_off: called\n");
673 #endif
674 }
675
676
677 static void dma_led_on(struct NCR_ESP * esp)
678 {
679 #ifdef DEBUG_MAC_ESP
680 printk("mac_esp: dma_led_on: called\n");
681 #endif
682 }
683
684
685 static int dma_ports_p(struct NCR_ESP * esp)
686 {
687 return 0;
688 }
689
690
691 static void dma_setup(struct NCR_ESP * esp, __u32 addr, int count, int write)
692 {
693
694 #ifdef DEBUG_MAC_ESP
695 printk("mac_esp: dma_setup\n");
696 #endif
697
698 if (write) {
699 dma_init_read(esp, (char *) addr, count);
700 } else {
701 dma_init_write(esp, (char *) addr, count);
702 }
703 }
704
705
706 static void dma_setup_quick(struct NCR_ESP * esp, __u32 addr, int count, int write)
707 {
708 #ifdef DEBUG_MAC_ESP
709 printk("mac_esp: dma_setup_quick\n");
710 #endif
711 }
712
713 static Scsi_Host_Template driver_template = SCSI_MAC_ESP;
714
715 #include "scsi_module.c"
716