Linux Audio

Check our new training course

Loading...
v3.1
 
  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, IRQ_FLG_SLOW, 
295			"ncr5380", instance)) {
296	    printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
297		   instance->host_no, instance->irq);
298	    instance->irq = SCSI_IRQ_NONE;
299	}
300
301    printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
302    if (instance->irq == SCSI_IRQ_NONE)
303	printk (KERN_INFO "s disabled");
304    else
305	printk (KERN_INFO " %d", instance->irq);
306    printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
307	   instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
308    printk(KERN_INFO "\nscsi%d:", instance->host_no);
309    NCR5380_print_options(instance);
310    printk("\n");
311    called = 1;
312    return 1;
313}
314
315int macscsi_release (struct Scsi_Host *shpnt)
 
316{
317	if (shpnt->irq != SCSI_IRQ_NONE)
318		free_irq(shpnt->irq, shpnt);
319	NCR5380_exit(shpnt);
320
321	return 0;
322}
323
324#ifdef RESET_BOOT
325/*
326 * Our 'bus reset on boot' function
327 */
328
329static void mac_scsi_reset_boot(struct Scsi_Host *instance)
330{
331	unsigned long end;
332
333	NCR5380_local_declare();
334	NCR5380_setup(instance);
335	
336	/*
337	 * Do a SCSI reset to clean up the bus during initialization. No messing
338	 * with the queues, interrupts, or locks necessary here.
339	 */
340
341	printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
342
343	/* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
344	disable_irq(IRQ_MAC_SCSI);
345
346	/* get in phase */
347	NCR5380_write( TARGET_COMMAND_REG,
348		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
349
350	/* assert RST */
351	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
352	/* The min. reset hold time is 25us, so 40us should be enough */
353	udelay( 50 );
354	/* reset RST and interrupt */
355	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
356	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
357
358	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
359		barrier();
360
361	/* switch on SCSI IRQ again */
362	enable_irq(IRQ_MAC_SCSI);
363
364	printk(KERN_INFO " done\n" );
365}
366#endif
367
368const char * macscsi_info (struct Scsi_Host *spnt) {
369	return "";
370}
371
372/* 
373   Pseudo-DMA: (Ove Edlund)
374   The code attempts to catch bus errors that occur if one for example
375   "trips over the cable".
376   XXX: Since bus errors in the PDMA routines never happen on my 
377   computer, the bus error code is untested. 
378   If the code works as intended, a bus error results in Pseudo-DMA 
379   beeing disabled, meaning that the driver switches to slow handshake. 
380   If bus errors are NOT extremely rare, this has to be changed. 
381*/
382
383#define CP_IO_TO_MEM(s,d,len)				\
384__asm__ __volatile__					\
385    ("    cmp.w  #4,%2\n"				\
386     "    bls    8f\n"					\
387     "    move.w %1,%%d0\n"				\
388     "    neg.b  %%d0\n"				\
389     "    and.w  #3,%%d0\n"				\
390     "    sub.w  %%d0,%2\n"				\
391     "    bra    2f\n"					\
392     " 1: move.b (%0),(%1)+\n"				\
393     " 2: dbf    %%d0,1b\n"				\
394     "    move.w %2,%%d0\n"				\
395     "    lsr.w  #5,%%d0\n"				\
396     "    bra    4f\n"					\
397     " 3: move.l (%0),(%1)+\n"				\
398     "31: move.l (%0),(%1)+\n"				\
399     "32: move.l (%0),(%1)+\n"				\
400     "33: move.l (%0),(%1)+\n"				\
401     "34: move.l (%0),(%1)+\n"				\
402     "35: move.l (%0),(%1)+\n"				\
403     "36: move.l (%0),(%1)+\n"				\
404     "37: move.l (%0),(%1)+\n"				\
405     " 4: dbf    %%d0,3b\n"				\
406     "    move.w %2,%%d0\n"				\
407     "    lsr.w  #2,%%d0\n"				\
408     "    and.w  #7,%%d0\n"				\
409     "    bra    6f\n"					\
410     " 5: move.l (%0),(%1)+\n"				\
411     " 6: dbf    %%d0,5b\n"				\
412     "    and.w  #3,%2\n"				\
413     "    bra    8f\n"					\
414     " 7: move.b (%0),(%1)+\n"				\
415     " 8: dbf    %2,7b\n"				\
416     "    moveq.l #0, %2\n"				\
417     " 9: \n"						\
418     ".section .fixup,\"ax\"\n"				\
419     "    .even\n"					\
420     "90: moveq.l #1, %2\n"				\
421     "    jra 9b\n"					\
422     ".previous\n"					\
423     ".section __ex_table,\"a\"\n"			\
424     "   .align 4\n"					\
425     "   .long  1b,90b\n"				\
426     "   .long  3b,90b\n"				\
427     "   .long 31b,90b\n"				\
428     "   .long 32b,90b\n"				\
429     "   .long 33b,90b\n"				\
430     "   .long 34b,90b\n"				\
431     "   .long 35b,90b\n"				\
432     "   .long 36b,90b\n"				\
433     "   .long 37b,90b\n"				\
434     "   .long  5b,90b\n"				\
435     "   .long  7b,90b\n"				\
436     ".previous"					\
437     : "=a"(s), "=a"(d), "=d"(len)			\
438     : "0"(s), "1"(d), "2"(len)				\
439     : "d0")
440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
442static int macscsi_pread (struct Scsi_Host *instance,
443			  unsigned char *dst, int len)
444{
445   unsigned char *d;
446   volatile unsigned char *s;
447
448   NCR5380_local_declare();
449   NCR5380_setup(instance);
450
451   s = mac_scsi_drq+0x60;
452   d = dst;
 
 
 
 
 
 
 
 
 
 
 
453
454/* These conditions are derived from MacOS */
455
456   while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
457         && !(NCR5380_read(STATUS_REG) & SR_REQ))
458      ;
459   if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
460         && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
461      printk(KERN_ERR "Error in macscsi_pread\n");
462      return -1;
463   }
464
465   CP_IO_TO_MEM(s, d, len);
466   
467   if (len != 0) {
468      printk(KERN_NOTICE "Bus error in macscsi_pread\n");
469      return -1;
470   }
471   
472   return 0;
473}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
 
