2 * Generic Macintosh NCR5380 driver
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
6 * derived in part from:
9 * Generic Generic NCR5380 driver
11 * Copyright 1995, Russell King
15 * For more information, please consult
18 * SCSI Protocol Controller
21 * NCR Microelectronics
22 * 1635 Aeroplaza Drive
23 * Colorado Springs, CO 80916
29 * $Log: mac_NCR5380.c,v $
32 #include <linux/types.h>
33 #include <linux/stddef.h>
34 #include <linux/ctype.h>
35 #include <linux/delay.h>
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>
46 #include <asm/system.h>
48 #include <asm/macintosh.h>
49 #include <asm/macints.h>
50 #include <asm/machw.h>
51 #include <asm/mac_via.h>
54 #include <scsi/scsi_host.h>
57 /* These control the behaviour of the generic 5380 core */
64 #define NDEBUG (NDEBUG_INTR | NDEBUG_PSEUDO_DMA | NDEBUG_ARBITRATION | NDEBUG_SELECTION | NDEBUG_RESELECTION)
66 #define NDEBUG (NDEBUG_ABORT)
72 extern void via_scsi_clear(void);
75 static void mac_scsi_reset_boot(struct Scsi_Host *instance);
78 static int setup_called = 0;
79 static int setup_can_queue = -1;
80 static int setup_cmd_per_lun = -1;
81 static int setup_sg_tablesize = -1;
82 static int setup_use_pdma = -1;
84 static int setup_use_tagged_queuing = -1;
86 static int setup_hostid = -1;
88 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
89 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
90 * need ten times the standard value... */
94 #define AFTER_RESET_DELAY (5*HZ/2)
96 #define AFTER_RESET_DELAY (HZ/2)
99 static volatile unsigned char *mac_scsi_regp = NULL;
100 static volatile unsigned char *mac_scsi_drq = NULL;
101 static volatile unsigned char *mac_scsi_nodrq = NULL;
105 * NCR 5380 register access functions
110 #define CTRL(p,v) (*ctrl = (v))
112 static char macscsi_read(struct Scsi_Host *instance, int reg)
114 int iobase = instance->io_port;
116 int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
119 i = in_8(iobase + (reg<<4));
125 static void macscsi_write(struct Scsi_Host *instance, int reg, int value)
127 int iobase = instance->io_port;
128 int *ctrl = &((struct NCR5380_hostdata *)instance->hostdata)->ctrl;
131 out_8(iobase + (reg<<4), value);
137 static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
139 return in_8(instance->io_port + (reg<<4));
142 static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
144 out_8(instance->io_port + (reg<<4), value);
150 * Function : mac_scsi_setup(char *str)
152 * Purpose : booter command line initialization of the overrides array,
154 * Inputs : str - comma delimited list of options
158 static int __init mac_scsi_setup(char *str) {
162 (void)get_options( str, ARRAY_SIZE(ints), ints);
164 if (setup_called++ || ints[0] < 1 || ints[0] > 6) {
165 printk(KERN_WARNING "scsi: <mac5380>"
166 " Usage: mac5380=<can_queue>[,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>,<use_pdma>]\n");
167 printk(KERN_ALERT "scsi: <mac5380> Bad Penguin parameters?\n");
173 /* no limits on this, just > 0 */
174 setup_can_queue = ints[1];
178 setup_cmd_per_lun = ints[2];
182 setup_sg_tablesize = ints[3];
183 /* Must be <= SG_ALL (255) */
184 if (setup_sg_tablesize > SG_ALL)
185 setup_sg_tablesize = SG_ALL;
189 /* Must be between 0 and 7 */
190 if (ints[4] >= 0 && ints[4] <= 7)
191 setup_hostid = ints[4];
192 else if (ints[4] > 7)
193 printk(KERN_WARNING "mac_scsi_setup: invalid host ID %d !\n", ints[4] );
198 setup_use_tagged_queuing = !!ints[5];
203 setup_use_pdma = ints[6];
208 setup_use_pdma = ints[5];
210 #endif /* SUPPORT_TAGS */
212 #endif /* DRIVER_SETUP */
216 __setup("mac5380=", mac_scsi_setup);
219 * If you want to find the instance with (k)gdb ...
222 static struct Scsi_Host *default_instance;
226 * Function : int macscsi_detect(struct scsi_host_template * tpnt)
228 * Purpose : initializes mac NCR5380 driver based on the
229 * command line / compile time port and irq definitions.
231 * Inputs : tpnt - template for this SCSI adapter.
233 * Returns : 1 if a host adapter was found, 0 if not.
237 int macscsi_detect(struct scsi_host_template * tpnt)
239 static int called = 0;
241 struct Scsi_Host *instance;
243 if (!MACH_IS_MAC || called)
246 if (macintosh_config->scsi_type != MAC_SCSI_OLD)
249 /* setup variables */
251 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
253 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
255 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
257 if (setup_hostid >= 0)
258 tpnt->this_id = setup_hostid;
260 /* use 7 as default */
265 if (setup_use_tagged_queuing < 0)
266 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
269 /* Once we support multiple 5380s (e.g. DuoDock) we'll do
270 something different here */
271 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
273 default_instance = instance;
276 if (macintosh_config->ident == MAC_MODEL_IIFX) {
277 mac_scsi_regp = via1+0x8000;
278 mac_scsi_drq = via1+0xE000;
279 mac_scsi_nodrq = via1+0xC000;
280 /* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
281 flags = FLAG_NO_PSEUDO_DMA;
283 mac_scsi_regp = via1+0x10000;
284 mac_scsi_drq = via1+0x6000;
285 mac_scsi_nodrq = via1+0x12000;
288 if (! setup_use_pdma)
289 flags = FLAG_NO_PSEUDO_DMA;
291 instance->io_port = (unsigned long) mac_scsi_regp;
292 instance->irq = IRQ_MAC_SCSI;
295 mac_scsi_reset_boot(instance);
298 NCR5380_init(instance, flags);
300 instance->n_io_port = 255;
302 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
304 if (instance->irq != SCSI_IRQ_NONE)
305 if (request_irq(instance->irq, NCR5380_intr, IRQ_FLG_SLOW,
306 "ncr5380", instance)) {
307 printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
308 instance->host_no, instance->irq);
309 instance->irq = SCSI_IRQ_NONE;
312 printk(KERN_INFO "scsi%d: generic 5380 at port %lX irq", instance->host_no, instance->io_port);
313 if (instance->irq == SCSI_IRQ_NONE)
314 printk (KERN_INFO "s disabled");
316 printk (KERN_INFO " %d", instance->irq);
317 printk(KERN_INFO " options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
318 instance->can_queue, instance->cmd_per_lun, MACSCSI_PUBLIC_RELEASE);
319 printk(KERN_INFO "\nscsi%d:", instance->host_no);
320 NCR5380_print_options(instance);
326 int macscsi_release (struct Scsi_Host *shpnt)
328 if (shpnt->irq != SCSI_IRQ_NONE)
329 free_irq(shpnt->irq, shpnt);
337 * Our 'bus reset on boot' function
340 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
344 NCR5380_local_declare();
345 NCR5380_setup(instance);
348 * Do a SCSI reset to clean up the bus during initialization. No messing
349 * with the queues, interrupts, or locks necessary here.
352 printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
354 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
355 disable_irq(IRQ_MAC_SCSI);
358 NCR5380_write( TARGET_COMMAND_REG,
359 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
362 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
363 /* The min. reset hold time is 25us, so 40us should be enough */
365 /* reset RST and interrupt */
366 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
367 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
369 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
372 /* switch on SCSI IRQ again */
373 enable_irq(IRQ_MAC_SCSI);
375 printk(KERN_INFO " done\n" );
379 const char * macscsi_info (struct Scsi_Host *spnt) {
384 Pseudo-DMA: (Ove Edlund)
385 The code attempts to catch bus errors that occur if one for example
386 "trips over the cable".
387 XXX: Since bus errors in the PDMA routines never happen on my
388 computer, the bus error code is untested.
389 If the code works as intended, a bus error results in Pseudo-DMA
390 beeing disabled, meaning that the driver switches to slow handshake.
391 If bus errors are NOT extremely rare, this has to be changed.
394 #define CP_IO_TO_MEM(s,d,len) \
395 __asm__ __volatile__ \
398 " move.w %1,%%d0\n" \
403 " 1: move.b (%0),(%1)+\n" \
404 " 2: dbf %%d0,1b\n" \
405 " move.w %2,%%d0\n" \
408 " 3: move.l (%0),(%1)+\n" \
409 "31: move.l (%0),(%1)+\n" \
410 "32: move.l (%0),(%1)+\n" \
411 "33: move.l (%0),(%1)+\n" \
412 "34: move.l (%0),(%1)+\n" \
413 "35: move.l (%0),(%1)+\n" \
414 "36: move.l (%0),(%1)+\n" \
415 "37: move.l (%0),(%1)+\n" \
416 " 4: dbf %%d0,3b\n" \
417 " move.w %2,%%d0\n" \
421 " 5: move.l (%0),(%1)+\n" \
422 " 6: dbf %%d0,5b\n" \
425 " 7: move.b (%0),(%1)+\n" \
427 " moveq.l #0, %2\n" \
429 ".section .fixup,\"ax\"\n" \
431 "90: moveq.l #1, %2\n" \
434 ".section __ex_table,\"a\"\n" \
448 : "=a"(s), "=a"(d), "=d"(len) \
449 : "0"(s), "1"(d), "2"(len) \
453 static int macscsi_pread (struct Scsi_Host *instance,
454 unsigned char *dst, int len)
457 volatile unsigned char *s;
459 NCR5380_local_declare();
460 NCR5380_setup(instance);
462 s = mac_scsi_drq+0x60;
465 /* These conditions are derived from MacOS */
467 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
468 && !(NCR5380_read(STATUS_REG) & SR_REQ))
470 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
471 && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
472 printk(KERN_ERR "Error in macscsi_pread\n");
476 CP_IO_TO_MEM(s, d, len);
479 printk(KERN_NOTICE "Bus error in macscsi_pread\n");
487 #define CP_MEM_TO_IO(s,d,len) \
488 __asm__ __volatile__ \
491 " move.w %0,%%d0\n" \
496 " 1: move.b (%0)+,(%1)\n" \
497 " 2: dbf %%d0,1b\n" \
498 " move.w %2,%%d0\n" \
501 " 3: move.l (%0)+,(%1)\n" \
502 "31: move.l (%0)+,(%1)\n" \
503 "32: move.l (%0)+,(%1)\n" \
504 "33: move.l (%0)+,(%1)\n" \
505 "34: move.l (%0)+,(%1)\n" \
506 "35: move.l (%0)+,(%1)\n" \
507 "36: move.l (%0)+,(%1)\n" \
508 "37: move.l (%0)+,(%1)\n" \
509 " 4: dbf %%d0,3b\n" \
510 " move.w %2,%%d0\n" \
514 " 5: move.l (%0)+,(%1)\n" \
515 " 6: dbf %%d0,5b\n" \
518 " 7: move.b (%0)+,(%1)\n" \
520 " moveq.l #0, %2\n" \
522 ".section .fixup,\"ax\"\n" \
524 "90: moveq.l #1, %2\n" \
527 ".section __ex_table,\"a\"\n" \
541 : "=a"(s), "=a"(d), "=d"(len) \
542 : "0"(s), "1"(d), "2"(len) \
545 static int macscsi_pwrite (struct Scsi_Host *instance,
546 unsigned char *src, int len)
549 volatile unsigned char *d;
551 NCR5380_local_declare();
552 NCR5380_setup(instance);
557 /* These conditions are derived from MacOS */
559 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
560 && (!(NCR5380_read(STATUS_REG) & SR_REQ)
561 || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
563 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
564 printk(KERN_ERR "Error in macscsi_pwrite\n");
568 CP_MEM_TO_IO(s, d, len);
571 printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
581 static struct scsi_host_template driver_template = {
582 .proc_name = "Mac5380",
583 .proc_info = macscsi_proc_info,
584 .name = "Macintosh NCR5380 SCSI",
585 .detect = macscsi_detect,
586 .release = macscsi_release,
587 .info = macscsi_info,
588 .queuecommand = macscsi_queue_command,
589 .eh_abort_handler = macscsi_abort,
590 .eh_bus_reset_handler = macscsi_bus_reset,
591 .can_queue = CAN_QUEUE,
593 .sg_tablesize = SG_ALL,
594 .cmd_per_lun = CMD_PER_LUN,
595 .unchecked_isa_dma = 0,
596 .use_clustering = DISABLE_CLUSTERING
600 #include "scsi_module.c"