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