475
476#define CP_MEM_TO_IO(s,d,len)				\
477__asm__ __volatile__					\
478    ("    cmp.w  #4,%2\n"				\
479     "    bls    8f\n"					\
480     "    move.w %0,%%d0\n"				\
481     "    neg.b  %%d0\n"				\
482     "    and.w  #3,%%d0\n"				\
483     "    sub.w  %%d0,%2\n"				\
484     "    bra    2f\n"					\
485     " 1: move.b (%0)+,(%1)\n"				\
486     " 2: dbf    %%d0,1b\n"				\
487     "    move.w %2,%%d0\n"				\
488     "    lsr.w  #5,%%d0\n"				\
489     "    bra    4f\n"					\
490     " 3: move.l (%0)+,(%1)\n"				\
491     "31: move.l (%0)+,(%1)\n"				\
492     "32: move.l (%0)+,(%1)\n"				\
493     "33: move.l (%0)+,(%1)\n"				\
494     "34: move.l (%0)+,(%1)\n"				\
495     "35: move.l (%0)+,(%1)\n"				\
496     "36: move.l (%0)+,(%1)\n"				\
497     "37: move.l (%0)+,(%1)\n"				\
498     " 4: dbf    %%d0,3b\n"				\
499     "    move.w %2,%%d0\n"				\
500     "    lsr.w  #2,%%d0\n"				\
501     "    and.w  #7,%%d0\n"				\
502     "    bra    6f\n"					\
503     " 5: move.l (%0)+,(%1)\n"				\
504     " 6: dbf    %%d0,5b\n"				\
505     "    and.w  #3,%2\n"				\
506     "    bra    8f\n"					\
507     " 7: move.b (%0)+,(%1)\n"				\
508     " 8: dbf    %2,7b\n"				\
509     "    moveq.l #0, %2\n"				\
510     " 9: \n"						\
511     ".section .fixup,\"ax\"\n"				\
512     "    .even\n"					\
513     "90: moveq.l #1, %2\n"				\
514     "    jra 9b\n"					\
515     ".previous\n"					\
516     ".section __ex_table,\"a\"\n"			\
517     "   .align 4\n"					\
518     "   .long  1b,90b\n"				\
519     "   .long  3b,90b\n"				\
520     "   .long 31b,90b\n"				\
521     "   .long 32b,90b\n"				\
522     "   .long 33b,90b\n"				\
523     "   .long 34b,90b\n"				\
524     "   .long 35b,90b\n"				\
525     "   .long 36b,90b\n"				\
526     "   .long 37b,90b\n"				\
527     "   .long  5b,90b\n"				\
528     "   .long  7b,90b\n"				\
529     ".previous"					\
530     : "=a"(s), "=a"(d), "=d"(len)			\
531     : "0"(s), "1"(d), "2"(len)				\
532     : "d0")
533
534static int macscsi_pwrite (struct Scsi_Host *instance,
535				  unsigned char *src, int len)
536{
537   unsigned char *s;
538   volatile unsigned char *d;
539
540   NCR5380_local_declare();
541   NCR5380_setup(instance);
542
543   s = src;
544   d = mac_scsi_drq;
545   
546/* These conditions are derived from MacOS */
547
548   while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) 
549         && (!(NCR5380_read(STATUS_REG) & SR_REQ) 
550            || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))) 
551      ;
552   if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
553      printk(KERN_ERR "Error in macscsi_pwrite\n");
554      return -1;
555   }
556
557   CP_MEM_TO_IO(s, d, len);   
558
559   if (len != 0) {
560      printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
561      return -1;
562   }
563   
564   return 0;
565}
566
 
 
 
