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