1/*
2 * Generic Macintosh NCR5380 driver
3 *
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
5 *
6 * derived in part from:
7 */
8/*
9 * Generic Generic NCR5380 driver
10 *
11 * Copyright 1995, Russell King
12 *
13 * ALPHA RELEASE 1.
14 *
15 * For more information, please consult
16 *
17 * NCR 5380 Family
18 * SCSI Protocol Controller
19 * Databook
20 *
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
24 * 1+ (719) 578-3400
25 * 1+ (800) 334-5454
26 */
27
28/*
29 * $Log: mac_NCR5380.c,v $
30 */
31
32#include <linux/types.h>
33#include <linux/stddef.h>
34#include <linux/ctype.h>
35#include <linux/delay.h>
36
37#include <linux/module.h>
38#include <linux/signal.h>
39#include <linux/ioport.h>
40#include <linux/init.h>
41#include <linux/blkdev.h>
42#include <linux/interrupt.h>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/system.h>
47
48#include <asm/macintosh.h>
49#include <asm/macints.h>
50#include <asm/mac_via.h>
51
52#include "scsi.h"
53#include <scsi/scsi_host.h>
54#include "mac_scsi.h"
55
56/* These control the behaviour of the generic 5380 core */
57#define AUTOSENSE
58#define PSEUDO_DMA
59
60#include "NCR5380.h"
61
62#if 0
63#define NDEBUG (NDEBUG_INTR | NDEBUG_PSEUDO_DMA | NDEBUG_ARBITRATION | NDEBUG_SELECTION | NDEBUG_RESELECTION)
64#else
65#define NDEBUG (NDEBUG_ABORT)
66#endif
67
68#define RESET_BOOT
69#define DRIVER_SETUP
70
71extern void via_scsi_clear(void);
72
73#ifdef RESET_BOOT
74static void mac_scsi_reset_boot(struct Scsi_Host *instance);
75#endif
76
77static int setup_called = 0;
78static int setup_can_queue = -1;
79static int setup_cmd_per_lun = -1;
80static int setup_sg_tablesize = -1;
81static int setup_use_pdma = -1;
82#ifdef SUPPORT_TAGS
83static int setup_use_tagged_queuing = -1;
84#endif
85static int setup_hostid = -1;
86
87/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
88 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
89 * need ten times the standard value... */
90#define TOSHIBA_DELAY
91
92#ifdef TOSHIBA_DELAY
93#define	AFTER_RESET_DELAY	(5*HZ/2)
94#else
95#define	AFTER_RESET_DELAY	(HZ/2)
96#endif
97
98static volatile unsigned char *mac_scsi_regp = NULL;
99static volatile unsigned char *mac_scsi_drq  = NULL;
100static volatile unsigned char *mac_scsi_nodrq = NULL;
101
102
103/*
104 * NCR 5380 register access functions
105 */
106
107#if 0
108/* Debug versions */
109#define CTRL(p,v) (*ctrl = (v))
110
111static char macscsi_read(struct Scsi_Host *instance, int reg)
112{
113  int iobase = instance->io_port;
114  int i;
115  int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
116
117  CTRL(iobase, 0);
118  i = in_8(iobase + (reg<<4));
119  CTRL(iobase, 0x40);
120
121  return i;
122}
123
124static void macscsi_write(struct Scsi_Host *instance, int reg, int value)
125{
126  int iobase = instance->io_port;
127  int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
128
129  CTRL(iobase, 0);
130  out_8(iobase + (reg<<4), value);
131  CTRL(iobase, 0x40);
132}
133#else
134
135/* Fast versions */
136static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
137{
138  return in_8(instance->io_port + (reg<<4));
139}
140
141static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
142{
143  out_8(instance->io_port + (reg<<4), value);
144}
145#endif
146
147
148/*
149 * Function : mac_scsi_setup(char *str)
150 *
151 * Purpose : booter command line initialization of the overrides array,
152 *
153 * Inputs : str - comma delimited list of options
154 *
155 */
156
157static int __init mac_scsi_setup(char *str) {
158#ifdef DRIVER_SETUP
159	int ints[7];
160
161	(void)get_options( str, ARRAY_SIZE(ints), ints);
162
163	if (setup_called++ || ints[0] < 1 || ints[0] > 6) {
164	    printk(KERN_WARNING "scsi: <mac5380>"
165		" Usage: mac5380=<can_queue>[,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>,<use_pdma>]\n");
166	    printk(KERN_ALERT "scsi: <mac5380> Bad Penguin parameters?\n");
167	    return 0;
168	}
169
170	if (ints[0] >= 1) {
171		if (ints[1] > 0)
172			/* no limits on this, just > 0 */
173			setup_can_queue = ints[1];
174	}
175	if (ints[0] >= 2) {
176		if (ints[2] > 0)
177			setup_cmd_per_lun = ints[2];
178	}
179	if (ints[0] >= 3) {
180		if (ints[3] >= 0) {
181			setup_sg_tablesize = ints[3];
182			/* Must be <= SG_ALL (255) */
183			if (setup_sg_tablesize > SG_ALL)
184				setup_sg_tablesize = SG_ALL;
185		}
186	}
187	if (ints[0] >= 4) {
188		/* Must be between 0 and 7 */
189		if (ints[4] >= 0 && ints[4] <= 7)
190			setup_hostid = ints[4];
191		else if (ints[4] > 7)
192			printk(KERN_WARNING "mac_scsi_setup: invalid host ID %d !\n", ints[4] );
193	}
194#ifdef SUPPORT_TAGS
195	if (ints[0] >= 5) {
196		if (ints[5] >= 0)
197			setup_use_tagged_queuing = !!ints[5];
198	}
199
200	if (ints[0] == 6) {
201	    if (ints[6] >= 0)
202		setup_use_pdma = ints[6];
203	}
204#else
205	if (ints[0] == 5) {
206	    if (ints[5] >= 0)
207		setup_use_pdma = ints[5];
208	}
209#endif /* SUPPORT_TAGS */
210
211#endif /* DRIVER_SETUP */
212	return 1;
213}
214
215__setup("mac5380=", mac_scsi_setup);
216
217/*
218 * Function : int macscsi_detect(struct scsi_host_template * tpnt)
219 *
220 * Purpose : initializes mac NCR5380 driver based on the
221 *	command line / compile time port and irq definitions.
222 *
223 * Inputs : tpnt - template for this SCSI adapter.
224 *
225 * Returns : 1 if a host adapter was found, 0 if not.
226 *
227 */
228
229int __init macscsi_detect(struct scsi_host_template * tpnt)
230{
231    static int called = 0;
232    int flags = 0;
233    struct Scsi_Host *instance;
234
235    if (!MACH_IS_MAC || called)
236	return( 0 );
237
238    if (macintosh_config->scsi_type != MAC_SCSI_OLD)
239	return( 0 );
240
241    /* setup variables */
242    tpnt->can_queue =
243	(setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
244    tpnt->cmd_per_lun =
245	(setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
246    tpnt->sg_tablesize =
247	(setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
248
249    if (setup_hostid >= 0)
250	tpnt->this_id = setup_hostid;
251    else {
252	/* use 7 as default */
253	tpnt->this_id = 7;
254    }
255
256#ifdef SUPPORT_TAGS
257    if (setup_use_tagged_queuing < 0)
258	setup_use_tagged_queuing = USE_TAGGED_QUEUING;
259#endif
260
261    /* Once we support multiple 5380s (e.g. DuoDock) we'll do
262       something different here */
263    instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
264
265    if (macintosh_config->ident == MAC_MODEL_IIFX) {
266	mac_scsi_regp  = via1+0x8000;
267	mac_scsi_drq   = via1+0xE000;
268	mac_scsi_nodrq = via1+0xC000;
269	/* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
270	flags = FLAG_NO_PSEUDO_DMA;
271    } else {
272	mac_scsi_regp  = via1+0x10000;
273	mac_scsi_drq   = via1+0x6000;
274	mac_scsi_nodrq = via1+0x12000;
275    }
276
277    if (! setup_use_pdma)
278	flags = FLAG_NO_PSEUDO_DMA;
279
280    instance->io_port = (unsigned long) mac_scsi_regp;
281    instance->irq = IRQ_MAC_SCSI;
282
283#ifdef RESET_BOOT
284    mac_scsi_reset_boot(instance);
285#endif
286
287    NCR5380_init(instance, flags);
288
289    instance->n_io_port = 255;
290
291    ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
292
293    if (instance->irq != SCSI_IRQ_NONE)
294	if (request_irq(instance->irq, NCR5380_intr, 0, "ncr5380", instance)) {
295	    printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
296		   instance->host_no, instance->irq);
297	    instance->irq = SCSI_IRQ_NONE;
298	}
299
300    printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
301    if (instance->irq == SCSI_IRQ_NONE)
302	printk (KERN_INFO "s disabled");
303    else
304	printk (KERN_INFO " %d", instance->irq);
305    printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
306	   instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
307    printk(KERN_INFO "\nscsi%d:", instance->host_no);
308    NCR5380_print_options(instance);
309    printk("\n");
310    called = 1;
311    return 1;
312}
313
314int macscsi_release (struct Scsi_Host *shpnt)
315{
316	if (shpnt->irq != SCSI_IRQ_NONE)
317		free_irq(shpnt->irq, shpnt);
318	NCR5380_exit(shpnt);
319
320	return 0;
321}
322
323#ifdef RESET_BOOT
324/*
325 * Our 'bus reset on boot' function
326 */
327
328static void mac_scsi_reset_boot(struct Scsi_Host *instance)
329{
330	unsigned long end;
331
332	NCR5380_local_declare();
333	NCR5380_setup(instance);
334
335	/*
336	 * Do a SCSI reset to clean up the bus during initialization. No messing
337	 * with the queues, interrupts, or locks necessary here.
338	 */
339
340	printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
341
342	/* get in phase */
343	NCR5380_write( TARGET_COMMAND_REG,
344		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
345
346	/* assert RST */
347	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
348	/* The min. reset hold time is 25us, so 40us should be enough */
349	udelay( 50 );
350	/* reset RST and interrupt */
351	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
352	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
353
354	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
355		barrier();
356
357	printk(KERN_INFO " done\n" );
358}
359#endif
360
361const char * macscsi_info (struct Scsi_Host *spnt) {
362	return "";
363}
364
365/*
366   Pseudo-DMA: (Ove Edlund)
367   The code attempts to catch bus errors that occur if one for example
368   "trips over the cable".
369   XXX: Since bus errors in the PDMA routines never happen on my
370   computer, the bus error code is untested.
371   If the code works as intended, a bus error results in Pseudo-DMA
372   beeing disabled, meaning that the driver switches to slow handshake.
373   If bus errors are NOT extremely rare, this has to be changed.
374*/
375
376#define CP_IO_TO_MEM(s,d,len)				\
377__asm__ __volatile__					\
378    ("    cmp.w  #4,%2\n"				\
379     "    bls    8f\n"					\
380     "    move.w %1,%%d0\n"				\
381     "    neg.b  %%d0\n"				\
382     "    and.w  #3,%%d0\n"				\
383     "    sub.w  %%d0,%2\n"				\
384     "    bra    2f\n"					\
385     " 1: move.b (%0),(%1)+\n"				\
386     " 2: dbf    %%d0,1b\n"				\
387     "    move.w %2,%%d0\n"				\
388     "    lsr.w  #5,%%d0\n"				\
389     "    bra    4f\n"					\
390     " 3: move.l (%0),(%1)+\n"				\
391     "31: move.l (%0),(%1)+\n"				\
392     "32: move.l (%0),(%1)+\n"				\
393     "33: move.l (%0),(%1)+\n"				\
394     "34: move.l (%0),(%1)+\n"				\
395     "35: move.l (%0),(%1)+\n"				\
396     "36: move.l (%0),(%1)+\n"				\
397     "37: move.l (%0),(%1)+\n"				\
398     " 4: dbf    %%d0,3b\n"				\
399     "    move.w %2,%%d0\n"				\
400     "    lsr.w  #2,%%d0\n"				\
401     "    and.w  #7,%%d0\n"				\
402     "    bra    6f\n"					\
403     " 5: move.l (%0),(%1)+\n"				\
404     " 6: dbf    %%d0,5b\n"				\
405     "    and.w  #3,%2\n"				\
406     "    bra    8f\n"					\
407     " 7: move.b (%0),(%1)+\n"				\
408     " 8: dbf    %2,7b\n"				\
409     "    moveq.l #0, %2\n"				\
410     " 9: \n"						\
411     ".section .fixup,\"ax\"\n"				\
412     "    .even\n"					\
413     "90: moveq.l #1, %2\n"				\
414     "    jra 9b\n"					\
415     ".previous\n"					\
416     ".section __ex_table,\"a\"\n"			\
417     "   .align 4\n"					\
418     "   .long  1b,90b\n"				\
419     "   .long  3b,90b\n"				\
420     "   .long 31b,90b\n"				\
421     "   .long 32b,90b\n"				\
422     "   .long 33b,90b\n"				\
423     "   .long 34b,90b\n"				\
424     "   .long 35b,90b\n"				\
425     "   .long 36b,90b\n"				\
426     "   .long 37b,90b\n"				\
427     "   .long  5b,90b\n"				\
428     "   .long  7b,90b\n"				\
429     ".previous"					\
430     : "=a"(s), "=a"(d), "=d"(len)			\
431     : "0"(s), "1"(d), "2"(len)				\
432     : "d0")
433
434
435static int macscsi_pread (struct Scsi_Host *instance,
436			  unsigned char *dst, int len)
437{
438   unsigned char *d;
439   volatile unsigned char *s;
440
441   NCR5380_local_declare();
442   NCR5380_setup(instance);
443
444   s = mac_scsi_drq+0x60;
445   d = dst;
446
447/* These conditions are derived from MacOS */
448
449   while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
450         && !(NCR5380_read(STATUS_REG) & SR_REQ))
451      ;
452   if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
453         && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
454      printk(KERN_ERR "Error in macscsi_pread\n");
455      return -1;
456   }
457
458   CP_IO_TO_MEM(s, d, len);
459
460   if (len != 0) {
461      printk(KERN_NOTICE "Bus error in macscsi_pread\n");
462      return -1;
463   }
464
465   return 0;
466}
467
468
469#define CP_MEM_TO_IO(s,d,len)				\
470__asm__ __volatile__					\
471    ("    cmp.w  #4,%2\n"				\
472     "    bls    8f\n"					\
473     "    move.w %0,%%d0\n"				\
474     "    neg.b  %%d0\n"				\
475     "    and.w  #3,%%d0\n"				\
476     "    sub.w  %%d0,%2\n"				\
477     "    bra    2f\n"					\
478     " 1: move.b (%0)+,(%1)\n"				\
479     " 2: dbf    %%d0,1b\n"				\
480     "    move.w %2,%%d0\n"				\
481     "    lsr.w  #5,%%d0\n"				\
482     "    bra    4f\n"					\
483     " 3: move.l (%0)+,(%1)\n"				\
484     "31: move.l (%0)+,(%1)\n"				\
485     "32: move.l (%0)+,(%1)\n"				\
486     "33: move.l (%0)+,(%1)\n"				\
487     "34: move.l (%0)+,(%1)\n"				\
488     "35: move.l (%0)+,(%1)\n"				\
489     "36: move.l (%0)+,(%1)\n"				\
490     "37: move.l (%0)+,(%1)\n"				\
491     " 4: dbf    %%d0,3b\n"				\
492     "    move.w %2,%%d0\n"				\
493     "    lsr.w  #2,%%d0\n"				\
494     "    and.w  #7,%%d0\n"				\
495     "    bra    6f\n"					\
496     " 5: move.l (%0)+,(%1)\n"				\
497     " 6: dbf    %%d0,5b\n"				\
498     "    and.w  #3,%2\n"				\
499     "    bra    8f\n"					\
500     " 7: move.b (%0)+,(%1)\n"				\
501     " 8: dbf    %2,7b\n"				\
502     "    moveq.l #0, %2\n"				\
503     " 9: \n"						\
504     ".section .fixup,\"ax\"\n"				\
505     "    .even\n"					\
506     "90: moveq.l #1, %2\n"				\
507     "    jra 9b\n"					\
508     ".previous\n"					\
509     ".section __ex_table,\"a\"\n"			\
510     "   .align 4\n"					\
511     "   .long  1b,90b\n"				\
512     "   .long  3b,90b\n"				\
513     "   .long 31b,90b\n"				\
514     "   .long 32b,90b\n"				\
515     "   .long 33b,90b\n"				\
516     "   .long 34b,90b\n"				\
517     "   .long 35b,90b\n"				\
518     "   .long 36b,90b\n"				\
519     "   .long 37b,90b\n"				\
520     "   .long  5b,90b\n"				\
521     "   .long  7b,90b\n"				\
522     ".previous"					\
523     : "=a"(s), "=a"(d), "=d"(len)			\
524     : "0"(s), "1"(d), "2"(len)				\
525     : "d0")
526
527static int macscsi_pwrite (struct Scsi_Host *instance,
528				  unsigned char *src, int len)
529{
530   unsigned char *s;
531   volatile unsigned char *d;
532
533   NCR5380_local_declare();
534   NCR5380_setup(instance);
535
536   s = src;
537   d = mac_scsi_drq;
538
539/* These conditions are derived from MacOS */
540
541   while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
542         && (!(NCR5380_read(STATUS_REG) & SR_REQ)
543            || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
544      ;
545   if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
546      printk(KERN_ERR "Error in macscsi_pwrite\n");
547      return -1;
548   }
549
550   CP_MEM_TO_IO(s, d, len);
551
552   if (len != 0) {
553      printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
554      return -1;
555   }
556
557   return 0;
558}
559
560
561#include "NCR5380.c"
562
563static struct scsi_host_template driver_template = {
564	.proc_name			= "Mac5380",
565	.proc_info			= macscsi_proc_info,
566	.name				= "Macintosh NCR5380 SCSI",
567	.detect				= macscsi_detect,
568	.release			= macscsi_release,
569	.info				= macscsi_info,
570	.queuecommand			= macscsi_queue_command,
571	.eh_abort_handler		= macscsi_abort,
572	.eh_bus_reset_handler		= macscsi_bus_reset,
573	.can_queue			= CAN_QUEUE,
574	.this_id			= 7,
575	.sg_tablesize			= SG_ALL,
576	.cmd_per_lun			= CMD_PER_LUN,
577	.use_clustering			= DISABLE_CLUSTERING
578};
579
580
581#include "scsi_module.c"
582