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