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