567
568#include "NCR5380.c"
 
 
 
 
 
 
569
570static struct scsi_host_template driver_template = {
571	.proc_name			= "Mac5380",
572	.proc_info			= macscsi_proc_info,
573	.name				= "Macintosh NCR5380 SCSI",
574	.detect				= macscsi_detect,
575	.release			= macscsi_release,
576	.info				= macscsi_info,
577	.queuecommand			= macscsi_queue_command,
578	.eh_abort_handler		= macscsi_abort,
579	.eh_bus_reset_handler		= macscsi_bus_reset,
580	.can_queue			= CAN_QUEUE,
581	.this_id			= 7,
582	.sg_tablesize			= SG_ALL,
583	.cmd_per_lun			= CMD_PER_LUN,
584	.use_clustering			= DISABLE_CLUSTERING
585};
586
 
587
588#include "scsi_module.c"
 
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Generic Macintosh NCR5380 driver
  4 *
  5 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  6 *
  7 * Copyright 2019 Finn Thain
  8 *
  9 * derived in part from:
 10 */
 11/*
 12 * Generic Generic NCR5380 driver
 13 *
 14 * Copyright 1995, Russell King
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 15 */
 16
 
 
 
 
 
 
 
 17#include <linux/delay.h>
 18#include <linux/types.h>
 19#include <linux/module.h>
 
 20#include <linux/ioport.h>
 21#include <linux/init.h>
 22#include <linux/blkdev.h>
 23#include <linux/interrupt.h>
 24#include <linux/platform_device.h>
 25
 26#include <asm/hwtest.h>
 27#include <asm/io.h>
 
 
 
 28#include <asm/macintosh.h>
 29#include <asm/macints.h>
 30#include <asm/setup.h>
 31
 
 32#include <scsi/scsi_host.h>
 
 33
 34/* Definitions for the core NCR5380 driver. */
 
 
 35
 36#define NCR5380_implementation_fields   int pdma_residual
 37
 38#define NCR5380_read(reg)           in_8(hostdata->io + ((reg) << 4))
 39#define NCR5380_write(reg, value)   out_8(hostdata->io + ((reg) << 4), value)
 40
 41#define NCR5380_dma_xfer_len            macscsi_dma_xfer_len
 42#define NCR5380_dma_recv_setup          macscsi_pread
 43#define NCR5380_dma_send_setup          macscsi_pwrite
 44#define NCR5380_dma_residual            macscsi_dma_residual
 45
 46#define NCR5380_intr                    macscsi_intr
 47#define NCR5380_queue_command           macscsi_queue_command
 48#define NCR5380_abort                   macscsi_abort
 49#define NCR5380_host_reset              macscsi_host_reset
 50#define NCR5380_info                    macscsi_info
 51
 52#include "NCR5380.h"
 
 
 53
 
 54static int setup_can_queue = -1;
 55module_param(setup_can_queue, int, 0);
 56static int setup_cmd_per_lun = -1;
 57module_param(setup_cmd_per_lun, int, 0);
 58static int setup_sg_tablesize = -1;
 59module_param(setup_sg_tablesize, int, 0);
 60static int setup_use_pdma = 512;
 61module_param(setup_use_pdma, int, 0);
 
 62static int setup_hostid = -1;
 63module_param(setup_hostid, int, 0);
 64static int setup_toshiba_delay = -1;
 65module_param(setup_toshiba_delay, int, 0);
 66
 67#ifndef MODULE
 68static int __init mac_scsi_setup(char *str)
 69{
 70	int ints[8];
 71
 72	(void)get_options(str, ARRAY_SIZE(ints), ints);
 73
 74	if (ints[0] < 1) {
 75		pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
 76		return 0;
 77	}
 78	if (ints[0] >= 1)
 79		setup_can_queue = ints[1];
 80	if (ints[0] >= 2)
 81		setup_cmd_per_lun = ints[2];
 82	if (ints[0] >= 3)
 83		setup_sg_tablesize = ints[3];
 84	if (ints[0] >= 4)
 85		setup_hostid = ints[4];
 86	/* ints[5] (use_tagged_queuing) is ignored */
 87	if (ints[0] >= 6)
 88		setup_use_pdma = ints[6];
 89	if (ints[0] >= 7)
 90		setup_toshiba_delay = ints[7];
 91	return 1;
 92}
 93
 94__setup("mac5380=", mac_scsi_setup);
 95#endif /* !MODULE */
 96
 97/*
 98 * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
 99 * specify the number of bytes between the delays expected from a SCSI target.
100 * This allows the operating system to "prevent bus errors when a target fails
101 * to deliver the next byte within the processor bus error timeout period."
102 * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
103 * so bus errors are unavoidable.
104 *
105 * If a MOVE.B instruction faults, we assume that zero bytes were transferred
106 * and simply retry. That assumption probably depends on target behaviour but
107 * seems to hold up okay. The NOP provides synchronization: without it the
108 * fault can sometimes occur after the program counter has moved past the
109 * offending instruction. Post-increment addressing can't be used.
110 */
111
112#define MOVE_BYTE(operands) \
113	asm volatile ( \
114		"1:     moveb " operands "     \n" \
115		"11:    nop                    \n" \
116		"       addq #1,%0             \n" \
117		"       subq #1,%1             \n" \
118		"40:                           \n" \
119		"                              \n" \
120		".section .fixup,\"ax\"        \n" \
121		".even                         \n" \
122		"90:    movel #1, %2           \n" \
123		"       jra 40b                \n" \
124		".previous                     \n" \
125		"                              \n" \
126		".section __ex_table,\"a\"     \n" \
127		".align  4                     \n" \
128		".long   1b,90b                \n" \
129		".long  11b,90b                \n" \
130		".previous                     \n" \
131		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
132
133/*
134 * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
135 * the residual byte count would be uncertain. In that situation the MOVE_WORD
136 * macro clears n in the fixup section to abort the transfer.
137 */
138
139#define MOVE_WORD(operands) \
140	asm volatile ( \
141		"1:     movew " operands "     \n" \
142		"11:    nop                    \n" \
143		"       subq #2,%1             \n" \
144		"40:                           \n" \
145		"                              \n" \
146		".section .fixup,\"ax\"        \n" \
147		".even                         \n" \
148		"90:    movel #0, %1           \n" \
149		"       movel #2, %2           \n" \
150		"       jra 40b                \n" \
151		".previous                     \n" \
152		"                              \n" \
153		".section __ex_table,\"a\"     \n" \
154		".align  4                     \n" \
155		".long   1b,90b                \n" \
156		".long  11b,90b                \n" \
157		".previous                     \n" \
158		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
159
160#define MOVE_16_WORDS(operands) \
161	asm volatile ( \
162		"1:     movew " operands "     \n" \
163		"2:     movew " operands "     \n" \
164		"3:     movew " operands "     \n" \
165		"4:     movew " operands "     \n" \
166		"5:     movew " operands "     \n" \
167		"6:     movew " operands "     \n" \
168		"7:     movew " operands "     \n" \
169		"8:     movew " operands "     \n" \
170		"9:     movew " operands "     \n" \
171		"10:    movew " operands "     \n" \
172		"11:    movew " operands "     \n" \
173		"12:    movew " operands "     \n" \
174		"13:    movew " operands "     \n" \
175		"14:    movew " operands "     \n" \
176		"15:    movew " operands "     \n" \
177		"16:    movew " operands "     \n" \
178		"17:    nop                    \n" \
179		"       subl  #32,%1           \n" \
180		"40:                           \n" \
181		"                              \n" \
182		".section .fixup,\"ax\"        \n" \
183		".even                         \n" \
184		"90:    movel #0, %1           \n" \
185		"       movel #2, %2           \n" \
186		"       jra 40b                \n" \
187		".previous                     \n" \
188		"                              \n" \
189		".section __ex_table,\"a\"     \n" \
190		".align  4                     \n" \
191		".long   1b,90b                \n" \
192		".long   2b,90b                \n" \
193		".long   3b,90b                \n" \
194		".long   4b,90b                \n" \
195		".long   5b,90b                \n" \
196		".long   6b,90b                \n" \
197		".long   7b,90b                \n" \
198		".long   8b,90b                \n" \
199		".long   9b,90b                \n" \
200		".long  10b,90b                \n" \
201		".long  11b,90b                \n" \
202		".long  12b,90b                \n" \
203		".long  13b,90b                \n" \
204		".long  14b,90b                \n" \
205		".long  15b,90b                \n" \
206		".long  16b,90b                \n" \
207		".long  17b,90b                \n" \
208		".previous                     \n" \
209		: "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
210
211#define MAC_PDMA_DELAY		32
212
213static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
214{
215	unsigned char *addr = start;
216	int result = 0;
 
 
 
 
 
217
218	if (n >= 1) {
219		MOVE_BYTE("%3@,%0@");
220		if (result)
221			goto out;
222	}
223	if (n >= 1 && ((unsigned long)addr & 1)) {
224		MOVE_BYTE("%3@,%0@");
225		if (result)
226			goto out;
227	}
228	while (n >= 32)
229		MOVE_16_WORDS("%3@,%0@+");
230	while (n >= 2)
231		MOVE_WORD("%3@,%0@+");
232	if (result)
233		return start - addr; /* Negated to indicate uncertain length */
234	if (n == 1)
235		MOVE_BYTE("%3@,%0@");
236out:
237	return addr - start;
238}
239
240static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
241{
242	unsigned char *addr = start;
243	int result = 0;
244
245	if (n >= 1) {
246		MOVE_BYTE("%0@,%3@");
247		if (result)
248			goto out;
249	}
250	if (n >= 1 && ((unsigned long)addr & 1)) {
251		MOVE_BYTE("%0@,%3@");
252		if (result)
253			goto out;
254	}
255	while (n >= 32)
256		MOVE_16_WORDS("%0@+,%3@");
257	while (n >= 2)
258		MOVE_WORD("%0@+,%3@");
259	if (result)
260		return start - addr; /* Negated to indicate uncertain length */
261	if (n == 1)
262		MOVE_BYTE("%0@,%3@");
263out:
264	return addr - start;
265}
266
267/* The "SCSI DMA" chip on the IIfx implements this register. */
268#define CTRL_REG                0x8
269#define CTRL_INTERRUPTS_ENABLE  BIT(1)
270#define CTRL_HANDSHAKE_MODE     BIT(3)
271
272static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
 
273{
274	out_be32(hostdata->io + (CTRL_REG << 4), value);
275}
276
277static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
278                                unsigned char *dst, int len)
279{
280	u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
281	unsigned char *d = dst;
282	int result = 0;
283
284	hostdata->pdma_residual = len;
285
286	while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
287	                              BASR_DRQ | BASR_PHASE_MATCH,
288	                              BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
289		int bytes;
290
291		if (macintosh_config->ident == MAC_MODEL_IIFX)
292			write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
293			                         CTRL_INTERRUPTS_ENABLE);
294
295		bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
296
297		if (bytes > 0) {
298			d += bytes;
299			hostdata->pdma_residual -= bytes;
300		}
301
302		if (hostdata->pdma_residual == 0)
303			goto out;
304
305		if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
306		                           BUS_AND_STATUS_REG, BASR_ACK,
307		                           BASR_ACK, HZ / 64) < 0)
308			scmd_printk(KERN_DEBUG, hostdata->connected,
309			            "%s: !REQ and !ACK\n", __func__);
310		if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
311			goto out;
312
313		if (bytes == 0)
314			udelay(MAC_PDMA_DELAY);
315
316		if (bytes >= 0)
317			continue;
318
319		dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
320		         "%s: bus error (%d/%d)\n", __func__, d - dst, len);
321		NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
322		result = -1;
323		goto out;
 
 
 
 
 
 
 
 
 
 
 
