blob: 3b93f000499a1bff21ed0dcc73c91238d67e00b1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int setup_can_queue = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110066module_param(setup_can_queue, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int setup_cmd_per_lun = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110068module_param(setup_cmd_per_lun, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int setup_sg_tablesize = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110070module_param(setup_sg_tablesize, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int setup_use_pdma = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110072module_param(setup_use_pdma, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int setup_use_tagged_queuing = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110074module_param(setup_use_tagged_queuing, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int setup_hostid = -1;
Finn Thain6e9ae6d2014-11-12 16:12:05 +110076module_param(setup_hostid, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
79 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
80 * need ten times the standard value... */
81#define TOSHIBA_DELAY
82
83#ifdef TOSHIBA_DELAY
84#define AFTER_RESET_DELAY (5*HZ/2)
85#else
86#define AFTER_RESET_DELAY (HZ/2)
87#endif
88
89static volatile unsigned char *mac_scsi_regp = NULL;
90static volatile unsigned char *mac_scsi_drq = NULL;
91static volatile unsigned char *mac_scsi_nodrq = NULL;
92
93
94/*
95 * NCR 5380 register access functions
96 */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static __inline__ char macscsi_read(struct Scsi_Host *instance, int reg)
99{
100 return in_8(instance->io_port + (reg<<4));
101}
102
103static __inline__ void macscsi_write(struct Scsi_Host *instance, int reg, int value)
104{
105 out_8(instance->io_port + (reg<<4), value);
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100108#ifndef MODULE
109static int __init mac_scsi_setup(char *str)
110{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int ints[7];
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100112
113 (void)get_options(str, ARRAY_SIZE(ints), ints);
114
115 if (ints[0] < 1 || ints[0] > 6) {
116 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>]]]]]\n");
117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100119 if (ints[0] >= 1)
120 setup_can_queue = ints[1];
121 if (ints[0] >= 2)
122 setup_cmd_per_lun = ints[2];
123 if (ints[0] >= 3)
124 setup_sg_tablesize = ints[3];
125 if (ints[0] >= 4)
126 setup_hostid = ints[4];
127 if (ints[0] >= 5)
128 setup_use_tagged_queuing = ints[5];
129 if (ints[0] >= 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 setup_use_pdma = ints[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 1;
132}
133
134__setup("mac5380=", mac_scsi_setup);
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100135#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100138 * Function : int macscsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * Purpose : initializes mac NCR5380 driver based on the
141 * command line / compile time port and irq definitions.
142 *
143 * Inputs : tpnt - template for this SCSI adapter.
144 *
145 * Returns : 1 if a host adapter was found, 0 if not.
146 *
147 */
148
Geert Uytterhoeven70c26cf2011-06-13 20:39:16 +0200149int __init macscsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 static int called = 0;
152 int flags = 0;
153 struct Scsi_Host *instance;
154
155 if (!MACH_IS_MAC || called)
156 return( 0 );
157
158 if (macintosh_config->scsi_type != MAC_SCSI_OLD)
159 return( 0 );
160
Finn Thaind572f652014-11-12 16:12:00 +1100161 if (setup_can_queue > 0)
162 tpnt->can_queue = setup_can_queue;
163 if (setup_cmd_per_lun > 0)
164 tpnt->cmd_per_lun = setup_cmd_per_lun;
165 if (setup_sg_tablesize >= 0)
166 tpnt->sg_tablesize = setup_sg_tablesize;
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100167 if (setup_hostid >= 0)
168 tpnt->this_id = setup_hostid & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170#ifdef SUPPORT_TAGS
171 if (setup_use_tagged_queuing < 0)
Finn Thaind572f652014-11-12 16:12:00 +1100172 setup_use_tagged_queuing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif
174
175 /* Once we support multiple 5380s (e.g. DuoDock) we'll do
176 something different here */
177 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
Alan603f2022013-12-03 16:11:04 +0000178 if (instance == NULL)
179 return 0;
Geert Uytterhoeven5db5c502011-06-13 20:39:17 +0200180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (macintosh_config->ident == MAC_MODEL_IIFX) {
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100182 mac_scsi_regp = (unsigned char *) VIA1_BASE + 0x8000;
183 mac_scsi_drq = (unsigned char *) VIA1_BASE + 0xE000;
184 mac_scsi_nodrq = (unsigned char *) VIA1_BASE + 0xC000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* The IIFX should be able to do true DMA, but pseudo-dma doesn't work */
186 flags = FLAG_NO_PSEUDO_DMA;
187 } else {
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100188 mac_scsi_regp = (unsigned char *) VIA1_BASE + 0x10000;
189 mac_scsi_drq = (unsigned char *) VIA1_BASE + 0x6000;
190 mac_scsi_nodrq = (unsigned char *) VIA1_BASE + 0x12000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 if (! setup_use_pdma)
194 flags = FLAG_NO_PSEUDO_DMA;
195
196 instance->io_port = (unsigned long) mac_scsi_regp;
197 instance->irq = IRQ_MAC_SCSI;
198
199#ifdef RESET_BOOT
200 mac_scsi_reset_boot(instance);
201#endif
202
203 NCR5380_init(instance, flags);
204
205 instance->n_io_port = 255;
206
Finn Thain22f5f102014-11-12 16:11:56 +1100207 if (instance->irq != NO_IRQ)
Geert Uytterhoevendddaaf72011-12-11 10:04:51 +0100208 if (request_irq(instance->irq, NCR5380_intr, 0, "ncr5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 printk(KERN_WARNING "scsi%d: IRQ%d not free, interrupts disabled\n",
210 instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100211 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 called = 1;
215 return 1;
216}
217
218int macscsi_release (struct Scsi_Host *shpnt)
219{
Finn Thain22f5f102014-11-12 16:11:56 +1100220 if (shpnt->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500221 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 NCR5380_exit(shpnt);
223
224 return 0;
225}
226
227#ifdef RESET_BOOT
228/*
229 * Our 'bus reset on boot' function
230 */
231
232static void mac_scsi_reset_boot(struct Scsi_Host *instance)
233{
234 unsigned long end;
235
236 NCR5380_local_declare();
237 NCR5380_setup(instance);
238
239 /*
240 * Do a SCSI reset to clean up the bus during initialization. No messing
241 * with the queues, interrupts, or locks necessary here.
242 */
243
244 printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* get in phase */
247 NCR5380_write( TARGET_COMMAND_REG,
248 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
249
250 /* assert RST */
251 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
252 /* The min. reset hold time is 25us, so 40us should be enough */
253 udelay( 50 );
254 /* reset RST and interrupt */
255 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
256 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
257
258 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
259 barrier();
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 printk(KERN_INFO " done\n" );
262}
263#endif
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
266 Pseudo-DMA: (Ove Edlund)
267 The code attempts to catch bus errors that occur if one for example
268 "trips over the cable".
269 XXX: Since bus errors in the PDMA routines never happen on my
270 computer, the bus error code is untested.
271 If the code works as intended, a bus error results in Pseudo-DMA
272 beeing disabled, meaning that the driver switches to slow handshake.
273 If bus errors are NOT extremely rare, this has to be changed.
274*/
275
276#define CP_IO_TO_MEM(s,d,len) \
277__asm__ __volatile__ \
278 (" cmp.w #4,%2\n" \
279 " bls 8f\n" \
280 " move.w %1,%%d0\n" \
281 " neg.b %%d0\n" \
282 " and.w #3,%%d0\n" \
283 " sub.w %%d0,%2\n" \
284 " bra 2f\n" \
285 " 1: move.b (%0),(%1)+\n" \
286 " 2: dbf %%d0,1b\n" \
287 " move.w %2,%%d0\n" \
288 " lsr.w #5,%%d0\n" \
289 " bra 4f\n" \
290 " 3: move.l (%0),(%1)+\n" \
291 "31: move.l (%0),(%1)+\n" \
292 "32: move.l (%0),(%1)+\n" \
293 "33: move.l (%0),(%1)+\n" \
294 "34: move.l (%0),(%1)+\n" \
295 "35: move.l (%0),(%1)+\n" \
296 "36: move.l (%0),(%1)+\n" \
297 "37: move.l (%0),(%1)+\n" \
298 " 4: dbf %%d0,3b\n" \
299 " move.w %2,%%d0\n" \
300 " lsr.w #2,%%d0\n" \
301 " and.w #7,%%d0\n" \
302 " bra 6f\n" \
303 " 5: move.l (%0),(%1)+\n" \
304 " 6: dbf %%d0,5b\n" \
305 " and.w #3,%2\n" \
306 " bra 8f\n" \
307 " 7: move.b (%0),(%1)+\n" \
308 " 8: dbf %2,7b\n" \
309 " moveq.l #0, %2\n" \
310 " 9: \n" \
311 ".section .fixup,\"ax\"\n" \
312 " .even\n" \
313 "90: moveq.l #1, %2\n" \
314 " jra 9b\n" \
315 ".previous\n" \
316 ".section __ex_table,\"a\"\n" \
317 " .align 4\n" \
318 " .long 1b,90b\n" \
319 " .long 3b,90b\n" \
320 " .long 31b,90b\n" \
321 " .long 32b,90b\n" \
322 " .long 33b,90b\n" \
323 " .long 34b,90b\n" \
324 " .long 35b,90b\n" \
325 " .long 36b,90b\n" \
326 " .long 37b,90b\n" \
327 " .long 5b,90b\n" \
328 " .long 7b,90b\n" \
329 ".previous" \
330 : "=a"(s), "=a"(d), "=d"(len) \
331 : "0"(s), "1"(d), "2"(len) \
332 : "d0")
333
334
335static int macscsi_pread (struct Scsi_Host *instance,
336 unsigned char *dst, int len)
337{
338 unsigned char *d;
339 volatile unsigned char *s;
340
341 NCR5380_local_declare();
342 NCR5380_setup(instance);
343
344 s = mac_scsi_drq+0x60;
345 d = dst;
346
347/* These conditions are derived from MacOS */
348
349 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
350 && !(NCR5380_read(STATUS_REG) & SR_REQ))
351 ;
352 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
353 && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
354 printk(KERN_ERR "Error in macscsi_pread\n");
355 return -1;
356 }
357
358 CP_IO_TO_MEM(s, d, len);
359
360 if (len != 0) {
361 printk(KERN_NOTICE "Bus error in macscsi_pread\n");
362 return -1;
363 }
364
365 return 0;
366}
367
368
369#define CP_MEM_TO_IO(s,d,len) \
370__asm__ __volatile__ \
371 (" cmp.w #4,%2\n" \
372 " bls 8f\n" \
373 " move.w %0,%%d0\n" \
374 " neg.b %%d0\n" \
375 " and.w #3,%%d0\n" \
376 " sub.w %%d0,%2\n" \
377 " bra 2f\n" \
378 " 1: move.b (%0)+,(%1)\n" \
379 " 2: dbf %%d0,1b\n" \
380 " move.w %2,%%d0\n" \
381 " lsr.w #5,%%d0\n" \
382 " bra 4f\n" \
383 " 3: move.l (%0)+,(%1)\n" \
384 "31: move.l (%0)+,(%1)\n" \
385 "32: move.l (%0)+,(%1)\n" \
386 "33: move.l (%0)+,(%1)\n" \
387 "34: move.l (%0)+,(%1)\n" \
388 "35: move.l (%0)+,(%1)\n" \
389 "36: move.l (%0)+,(%1)\n" \
390 "37: move.l (%0)+,(%1)\n" \
391 " 4: dbf %%d0,3b\n" \
392 " move.w %2,%%d0\n" \
393 " lsr.w #2,%%d0\n" \
394 " and.w #7,%%d0\n" \
395 " bra 6f\n" \
396 " 5: move.l (%0)+,(%1)\n" \
397 " 6: dbf %%d0,5b\n" \
398 " and.w #3,%2\n" \
399 " bra 8f\n" \
400 " 7: move.b (%0)+,(%1)\n" \
401 " 8: dbf %2,7b\n" \
402 " moveq.l #0, %2\n" \
403 " 9: \n" \
404 ".section .fixup,\"ax\"\n" \
405 " .even\n" \
406 "90: moveq.l #1, %2\n" \
407 " jra 9b\n" \
408 ".previous\n" \
409 ".section __ex_table,\"a\"\n" \
410 " .align 4\n" \
411 " .long 1b,90b\n" \
412 " .long 3b,90b\n" \
413 " .long 31b,90b\n" \
414 " .long 32b,90b\n" \
415 " .long 33b,90b\n" \
416 " .long 34b,90b\n" \
417 " .long 35b,90b\n" \
418 " .long 36b,90b\n" \
419 " .long 37b,90b\n" \
420 " .long 5b,90b\n" \
421 " .long 7b,90b\n" \
422 ".previous" \
423 : "=a"(s), "=a"(d), "=d"(len) \
424 : "0"(s), "1"(d), "2"(len) \
425 : "d0")
426
427static int macscsi_pwrite (struct Scsi_Host *instance,
428 unsigned char *src, int len)
429{
430 unsigned char *s;
431 volatile unsigned char *d;
432
433 NCR5380_local_declare();
434 NCR5380_setup(instance);
435
436 s = src;
437 d = mac_scsi_drq;
438
439/* These conditions are derived from MacOS */
440
441 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)
442 && (!(NCR5380_read(STATUS_REG) & SR_REQ)
443 || (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
444 ;
445 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
446 printk(KERN_ERR "Error in macscsi_pwrite\n");
447 return -1;
448 }
449
450 CP_MEM_TO_IO(s, d, len);
451
452 if (len != 0) {
453 printk(KERN_NOTICE "Bus error in macscsi_pwrite\n");
454 return -1;
455 }
456
457 return 0;
458}
459
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#include "NCR5380.c"
462
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100463static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .proc_name = "Mac5380",
Al Virodd7ab712013-03-31 01:15:54 -0400465 .show_info = macscsi_show_info,
466 .write_info = macscsi_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .name = "Macintosh NCR5380 SCSI",
468 .detect = macscsi_detect,
469 .release = macscsi_release,
470 .info = macscsi_info,
471 .queuecommand = macscsi_queue_command,
472 .eh_abort_handler = macscsi_abort,
473 .eh_bus_reset_handler = macscsi_bus_reset,
Finn Thaind572f652014-11-12 16:12:00 +1100474 .can_queue = 16,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 .this_id = 7,
476 .sg_tablesize = SG_ALL,
Finn Thaind572f652014-11-12 16:12:00 +1100477 .cmd_per_lun = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 .use_clustering = DISABLE_CLUSTERING
479};
480
481
482#include "scsi_module.c"
Finn Thain6e9ae6d2014-11-12 16:12:05 +1100483
484MODULE_LICENSE("GPL");