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