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