clean
[linux-2.4.21-pre4.git] / 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_scsi.c,v $
30  * Revision 1.1.1.1  2005/04/11 02:50:36  jack
31  * first release
32  *
33  * Revision 1.1.1.1  2005/01/10 13:16:36  jack
34  * First release
35  *
36  */
37
38 #include <linux/types.h>
39 #include <linux/stddef.h>
40 #include <linux/ctype.h>
41 #include <linux/delay.h>
42
43 #include <linux/module.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/ioport.h>
47 #include <linux/init.h>
48 #include <linux/blk.h>
49
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/system.h>
53
54 #include <asm/macintosh.h>
55 #include <asm/macints.h>
56 #include <asm/machw.h>
57 #include <asm/mac_via.h>
58
59 #include "scsi.h"
60 #include "hosts.h"
61 #include "mac_scsi.h"
62 #include "NCR5380.h"
63 #include "constants.h"
64
65 #if 0
66 #define NDEBUG (NDEBUG_INTR | NDEBUG_PSEUDO_DMA | NDEBUG_ARBITRATION | NDEBUG_SELECTION | NDEBUG_RESELECTION)
67 #else
68 #define NDEBUG (NDEBUG_ABORT)
69 #endif
70
71 #define RESET_BOOT
72 #define DRIVER_SETUP
73
74 #define ENABLE_IRQ()    mac_enable_irq( IRQ_MAC_SCSI ); 
75 #define DISABLE_IRQ()   mac_disable_irq( IRQ_MAC_SCSI );
76
77 extern void via_scsi_clear(void);
78
79 #ifdef RESET_BOOT
80 static void mac_scsi_reset_boot(struct Scsi_Host *instance);
81 #endif
82
83 static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg);
84 static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value);
85
86 static int setup_called = 0;
87 static int setup_can_queue = -1;
88 static int setup_cmd_per_lun = -1;
89 static int setup_sg_tablesize = -1;
90 static int setup_use_pdma = -1;
91 #ifdef SUPPORT_TAGS
92 static int setup_use_tagged_queuing = -1;
93 #endif
94 static int setup_hostid = -1;
95
96 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
97  * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
98  * need ten times the standard value... */
99 #define TOSHIBA_DELAY
100
101 #ifdef TOSHIBA_DELAY
102 #define AFTER_RESET_DELAY       (5*HZ/2)
103 #else
104 #define AFTER_RESET_DELAY       (HZ/2)
105 #endif
106
107 static volatile unsigned char *mac_scsi_regp = NULL;
108 static volatile unsigned char *mac_scsi_drq  = NULL;
109 static volatile unsigned char *mac_scsi_nodrq = NULL;
110
111 /*
112  * Function : mac_scsi_setup(char *str)
113  *
114  * Purpose : booter command line initialization of the overrides array,
115  *
116  * Inputs : str - comma delimited list of options
117  *
118  */
119
120 static int __init mac_scsi_setup(char *str) {
121 #ifdef DRIVER_SETUP     
122         int ints[7];
123         
124         (void)get_options( str, ARRAY_SIZE(ints), ints);
125         
126         if (setup_called++ || ints[0] < 1 || ints[0] > 6) {
127             printk(KERN_WARNING "scsi: <mac5380>"
128                 " Usage: mac5380=<can_queue>[,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>,<use_pdma>]\n");
129             printk(KERN_ALERT "scsi: <mac5380> Bad Penguin parameters?\n");
130             return 0;
131         }
132             
133         if (ints[0] >= 1) {
134                 if (ints[1] > 0)
135                         /* no limits on this, just > 0 */
136                         setup_can_queue = ints[1];
137         }
138         if (ints[0] >= 2) {
139                 if (ints[2] > 0)
140                         setup_cmd_per_lun = ints[2];
141         }
142         if (ints[0] >= 3) {
143                 if (ints[3] >= 0) {
144                         setup_sg_tablesize = ints[3];
145                         /* Must be <= SG_ALL (255) */
146                         if (setup_sg_tablesize > SG_ALL)
147                                 setup_sg_tablesize = SG_ALL;
148                 }
149         }
150         if (ints[0] >= 4) {
151                 /* Must be between 0 and 7 */
152                 if (ints[4] >= 0 && ints[4] <= 7)
153                         setup_hostid = ints[4];
154                 else if (ints[4] > 7)
155                         printk(KERN_WARNING "mac_scsi_setup: invalid host ID %d !\n", ints[4] );
156         }
157 #ifdef SUPPORT_TAGS     
158         if (ints[0] >= 5) {
159                 if (ints[5] >= 0)
160                         setup_use_tagged_queuing = !!ints[5];
161         }
162         
163         if (ints[0] == 6) {
164             if (ints[6] >= 0)
165                 setup_use_pdma = ints[6];
166         }
167 #else
168         if (ints[0] == 5) {
169             if (ints[5] >= 0)
170                 setup_use_pdma = ints[5];
171         }
172 #endif
173         
174 #endif
175         return 1;
176 }
177
178 __setup("mac5380=", mac_scsi_setup);
179
180 /*
181  * XXX: status debug
182  */
183 static struct Scsi_Host *default_instance;
184
185 /*
186  * Function : int macscsi_detect(Scsi_Host_Template * tpnt)
187  *
188  * Purpose : initializes mac NCR5380 driver based on the
189  *      command line / compile time port and irq definitions.
190  *
191  * Inputs : tpnt - template for this SCSI adapter.
192  *
193  * Returns : 1 if a host adapter was found, 0 if not.
194  *
195  */
196  
197 int macscsi_detect(Scsi_Host_Template * tpnt)
198 {
199     static int called = 0;
200     int flags = 0;
201     struct Scsi_Host *instance;
202
203     if (!MACH_IS_MAC || called)
204         return( 0 );
205
206     if (macintosh_config->scsi_type != MAC_SCSI_OLD)
207         return( 0 );
208
209     /* setup variables */
210     tpnt->can_queue =
211         (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
212     tpnt->cmd_per_lun =
213         (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
214     tpnt->sg_tablesize = 
215         (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
216
217     if (setup_hostid >= 0)
218         tpnt->this_id = setup_hostid;
219     else {
220         /* use 7 as default */
221         tpnt->this_id = 7;
222     }
223
224 #ifdef SUPPORT_TAGS
225     if (setup_use_tagged_queuing < 0)
226         setup_use_tagged_queuing = USE_TAGGED_QUEUING;
227 #endif
228
229     /* Once we support multiple 5380s (e.g. DuoDock) we'll do
230        something different here */
231     instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
232     default_instance = instance;
233     
234     if (macintosh_config->ident == MAC_MODEL_IIFX) {
235         mac_scsi_regp  = via1+0x8000;
236         mac_scsi_drq   = via1+0xE000;
237         mac_scsi_nodrq = via1+0xC000;
238         /* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
239         flags = FLAG_NO_PSEUDO_DMA;
240     } else {
241         mac_scsi_regp  = via1+0x10000;
242         mac_scsi_drq   = via1+0x6000;
243         mac_scsi_nodrq = via1+0x12000;
244     }
245
246     if (! setup_use_pdma)
247         flags = FLAG_NO_PSEUDO_DMA;
248         
249     instance->io_port = (unsigned long) mac_scsi_regp;
250     instance->irq = IRQ_MAC_SCSI;
251
252 #ifdef RESET_BOOT   
253     mac_scsi_reset_boot(instance);
254 #endif
255     
256     NCR5380_init(instance, flags);
257
258     instance->n_io_port = 255;
259
260     ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
261
262     if (instance->irq != IRQ_NONE)
263         if (request_irq(instance->irq, NCR5380_intr, IRQ_FLG_SLOW, 
264                 "ncr5380", NCR5380_intr)) {
265             printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
266                    instance->host_no, instance->irq);
267             instance->irq = IRQ_NONE;
268         }
269
270     printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
271     if (instance->irq == IRQ_NONE)
272         printk (KERN_INFO "s disabled");
273     else
274         printk (KERN_INFO " %d", instance->irq);
275     printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
276            instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
277     printk(KERN_INFO "\nscsi%d:", instance->host_no);
278     NCR5380_print_options(instance);
279     printk("\n");
280     called = 1;
281     return 1;
282 }
283
284 int macscsi_release (struct Scsi_Host *shpnt)
285 {
286         if (shpnt->irq != IRQ_NONE)
287                 free_irq (shpnt->irq, NCR5380_intr);
288
289         return 0;
290 }
291
292 #ifdef RESET_BOOT
293 /*
294  * Our 'bus reset on boot' function
295  */
296
297 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
298 {
299         unsigned long end;
300
301         NCR5380_local_declare();
302         NCR5380_setup(instance);
303         
304         /*
305          * Do a SCSI reset to clean up the bus during initialization. No messing
306          * with the queues, interrupts, or locks necessary here.
307          */
308
309         printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
310
311         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
312         mac_disable_irq(IRQ_MAC_SCSI);
313
314         /* get in phase */
315         NCR5380_write( TARGET_COMMAND_REG,
316                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
317
318         /* assert RST */
319         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
320         /* The min. reset hold time is 25us, so 40us should be enough */
321         udelay( 50 );
322         /* reset RST and interrupt */
323         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
324         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
325
326         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
327                 barrier();
328
329         /* switch on SCSI IRQ again */
330         mac_enable_irq(IRQ_MAC_SCSI);
331
332         printk(KERN_INFO " done\n" );
333 }
334 #endif
335
336 const char * macscsi_info (struct Scsi_Host *spnt) {
337         return "";
338 }
339
340 /*
341  * NCR 5380 register access functions
342  */
343
344 /* Debug versions
345 #define CTRL(p,v) (*ctrl = (v))
346
347 static char macscsi_read(struct Scsi_Host *instance, int reg)
348 {
349   int iobase = instance->io_port;
350   int i;
351   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
352
353   CTRL(iobase, 0);
354   i = in_8(iobase + (reg<<4));
355   CTRL(iobase, 0x40);
356
357   return i;
358 }
359
360 static void macscsi_write(struct Scsi_Host *instance, int reg, int value)
361 {
362   int iobase = instance->io_port;
363   int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
364
365   CTRL(iobase, 0);
366   out_8(value, iobase + (reg<<4));
367   CTRL(iobase, 0x40);
368 }
369 */
370
371 /* Fast versions */
372 static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
373 {
374   return in_8(instance->io_port + (reg<<4));
375 }
376
377 static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
378 {
379   out_8(value, instance->io_port + (reg<<4));
380 }
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 /* These control the behaviour of the generic 5380 core */
580 #define AUTOSENSE
581 #define PSEUDO_DMA
582
583 #include "NCR5380.c"
584
585 static Scsi_Host_Template driver_template = MAC_NCR5380;
586
587 #include "scsi_module.c"