324	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
326	scmd_printk(KERN_ERR, hostdata->connected,
327	            "%s: phase mismatch or !DRQ\n", __func__);
328	NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
329	result = -1;
330out:
331	if (macintosh_config->ident == MAC_MODEL_IIFX)
332		write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
333	return result;
334}
335
336static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
337                                 unsigned char *src, int len)
 
 
 
 
 
 
 
 
 
 
 
338{
339	unsigned char *s = src;
340	u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
341	int result = 0;
342
343	hostdata->pdma_residual = len;
344
345	while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
346	                              BASR_DRQ | BASR_PHASE_MATCH,
347	                              BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
348		int bytes;
349
350		if (macintosh_config->ident == MAC_MODEL_IIFX)
351			write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
352			                         CTRL_INTERRUPTS_ENABLE);
353
354		bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
355
356		if (bytes > 0) {
357			s += bytes;
358			hostdata->pdma_residual -= bytes;
359		}
360
361		if (hostdata->pdma_residual == 0) {
362			if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
363			                          TCR_LAST_BYTE_SENT,
364			                          TCR_LAST_BYTE_SENT,
365			                          HZ / 64) < 0) {
366				scmd_printk(KERN_ERR, hostdata->connected,
367				            "%s: Last Byte Sent timeout\n", __func__);
368				result = -1;
369			}
370			goto out;
371		}
372
373		if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
374		                           BUS_AND_STATUS_REG, BASR_ACK,
375		                           BASR_ACK, HZ / 64) < 0)
376			scmd_printk(KERN_DEBUG, hostdata->connected,
377			            "%s: !REQ and !ACK\n", __func__);
378		if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
379			goto out;
380
381		if (bytes == 0)
382			udelay(MAC_PDMA_DELAY);
383
384		if (bytes >= 0)
385			continue;
386
387		dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
388		         "%s: bus error (%d/%d)\n", __func__, s - src, len);
389		NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
390		result = -1;
391		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392	}
393
394	scmd_printk(KERN_ERR, hostdata->connected,
395	            "%s: phase mismatch or !DRQ\n", __func__);
396	NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
397	result = -1;
398out:
399	if (macintosh_config->ident == MAC_MODEL_IIFX)
400		write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
401	return result;
 
 
 
 
402}
403
404static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
405                                struct scsi_cmnd *cmd)
406{
407	if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
408	    cmd->SCp.this_residual < setup_use_pdma)
409		return 0;
410
411	return cmd->SCp.this_residual;
412}
413
414static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
 
 
 
 
 
