ide/legacy/q40ide.c: add MODULE_LICENSE
[linux-2.6] / drivers / scsi / mac_scsi.c
1 /*
2  * Generic Macintosh NCR5380 driver
3  *
4  * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
5  *
6  * derived in part from:
7  */
8 /*
9  * Generic Generic NCR5380 driver
10  *
11  * Copyright 1995, Russell King
12  *
13  * ALPHA RELEASE 1.
14  *
15  * For more information, please consult
16  *
17  * NCR 5380 Family
18  * SCSI Protocol Controller
19  * Databook
20  *
21  * NCR Microelectronics
22  * 1635 Aeroplaza Drive
23  * Colorado Springs, CO 80916
24  * 1+ (719) 578-3400
25  * 1+ (800) 334-5454
26  */
27
28 /*
29  * $Log: mac_NCR5380.c,v $
30  */
31
32 #include <linux/types.h>
33 #include <linux/stddef.h>
34 #include <linux/ctype.h>
35 #include <linux/delay.h>
36
37 #include <linux/module.h>
38 #include <linux/signal.h>
39 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/blkdev.h>
42 #include <linux/interrupt.h>
43
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47
48 #include <asm/macintosh.h>
49 #include <asm/macints.h>
50 #include <asm/machw.h>
51 #include <asm/mac_via.h>
52
53 #include "scsi.h"
54 #include <scsi/scsi_host.h>
55 #include "mac_scsi.h"
56
57 /* These control the behaviour of the generic 5380 core */
58 #define AUTOSENSE
59 #define PSEUDO_DMA
60
61 #include "NCR5380.h"
62
63 #if 0
64 #define NDEBUG (NDEBUG_INTR | NDEBUG_PSEUDO_DMA | NDEBUG_ARBITRATION | NDEBUG_SELECTION | NDEBUG_RESELECTION)
65 #else
66 #define NDEBUG (NDEBUG_ABORT)
67 #endif
68
69 #define RESET_BOOT
70 #define DRIVER_SETUP
71
72 extern void via_scsi_clear(void);
73
74 #ifdef RESET_BOOT
75 static void mac_scsi_reset_boot(struct Scsi_Host *instance);
76 #endif
77
78 static int setup_called = 0;
79 static int setup_can_queue = -1;
80 static int setup_cmd_per_lun = -1;
81 static int setup_sg_tablesize = -1;
82 static int setup_use_pdma = -1;
83 #ifdef SUPPORT_TAGS
84 static int setup_use_tagged_queuing = -1;
85 #endif
86 static int setup_hostid = -1;
87
88 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
89  * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
90  * need ten times the standard value... */
91 #define TOSHIBA_DELAY
92
93 #ifdef TOSHIBA_DELAY
94 #define AFTER_RESET_DELAY       (5*HZ/2)
95 #else
96 #define AFTER_RESET_DELAY       (HZ/2)
97 #endif
98
99 static volatile unsigned char *mac_scsi_regp = NULL;
100 static volatile unsigned char *mac_scsi_drq  = NULL;
101 static volatile unsigned char *mac_scsi_nodrq = NULL;
102
103
104 /*
105  * NCR 5380 register access functions
106  */
107
108 #if 0
109 /* Debug versions */
110 #define CTRL(p,v) (*ctrl = (v))
111
112 static char macscsi_read(struct Scsi_Host *instance, int reg)
113 {
114   int iobase = instance->io_port;
115   int i;
116   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
117
118   CTRL(iobase, 0);
119   i = in_8(iobase + (reg<<4));
120   CTRL(iobase, 0x40);
121
122   return i;
123 }
124
125 static void macscsi_write(struct Scsi_Host *instance, int reg, int value)
126 {
127   int iobase = instance->io_port;
128   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
129
130   CTRL(iobase, 0);
131   out_8(iobase + (reg<<4), value);
132   CTRL(iobase, 0x40);
133 }
134 #else
135
136 /* Fast versions */
137 static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
138 {
139   return in_8(instance->io_port + (reg<<4));
140 }
141
142 static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
143 {
144   out_8(instance->io_port + (reg<<4), value);
145 }
146 #endif
147
148
149 /*
150  * Function : mac_scsi_setup(char *str)
151  *
152  * Purpose : booter command line initialization of the overrides array,
153  *
154  * Inputs : str - comma delimited list of options
155  *
156  */
157
158 static int __init mac_scsi_setup(char *str) {
159 #ifdef DRIVER_SETUP     
160         int ints[7];
161         
162         (void)get_options( str, ARRAY_SIZE(ints), ints);
163         
164         if (setup_called++ || ints[0] < 1 || ints[0] > 6) {
165             printk(KERN_WARNING "scsi: <mac5380>"
166                 " Usage: mac5380=<can_queue>[,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>,<use_pdma>]\n");
167             printk(KERN_ALERT "scsi: <mac5380> Bad Penguin parameters?\n");
168             return 0;
169         }
170             
171         if (ints[0] >= 1) {
172                 if (ints[1] > 0)
173                         /* no limits on this, just > 0 */
174                         setup_can_queue = ints[1];
175         }
176         if (ints[0] >= 2) {
177                 if (ints[2] > 0)
178                         setup_cmd_per_lun = ints[2];
179         }
180         if (ints[0] >= 3) {
181                 if (ints[3] >= 0) {
182                         setup_sg_tablesize = ints[3];
183                         /* Must be <= SG_ALL (255) */
184                         if (setup_sg_tablesize > SG_ALL)
185                                 setup_sg_tablesize = SG_ALL;
186                 }
187         }
188         if (ints[0] >= 4) {
189                 /* Must be between 0 and 7 */
190                 if (ints[4] >= 0 && ints[4] <= 7)
191                         setup_hostid = ints[4];
192                 else if (ints[4] > 7)
193                         printk(KERN_WARNING "mac_scsi_setup: invalid host ID %d !\n", ints[4] );
194         }
195 #ifdef SUPPORT_TAGS     
196         if (ints[0] >= 5) {
197                 if (ints[5] >= 0)
198                         setup_use_tagged_queuing = !!ints[5];
199         }
200         
201         if (ints[0] == 6) {
202             if (ints[6] >= 0)
203                 setup_use_pdma = ints[6];
204         }
205 #else
206         if (ints[0] == 5) {
207             if (ints[5] >= 0)
208                 setup_use_pdma = ints[5];
209         }
210 #endif /* SUPPORT_TAGS */
211         
212 #endif /* DRIVER_SETUP */
213         return 1;
214 }
215
216 __setup("mac5380=", mac_scsi_setup);
217
218 /*
219  * If you want to find the instance with (k)gdb ...
220  */
221 #if NDEBUG
222 static struct Scsi_Host *default_instance;
223 #endif
224
225 /*
226  * Function : int macscsi_detect(struct scsi_host_template * tpnt)
227  *
228  * Purpose : initializes mac NCR5380 driver based on the
229  *      command line / compile time port and irq definitions.
230  *
231  * Inputs : tpnt - template for this SCSI adapter.
232  *
233  * Returns : 1 if a host adapter was found, 0 if not.
234  *
235  */
236  
237 int macscsi_detect(struct scsi_host_template * tpnt)
238 {
239     static int called = 0;
240     int flags = 0;
241     struct Scsi_Host *instance;
242
243     if (!MACH_IS_MAC || called)
244         return( 0 );
245
246     if (macintosh_config->scsi_type != MAC_SCSI_OLD)
247         return( 0 );
248
249     /* setup variables */
250     tpnt->can_queue =
251         (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
252     tpnt->cmd_per_lun =
253         (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
254     tpnt->sg_tablesize = 
255         (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
256
257     if (setup_hostid >= 0)
258         tpnt->this_id = setup_hostid;
259     else {
260         /* use 7 as default */
261         tpnt->this_id = 7;
262     }
263
264 #ifdef SUPPORT_TAGS
265     if (setup_use_tagged_queuing < 0)
266         setup_use_tagged_queuing = USE_TAGGED_QUEUING;
267 #endif
268
269     /* Once we support multiple 5380s (e.g. DuoDock) we'll do
270        something different here */
271     instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
272 #if NDEBUG
273     default_instance = instance;
274 #endif
275     
276     if (macintosh_config->ident == MAC_MODEL_IIFX) {
277         mac_scsi_regp  = via1+0x8000;
278         mac_scsi_drq   = via1+0xE000;
279         mac_scsi_nodrq = via1+0xC000;
280         /* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
281         flags = FLAG_NO_PSEUDO_DMA;
282     } else {
283         mac_scsi_regp  = via1+0x10000;
284         mac_scsi_drq   = via1+0x6000;
285         mac_scsi_nodrq = via1+0x12000;
286     }
287
288     if (! setup_use_pdma)
289         flags = FLAG_NO_PSEUDO_DMA;
290         
291     instance->io_port = (unsigned long) mac_scsi_regp;
292     instance->irq = IRQ_MAC_SCSI;
293
294 #ifdef RESET_BOOT   
295     mac_scsi_reset_boot(instance);
296 #endif
297     
298     NCR5380_init(instance, flags);
299
300     instance->n_io_port = 255;
301
302     ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
303
304     if (instance->irq != SCSI_IRQ_NONE)
305         if (request_irq(instance->irq, NCR5380_intr, IRQ_FLG_SLOW, 
306                         "ncr5380", instance)) {
307             printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
308                    instance->host_no, instance->irq);
309             instance->irq = SCSI_IRQ_NONE;
310         }
311
312     printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
313     if (instance->irq == SCSI_IRQ_NONE)
314         printk (KERN_INFO "s disabled");
315     else
316         printk (KERN_INFO " %d", instance->irq);
317     printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
318            instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
319     printk(KERN_INFO "\nscsi%d:", instance->host_no);
320     NCR5380_print_options(instance);
321     printk("\n");
322     called = 1;
323     return 1;
324 }
325
326 int macscsi_release (struct Scsi_Host *shpnt)
327 {
328         if (shpnt->irq != SCSI_IRQ_NONE)
329                 free_irq(shpnt->irq, shpnt);
330         NCR5380_exit(shpnt);
331
332         return 0;
333 }
334
335 #ifdef RESET_BOOT
336 /*
337  * Our 'bus reset on boot' function
338  */
339
340 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
341 {
342         unsigned long end;
343
344         NCR5380_local_declare();
345         NCR5380_setup(instance);
346         
347         /*
348          * Do a SCSI reset to clean up the bus during initialization. No messing
349          * with the queues, interrupts, or locks necessary here.
350          */
351
352         printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
353
354         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
355         disable_irq(IRQ_MAC_SCSI);
356
357         /* get in phase */
358         NCR5380_write( TARGET_COMMAND_REG,
359                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
360
361         /* assert RST */
362         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
363         /* The min. reset hold time is 25us, so 40us should be enough */
364         udelay( 50 );
365         /* reset RST and interrupt */
366         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
367         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
368
369         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
370                 barrier();
371
372         /* switch on SCSI IRQ again */
373         enable_irq(IRQ_MAC_SCSI);
374
375         printk(KERN_INFO " done\n" );
376 }
377 #endif
378
379 const char * macscsi_info (struct Scsi_Host *spnt) {
380         return "";
381 }
382
383 /* 
384    Pseudo-DMA: (Ove Edlund)
385    The code attempts to catch bus errors that occur if one for example
386    "trips over the cable".
387    XXX: Since bus errors in the PDMA routines never happen on my 
388    computer, the bus error code is untested. 
389    If the code works as intended, a bus error results in Pseudo-DMA 
390    beeing disabled, meaning that the driver switches to slow handshake. 
391    If bus errors are NOT extremely rare, this has to be changed. 
392 */
393
394 #define CP_IO_TO_MEM(s,d,len)                           \
395 __asm__ __volatile__                                    \
396     ("    cmp.w  #4,%2\n"                               \
397      "    bls    8f\n"                                  \
398      "    move.w %1,%%d0\n"                             \
399      "    neg.b  %%d0\n"                                \
400      "    and.w  #3,%%d0\n"                             \
401      "    sub.w  %%d0,%2\n"                             \
402      "    bra    2f\n"                                  \
403      " 1: move.b (%0),(%1)+\n"                          \
404      " 2: dbf    %%d0,1b\n"                             \
405      "    move.w %2,%%d0\n"                             \
406      "    lsr.w  #5,%%d0\n"                             \
407      "    bra    4f\n"                                  \
408      " 3: move.l (%0),(%1)+\n"                          \
409      "31: move.l (%0),(%1)+\n"                          \
410      "32: move.l (%0),(%1)+\n"                          \
411      "33: move.l (%0),(%1)+\n"                          \
412      "34: move.l (%0),(%1)+\n"                          \
413      "35: move.l (%0),(%1)+\n"                          \
414      "36: move.l (%0),(%1)+\n"                          \
415      "37: move.l (%0),(%1)+\n"                          \
416      " 4: dbf    %%d0,3b\n"                             \
417      "    move.w %2,%%d0\n"                             \
418      "    lsr.w  #2,%%d0\n"                             \
419      "    and.w  #7,%%d0\n"                             \
420      "    bra    6f\n"                                  \
421      " 5: move.l (%0),(%1)+\n"                          \
422      " 6: dbf    %%d0,5b\n"                             \
423      "    and.w  #3,%2\n"                               \
424      "    bra    8f\n"                                  \
425      " 7: move.b (%0),(%1)+\n"                          \
426      " 8: dbf    %2,7b\n"                               \
427      "    moveq.l #0, %2\n"                             \
428      " 9: \n"                                           \
429      ".section .fixup,\"ax\"\n"                         \
430      "    .even\n"                                      \
431      "90: moveq.l #1, %2\n"                             \
432      "    jra 9b\n"                                     \
433      ".previous\n"                                      \
434      ".section __ex_table,\"a\"\n"                      \
435      "   .align 4\n"                                    \
436      "   .long  1b,90b\n"                               \
437      "   .long  3b,90b\n"                               \
438      "   .long 31b,90b\n"                               \
439      "   .long 32b,90b\n"                               \
440      "   .long 33b,90b\n"                               \
441      "   .long 34b,90b\n"                               \
442      "   .long 35b,90b\n"                               \
443      "   .long 36b,90b\n"                               \
444      "   .long 37b,90b\n"                               \
445      "   .long  5b,90b\n"                               \
446      "   .long  7b,90b\n"                               \
447      ".previous"                                        \
448      : "=a"(s), "=a"(d), "=d"(len)                      \
449      : "0"(s), "1"(d), "2"(len)                         \
450      : "d0")
451
452
453 static int macscsi_pread (struct Scsi_Host *instance,
454                           unsigned char *dst, int len)
455 {
456    unsigned char *d;
457    volatile unsigned char *s;
458
459    NCR5380_local_declare();
460    NCR5380_setup(instance);
461
462    s = mac_scsi_drq+0x60;
463    d = dst;
464
465 /* These conditions are derived from MacOS */
466
467    while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
468          && !(NCR5380_read(STATUS_REG) & SR_REQ))
469       ;
470    if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
471          && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
472       printk(KERN_ERR "Error in macscsi_pread\n");
473       return -1;
474    }
475
476    CP_IO_TO_MEM(s, d, len);
477    
478    if (len != 0) {
479       printk(KERN_NOTICE "Bus error in macscsi_pread\n");
480       return -1;
481    }
482    
483    return 0;
484 }
485
486
487 #define CP_MEM_TO_IO(s,d,len)                           \
488 __asm__ __volatile__                                    \
489     ("    cmp.w  #4,%2\n"                               \
490      "    bls    8f\n"                                  \
491      "    move.w %0,%%d0\n"                             \
492      "    neg.b  %%d0\n"                                \
493      "    and.w  #3,%%d0\n"                             \
494      "    sub.w  %%d0,%2\n"                             \
495      "    bra    2f\n"                                  \
496      " 1: move.b (%0)+,(%1)\n"                          \
497      " 2: dbf    %%d0,1b\n"                             \
498      "    move.w %2,%%d0\n"                             \
499      "    lsr.w  #5,%%d0\n"                             \
500      "    bra    4f\n"                                  \
501      " 3: move.l (%0)+,(%1)\n"                          \
502      "31: move.l (%0)+,(%1)\n"                          \
503      "32: move.l (%0)+,(%1)\n"                          \
504      "33: move.l (%0)+,(%1)\n"                          \
505      "34: move.l (%0)+,(%1)\n"                          \
506      "35: move.l (%0)+,(%1)\n"                          \
507      "36: move.l (%0)+,(%1)\n"                          \
508      "37: move.l (%0)+,(%1)\n"                          \
509      " 4: dbf    %%d0,3b\n"                             \
510      "    move.w %2,%%d0\n"                             \
511      "    lsr.w  #2,%%d0\n"                             \
512      "    and.w  #7,%%d0\n"                             \
513      "    bra    6f\n"                                  \
514      " 5: move.l (%0)+,(%1)\n"                          \
515      " 6: dbf    %%d0,5b\n"                             \
516      "    and.w  #3,%2\n"                               \
517      "    bra    8f\n"                                  \
518      " 7: move.b (%0)+,(%1)\n"                          \
519      " 8: dbf    %2,7b\n"                               \
520      "    moveq.l #0, %2\n"                             \
521      " 9: \n"                                           \
522      ".section .fixup,\"ax\"\n"                         \
523      "    .even\n"                                      \
524      "90: moveq.l #1, %2\n"                             \
525      "    jra 9b\n"                                     \
526      ".previous\n"                                      \
527      ".section __ex_table,\"a\"\n"                      \
528      "   .align 4\n"                                    \
529      "   .long  1b,90b\n"                               \
530      "   .long  3b,90b\n"                               \
531      "   .long 31b,90b\n"                               \
532      "   .long 32b,90b\n"                               \
533      "   .long 33b,90b\n"                               \
534      "   .long 34b,90b\n"                               \
535      "   .long 35b,90b\n"                               \
536      "   .long 36b,90b\n"                               \
537      "   .long 37b,90b\n"                               \
538      "   .long  5b,90b\n"                               \
539      "   .long  7b,90b\n"                               \
540      ".previous"                                        \
541      : "=a"(s), "=a"(d), "=d"(len)                      \
542      : "0"(s), "1"(d), "2"(len)                         \
543      : "d0")
544
545 static int macscsi_pwrite (struct Scsi_Host *instance,
546                                   unsigned char *src, int len)
547 {
548    unsigned char *s;
549    volatile unsigned char *d;
550
551    NCR5380_local_declare();
552    NCR5380_setup(instance);
553
554    s = src;
555    d = mac_scsi_drq;
556    
557 /* These conditions are derived from MacOS */
558
559    while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
560          && (!(NCR5380_read(STATUS_REG) & SR_REQ) 
561             || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))) 
562       ;
563    if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
564       printk(KERN_ERR "Error in macscsi_pwrite\n");
565       return -1;
566    }
567
568    CP_MEM_TO_IO(s, d, len);   
569
570    if (len != 0) {
571       printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
572       return -1;
573    }
574    
575    return 0;
576 }
577
578
579 #include "NCR5380.c"
580
581 static struct scsi_host_template driver_template = {
582         .proc_name                      = "Mac5380",
583         .proc_info                      = macscsi_proc_info,
584         .name                           = "Macintosh NCR5380 SCSI",
585         .detect                         = macscsi_detect,
586         .release                        = macscsi_release,
587         .info                           = macscsi_info,
588         .queuecommand                   = macscsi_queue_command,
589         .eh_abort_handler               = macscsi_abort,
590         .eh_bus_reset_handler           = macscsi_bus_reset,
591         .can_queue                      = CAN_QUEUE,
592         .this_id                        = 7,
593         .sg_tablesize                   = SG_ALL,
594         .cmd_per_lun                    = CMD_PER_LUN,
595         .unchecked_isa_dma              = 0,
596         .use_clustering                 = DISABLE_CLUSTERING
597 };
598
599
600 #include "scsi_module.c"