ncr5380: Move static PDMA spin counters to host data
[cascardo/linux.git] / drivers / scsi / pas16.c
1 #define PSEUDO_DMA
2 #define UNSAFE  /* Not unsafe for PAS16 -- use it */
3 #define PDEBUG 0
4
5 /*
6  * This driver adapted from Drew Eckhardt's Trantor T128 driver
7  *
8  * Copyright 1993, Drew Eckhardt
9  *      Visionary Computing
10  *      (Unix and Linux consulting and custom programming)
11  *      drew@colorado.edu
12  *      +1 (303) 666-5836
13  *
14  *  ( Based on T128 - DISTRIBUTION RELEASE 3. ) 
15  *
16  * Modified to work with the Pro Audio Spectrum/Studio 16
17  * by John Weidman.
18  *
19  *
20  * For more information, please consult 
21  *
22  * Media Vision
23  * (510) 770-8600
24  * (800) 348-7116
25  */
26
27 /*
28  * The card is detected and initialized in one of several ways : 
29  * 1.  Autoprobe (default) - There are many different models of
30  *     the Pro Audio Spectrum/Studio 16, and I only have one of
31  *     them, so this may require a little tweaking.  An interrupt
32  *     is triggered to autoprobe for the interrupt line.  Note:
33  *     with the newer model boards, the interrupt is set via
34  *     software after reset using the default_irq for the
35  *     current board number.
36  *
37  * 2.  With command line overrides - pas16=port,irq may be 
38  *     used on the LILO command line to override the defaults.
39  *
40  * 3.  With the PAS16_OVERRIDE compile time define.  This is 
41  *     specified as an array of address, irq tuples.  Ie, for
42  *     one board at the default 0x388 address, IRQ10, I could say 
43  *     -DPAS16_OVERRIDE={{0x388, 10}}
44  *     NOTE:  Untested.
45  *      
46  * 4.  When included as a module, with arguments passed on the command line:
47  *         pas16_irq=xx         the interrupt
48  *         pas16_addr=xx        the port
49  *     e.g. "modprobe pas16 pas16_addr=0x388 pas16_irq=5"
50  *
51  *     Note that if the override methods are used, place holders must
52  *     be specified for other boards in the system.
53  *
54  *
55  * Configuration notes :
56  *   The current driver does not support interrupt sharing with the
57  *   sound portion of the card.  If you use the same irq for the
58  *   scsi port and sound you will have problems.  Either use
59  *   a different irq for the scsi port or don't use interrupts
60  *   for the scsi port.
61  *
62  *   If you have problems with your card not being recognized, use
63  *   the LILO command line override.  Try to get it recognized without
64  *   interrupts.  Ie, for a board at the default 0x388 base port,
65  *   boot: linux pas16=0x388,0
66  *
67  *   NO_IRQ (0) should be specified for no interrupt,
68  *   IRQ_AUTO (254) to autoprobe for an IRQ line if overridden
69  *   on the command line.
70  */
71  
72 #include <linux/module.h>
73
74 #include <linux/signal.h>
75 #include <linux/proc_fs.h>
76 #include <asm/io.h>
77 #include <asm/dma.h>
78 #include <linux/blkdev.h>
79 #include <linux/delay.h>
80 #include <linux/interrupt.h>
81 #include <linux/stat.h>
82 #include <linux/init.h>
83
84 #include "scsi.h"
85 #include <scsi/scsi_host.h>
86 #include "pas16.h"
87 #define AUTOPROBE_IRQ
88 #include "NCR5380.h"
89
90
91 static unsigned short pas16_addr = 0;
92 static int pas16_irq = 0;
93  
94
95 static const int scsi_irq_translate[] =
96         { 0,  0,  1,  2,  3,  4,  5,  6, 0,  0,  7,  8,  9,  0, 10, 11 };
97
98 /* The default_irqs array contains values used to set the irq into the
99  * board via software (as must be done on newer model boards without
100  * irq jumpers on the board).  The first value in the array will be
101  * assigned to logical board 0, the next to board 1, etc.
102  */
103 static int default_irqs[] __initdata =
104         {  PAS16_DEFAULT_BOARD_1_IRQ,
105            PAS16_DEFAULT_BOARD_2_IRQ,
106            PAS16_DEFAULT_BOARD_3_IRQ,
107            PAS16_DEFAULT_BOARD_4_IRQ
108         };
109
110 static struct override {
111     unsigned short io_port;
112     int  irq;
113 } overrides
114 #ifdef PAS16_OVERRIDE
115     [] __initdata = PAS16_OVERRIDE;
116 #else
117     [4] __initdata = {{0,IRQ_AUTO}, {0,IRQ_AUTO}, {0,IRQ_AUTO},
118         {0,IRQ_AUTO}};
119 #endif
120
121 #define NO_OVERRIDES ARRAY_SIZE(overrides)
122
123 static struct base {
124     unsigned short io_port;
125     int noauto;
126 } bases[] __initdata =
127         { {PAS16_DEFAULT_BASE_1, 0},
128           {PAS16_DEFAULT_BASE_2, 0},
129           {PAS16_DEFAULT_BASE_3, 0},
130           {PAS16_DEFAULT_BASE_4, 0}
131         };
132
133 #define NO_BASES ARRAY_SIZE(bases)
134
135 static const unsigned short  pas16_offset[ 8 ] =
136     {
137         0x1c00,    /* OUTPUT_DATA_REG */
138         0x1c01,    /* INITIATOR_COMMAND_REG */
139         0x1c02,    /* MODE_REG */
140         0x1c03,    /* TARGET_COMMAND_REG */
141         0x3c00,    /* STATUS_REG ro, SELECT_ENABLE_REG wo */
142         0x3c01,    /* BUS_AND_STATUS_REG ro, START_DMA_SEND_REG wo */
143         0x3c02,    /* INPUT_DATA_REGISTER ro, (N/A on PAS16 ?)
144                     * START_DMA_TARGET_RECEIVE_REG wo
145                     */
146         0x3c03,    /* RESET_PARITY_INTERRUPT_REG ro,
147                     * START_DMA_INITIATOR_RECEIVE_REG wo
148                     */
149     };
150 /*----------------------------------------------------------------*/
151 /* the following will set the monitor border color (useful to find
152  where something crashed or gets stuck at */
153 /* 1 = blue
154  2 = green
155  3 = cyan
156  4 = red
157  5 = magenta
158  6 = yellow
159  7 = white
160 */
161 #if 1
162 #define rtrc(i) {inb(0x3da); outb(0x31, 0x3c0); outb((i), 0x3c0);}
163 #else
164 #define rtrc(i) {}
165 #endif
166
167
168 /*
169  * Function : enable_board( int  board_num, unsigned short port )
170  *
171  * Purpose :  set address in new model board
172  *
173  * Inputs : board_num - logical board number 0-3, port - base address
174  *
175  */
176
177 static void __init
178         enable_board( int  board_num,  unsigned short port )
179 {
180     outb( 0xbc + board_num, MASTER_ADDRESS_PTR );
181     outb( port >> 2, MASTER_ADDRESS_PTR );
182 }
183
184
185
186 /*
187  * Function : init_board( unsigned short port, int irq )
188  *
189  * Purpose :  Set the board up to handle the SCSI interface
190  *
191  * Inputs : port - base address of the board,
192  *          irq - irq to assign to the SCSI port
193  *          force_irq - set it even if it conflicts with sound driver
194  *
195  */
196
197 static void __init 
198         init_board( unsigned short io_port, int irq, int force_irq )
199 {
200         unsigned int    tmp;
201         unsigned int    pas_irq_code;
202
203         /* Initialize the SCSI part of the board */
204
205         outb( 0x30, io_port + P_TIMEOUT_COUNTER_REG );  /* Timeout counter */
206         outb( 0x01, io_port + P_TIMEOUT_STATUS_REG_OFFSET );   /* Reset TC */
207         outb( 0x01, io_port + WAIT_STATE );   /* 1 Wait state */
208
209         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
210
211         /* Set the SCSI interrupt pointer without mucking up the sound
212          * interrupt pointer in the same byte.
213          */
214         pas_irq_code = ( irq < 16 ) ? scsi_irq_translate[irq] : 0;
215         tmp = inb( io_port + IO_CONFIG_3 );
216
217         if( (( tmp & 0x0f ) == pas_irq_code) && pas_irq_code > 0 
218             && !force_irq )
219         {
220             printk( "pas16: WARNING: Can't use same irq as sound "
221                     "driver -- interrupts disabled\n" );
222             /* Set up the drive parameters, disable 5380 interrupts */
223             outb( 0x4d, io_port + SYS_CONFIG_4 );
224         }
225         else
226         {
227             tmp = (  tmp & 0x0f ) | ( pas_irq_code << 4 );
228             outb( tmp, io_port + IO_CONFIG_3 );
229
230             /* Set up the drive parameters and enable 5380 interrupts */
231             outb( 0x6d, io_port + SYS_CONFIG_4 );
232         }
233 }
234
235
236 /*
237  * Function : pas16_hw_detect( unsigned short board_num )
238  *
239  * Purpose : determine if a pas16 board is present
240  * 
241  * Inputs : board_num - logical board number ( 0 - 3 )
242  *
243  * Returns : 0 if board not found, 1 if found.
244  */
245
246 static int __init 
247      pas16_hw_detect( unsigned short  board_num )
248 {
249     unsigned char       board_rev, tmp;
250     unsigned short      io_port = bases[ board_num ].io_port;
251
252     /* See if we can find a PAS16 board at the address associated
253      * with this logical board number.
254      */
255
256     /* First, attempt to take a newer model board out of reset and
257      * give it a base address.  This shouldn't affect older boards.
258      */
259     enable_board( board_num, io_port );
260
261     /* Now see if it looks like a PAS16 board */
262     board_rev = inb( io_port + PCB_CONFIG );
263
264     if( board_rev == 0xff )
265         return 0;
266
267     tmp = board_rev ^ 0xe0;
268
269     outb( tmp, io_port + PCB_CONFIG );
270     tmp = inb( io_port + PCB_CONFIG );
271     outb( board_rev, io_port + PCB_CONFIG );
272
273     if( board_rev != tmp )      /* Not a PAS-16 */
274         return 0;
275
276     if( ( inb( io_port + OPERATION_MODE_1 ) & 0x03 ) != 0x03 ) 
277         return 0;       /* return if no SCSI interface found */
278
279     /* Mediavision has some new model boards that return ID bits
280      * that indicate a SCSI interface, but they're not (LMS).  We'll
281      * put in an additional test to try to weed them out.
282      */
283
284     outb( 0x01, io_port + WAIT_STATE );         /* 1 Wait state */
285     NCR5380_write( MODE_REG, 0x20 );            /* Is it really SCSI? */
286     if( NCR5380_read( MODE_REG ) != 0x20 )      /* Write to a reg.    */
287         return 0;                               /* and try to read    */
288     NCR5380_write( MODE_REG, 0x00 );            /* it back.           */
289     if( NCR5380_read( MODE_REG ) != 0x00 )
290         return 0;
291
292     return 1;
293 }
294
295
296 #ifndef MODULE
297 /*
298  * Function : pas16_setup(char *str, int *ints)
299  *
300  * Purpose : LILO command line initialization of the overrides array,
301  * 
302  * Inputs : str - unused, ints - array of integer parameters with ints[0]
303  *      equal to the number of ints.
304  *
305  */
306
307 static int __init pas16_setup(char *str)
308 {
309     static int commandline_current = 0;
310     int i;
311     int ints[10];
312
313     get_options(str, ARRAY_SIZE(ints), ints);
314     if (ints[0] != 2) 
315         printk("pas16_setup : usage pas16=io_port,irq\n");
316     else 
317         if (commandline_current < NO_OVERRIDES) {
318             overrides[commandline_current].io_port = (unsigned short) ints[1];
319             overrides[commandline_current].irq = ints[2];
320             for (i = 0; i < NO_BASES; ++i)
321                 if (bases[i].io_port == (unsigned short) ints[1]) {
322                     bases[i].noauto = 1;
323                     break;
324                 }
325             ++commandline_current;
326         }
327     return 1;
328 }
329
330 __setup("pas16=", pas16_setup);
331 #endif
332
333 /* 
334  * Function : int pas16_detect(struct scsi_host_template * tpnt)
335  *
336  * Purpose : detects and initializes PAS16 controllers
337  *      that were autoprobed, overridden on the LILO command line, 
338  *      or specified at compile time.
339  *
340  * Inputs : tpnt - template for this SCSI adapter.
341  * 
342  * Returns : 1 if a host adapter was found, 0 if not.
343  *
344  */
345
346 static int __init pas16_detect(struct scsi_host_template *tpnt)
347 {
348     static int current_override = 0;
349     static unsigned short current_base = 0;
350     struct Scsi_Host *instance;
351     unsigned short io_port;
352     int  count;
353
354     if (pas16_addr != 0) {
355         overrides[0].io_port = pas16_addr;
356         /*
357         *  This is how we avoid seeing more than
358         *  one host adapter at the same I/O port.
359         *  Cribbed shamelessly from pas16_setup().
360         */
361         for (count = 0; count < NO_BASES; ++count)
362             if (bases[count].io_port == pas16_addr) {
363                     bases[count].noauto = 1;
364                     break;
365         }
366     }
367     if (pas16_irq != 0)
368         overrides[0].irq = pas16_irq;
369
370     for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
371         io_port = 0;
372
373         if (overrides[current_override].io_port)
374         {
375             io_port = overrides[current_override].io_port;
376             enable_board( current_override, io_port );
377             init_board( io_port, overrides[current_override].irq, 1 );
378         }
379         else
380             for (; !io_port && (current_base < NO_BASES); ++current_base) {
381 #if (PDEBUG & PDEBUG_INIT)
382     printk("scsi-pas16 : probing io_port %04x\n", (unsigned int) bases[current_base].io_port);
383 #endif
384                 if ( !bases[current_base].noauto &&
385                      pas16_hw_detect( current_base ) ){
386                         io_port = bases[current_base].io_port;
387                         init_board( io_port, default_irqs[ current_base ], 0 ); 
388 #if (PDEBUG & PDEBUG_INIT)
389                         printk("scsi-pas16 : detected board.\n");
390 #endif
391                 }
392     }
393
394
395 #if defined(PDEBUG) && (PDEBUG & PDEBUG_INIT)
396         printk("scsi-pas16 : io_port = %04x\n", (unsigned int) io_port);
397 #endif
398
399         if (!io_port)
400             break;
401
402         instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
403         if(instance == NULL)
404                 break;
405                 
406         instance->io_port = io_port;
407
408         NCR5380_init(instance, 0);
409
410         if (overrides[current_override].irq != IRQ_AUTO)
411             instance->irq = overrides[current_override].irq;
412         else 
413             instance->irq = NCR5380_probe_irq(instance, PAS16_IRQS);
414
415         /* Compatibility with documented NCR5380 kernel parameters */
416         if (instance->irq == 255)
417                 instance->irq = NO_IRQ;
418
419         if (instance->irq != NO_IRQ)
420             if (request_irq(instance->irq, pas16_intr, 0,
421                             "pas16", instance)) {
422                 printk("scsi%d : IRQ%d not free, interrupts disabled\n", 
423                     instance->host_no, instance->irq);
424                 instance->irq = NO_IRQ;
425             } 
426
427         if (instance->irq == NO_IRQ) {
428             printk("scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
429             printk("scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
430             /* Disable 5380 interrupts, leave drive params the same */
431             outb( 0x4d, io_port + SYS_CONFIG_4 );
432             outb( (inb(io_port + IO_CONFIG_3) & 0x0f), io_port + IO_CONFIG_3 );
433         }
434
435 #if defined(PDEBUG) && (PDEBUG & PDEBUG_INIT)
436         printk("scsi%d : irq = %d\n", instance->host_no, instance->irq);
437 #endif
438
439         ++current_override;
440         ++count;
441     }
442     return count;
443 }
444
445 /*
446  * Function : int pas16_biosparam(Disk *disk, struct block_device *dev, int *ip)
447  *
448  * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for 
449  *      the specified device / size.
450  * 
451  * Inputs : size = size of device in sectors (512 bytes), dev = block device
452  *      major / minor, ip[] = {heads, sectors, cylinders}  
453  *
454  * Returns : always 0 (success), initializes ip
455  *      
456  */
457
458 /* 
459  * XXX Most SCSI boards use this mapping, I could be incorrect.  Some one
460  * using hard disks on a trantor should verify that this mapping corresponds
461  * to that used by the BIOS / ASPI driver by running the linux fdisk program
462  * and matching the H_C_S coordinates to what DOS uses.
463  */
464
465 static int pas16_biosparam(struct scsi_device *sdev, struct block_device *dev,
466                            sector_t capacity, int *ip)
467 {
468   int size = capacity;
469   ip[0] = 64;
470   ip[1] = 32;
471   ip[2] = size >> 11;           /* I think I have it as /(32*64) */
472   if( ip[2] > 1024 ) {          /* yes, >, not >= */
473         ip[0]=255;
474         ip[1]=63;
475         ip[2]=size/(63*255);
476         if( ip[2] > 1023 )      /* yes >1023... */
477                 ip[2] = 1023;
478   }
479
480   return 0;
481 }
482
483 /*
484  * Function : int NCR5380_pread (struct Scsi_Host *instance, 
485  *      unsigned char *dst, int len)
486  *
487  * Purpose : Fast 5380 pseudo-dma read function, transfers len bytes to 
488  *      dst
489  * 
490  * Inputs : dst = destination, len = length in bytes
491  *
492  * Returns : 0 on success, non zero on a failure such as a watchdog 
493  *      timeout.
494  */
495
496 static inline int NCR5380_pread (struct Scsi_Host *instance, unsigned char *dst,
497     int len) {
498     register unsigned char  *d = dst;
499     register unsigned short reg = (unsigned short) (instance->io_port + 
500         P_DATA_REG_OFFSET);
501     register int i = len;
502     int ii = 0;
503     struct NCR5380_hostdata *hostdata = shost_priv(instance);
504
505     while ( !(inb(instance->io_port + P_STATUS_REG_OFFSET) & P_ST_RDY) )
506          ++ii;
507
508     insb( reg, d, i );
509
510     if ( inb(instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET) & P_TS_TIM) {
511         outb( P_TS_CT, instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET);
512         printk("scsi%d : watchdog timer fired in NCR5380_pread()\n",
513             instance->host_no);
514         return -1;
515     }
516     if (ii > hostdata->spin_max_r)
517         hostdata->spin_max_r = ii;
518     return 0;
519 }
520
521 /*
522  * Function : int NCR5380_pwrite (struct Scsi_Host *instance, 
523  *      unsigned char *src, int len)
524  *
525  * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
526  *      src
527  * 
528  * Inputs : src = source, len = length in bytes
529  *
530  * Returns : 0 on success, non zero on a failure such as a watchdog 
531  *      timeout.
532  */
533
534 static inline int NCR5380_pwrite (struct Scsi_Host *instance, unsigned char *src,
535     int len) {
536     register unsigned char *s = src;
537     register unsigned short reg = (instance->io_port + P_DATA_REG_OFFSET);
538     register int i = len;
539     int ii = 0;
540     struct NCR5380_hostdata *hostdata = shost_priv(instance);
541
542     while ( !((inb(instance->io_port + P_STATUS_REG_OFFSET)) & P_ST_RDY) )
543          ++ii;
544  
545     outsb( reg, s, i );
546
547     if (inb(instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET) & P_TS_TIM) {
548         outb( P_TS_CT, instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET);
549         printk("scsi%d : watchdog timer fired in NCR5380_pwrite()\n",
550             instance->host_no);
551         return -1;
552     }
553     if (ii > hostdata->spin_max_w)
554         hostdata->spin_max_w = ii;
555     return 0;
556 }
557
558 #include "NCR5380.c"
559
560 static int pas16_release(struct Scsi_Host *shost)
561 {
562         if (shost->irq != NO_IRQ)
563                 free_irq(shost->irq, shost);
564         NCR5380_exit(shost);
565         if (shost->io_port && shost->n_io_port)
566                 release_region(shost->io_port, shost->n_io_port);
567         scsi_unregister(shost);
568         return 0;
569 }
570
571 static struct scsi_host_template driver_template = {
572         .name           = "Pro Audio Spectrum-16 SCSI",
573         .detect         = pas16_detect,
574         .release        = pas16_release,
575         .proc_name      = "pas16",
576         .show_info      = pas16_show_info,
577         .write_info     = pas16_write_info,
578         .info           = pas16_info,
579         .queuecommand   = pas16_queue_command,
580         .eh_abort_handler = pas16_abort,
581         .eh_bus_reset_handler = pas16_bus_reset,
582         .bios_param     = pas16_biosparam, 
583         .can_queue      = CAN_QUEUE,
584         .this_id        = 7,
585         .sg_tablesize   = SG_ALL,
586         .cmd_per_lun    = CMD_PER_LUN,
587         .use_clustering = DISABLE_CLUSTERING,
588 };
589 #include "scsi_module.c"
590
591 #ifdef MODULE
592 module_param(pas16_addr, ushort, 0);
593 module_param(pas16_irq, int, 0);
594 #endif
595 MODULE_LICENSE("GPL");