415{
416	return hostdata->pdma_residual;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417}
 
418
419#include "NCR5380.c"
 
 
420
421#define DRV_MODULE_NAME         "mac_scsi"
422#define PFX                     DRV_MODULE_NAME ": "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
424static struct scsi_host_template mac_scsi_template = {
425	.module			= THIS_MODULE,
426	.proc_name		= DRV_MODULE_NAME,
427	.name			= "Macintosh NCR5380 SCSI",
428	.info			= macscsi_info,
429	.queuecommand		= macscsi_queue_command,
430	.eh_abort_handler	= macscsi_abort,
431	.eh_host_reset_handler	= macscsi_host_reset,
432	.can_queue		= 16,
433	.this_id		= 7,
434	.sg_tablesize		= 1,
435	.cmd_per_lun		= 2,
436	.dma_boundary		= PAGE_SIZE - 1,
437	.cmd_size		= NCR5380_CMD_SIZE,
438	.max_sectors		= 128,
439};
440
441static int __init mac_scsi_probe(struct platform_device *pdev)
 
442{
443	struct Scsi_Host *instance;
444	struct NCR5380_hostdata *hostdata;
445	int error;
446	int host_flags = 0;
447	struct resource *irq, *pio_mem, *pdma_mem = NULL;
448
449	pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
450	if (!pio_mem)
451		return -ENODEV;
452
453	pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
454
455	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
456
457	if (!hwreg_present((unsigned char *)pio_mem->start +
458	                   (STATUS_REG << 4))) {
459		pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
460		return -ENODEV;
461	}
462
463	if (setup_can_queue > 0)
464		mac_scsi_template.can_queue = setup_can_queue;
465	if (setup_cmd_per_lun > 0)
466		mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
467	if (setup_sg_tablesize >= 0)
468		mac_scsi_template.sg_tablesize = setup_sg_tablesize;
469	if (setup_hostid >= 0)
470		mac_scsi_template.this_id = setup_hostid & 7;
471
472	instance = scsi_host_alloc(&mac_scsi_template,
473	                           sizeof(struct NCR5380_hostdata));
474	if (!instance)
475		return -ENOMEM;
476
477	if (irq)
478		instance->irq = irq->start;
479	else
480		instance->irq = NO_IRQ;
481
482	hostdata = shost_priv(instance);
483	hostdata->base = pio_mem->start;
484	hostdata->io = (u8 __iomem *)pio_mem->start;
485
486	if (pdma_mem && setup_use_pdma)
487		hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
488	else
489		host_flags |= FLAG_NO_PSEUDO_DMA;
490
491	host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
492
493	error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
494	if (error)
495		goto fail_init;
496
497	if (instance->irq != NO_IRQ) {
498		error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
499		                    "NCR5380", instance);
500		if (error)
501			goto fail_irq;
502	}
503
504	NCR5380_maybe_reset_bus(instance);
505
506	error = scsi_add_host(instance, NULL);
507	if (error)
508		goto fail_host;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
510	platform_set_drvdata(pdev, instance);
 
 
 
 
511
512	scsi_scan_host(instance);
513	return 0;
514
515fail_host:
516	if (instance->irq != NO_IRQ)
517		free_irq(instance->irq, instance);
518fail_irq:
519	NCR5380_exit(instance);
520fail_init:
521	scsi_host_put(instance);
522	return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523}
524
525static int __exit mac_scsi_remove(struct platform_device *pdev)
526{
527	struct Scsi_Host *instance = platform_get_drvdata(pdev);
528
529	scsi_remove_host(instance);
530	if (instance->irq != NO_IRQ)
531		free_irq(instance->irq, instance);
532	NCR5380_exit(instance);
533	scsi_host_put(instance);
534	return 0;
535}
536
537static struct platform_driver mac_scsi_driver = {
538	.remove = __exit_p(mac_scsi_remove),
539	.driver = {
540		.name	= DRV_MODULE_NAME,
541	},
 
 
 
 
 
 
 
 
 
 
542};
543
544module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
545
546MODULE_ALIAS("platform:" DRV_MODULE_NAME);
547MODULE_LICENSE("GPL");