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