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