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