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