xref: /openbmc/linux/drivers/scsi/mac_scsi.c (revision 8684014d)
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 
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
19 #include <linux/blkdev.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 
23 #include <asm/hwtest.h>
24 #include <asm/io.h>
25 #include <asm/macints.h>
26 #include <asm/setup.h>
27 
28 #include <scsi/scsi_host.h>
29 
30 /* Definitions for the core NCR5380 driver. */
31 
32 #define PSEUDO_DMA
33 
34 #define NCR5380_implementation_fields   unsigned char *pdma_base
35 #define NCR5380_local_declare()         struct Scsi_Host *_instance
36 #define NCR5380_setup(instance)         _instance = instance
37 
38 #define NCR5380_read(reg)               macscsi_read(_instance, reg)
39 #define NCR5380_write(reg, value)       macscsi_write(_instance, reg, value)
40 
41 #define NCR5380_pread                   macscsi_pread
42 #define NCR5380_pwrite                  macscsi_pwrite
43 
44 #define NCR5380_intr                    macscsi_intr
45 #define NCR5380_queue_command           macscsi_queue_command
46 #define NCR5380_abort                   macscsi_abort
47 #define NCR5380_bus_reset               macscsi_bus_reset
48 #define NCR5380_info                    macscsi_info
49 #define NCR5380_show_info               macscsi_show_info
50 #define NCR5380_write_info              macscsi_write_info
51 
52 #include "NCR5380.h"
53 
54 #define RESET_BOOT
55 
56 static int setup_can_queue = -1;
57 module_param(setup_can_queue, int, 0);
58 static int setup_cmd_per_lun = -1;
59 module_param(setup_cmd_per_lun, int, 0);
60 static int setup_sg_tablesize = -1;
61 module_param(setup_sg_tablesize, int, 0);
62 static int setup_use_pdma = -1;
63 module_param(setup_use_pdma, int, 0);
64 static int setup_use_tagged_queuing = -1;
65 module_param(setup_use_tagged_queuing, int, 0);
66 static int setup_hostid = -1;
67 module_param(setup_hostid, int, 0);
68 
69 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
70  * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
71  * need ten times the standard value... */
72 #define TOSHIBA_DELAY
73 
74 #ifdef TOSHIBA_DELAY
75 #define	AFTER_RESET_DELAY	(5*HZ/2)
76 #else
77 #define	AFTER_RESET_DELAY	(HZ/2)
78 #endif
79 
80 /*
81  * NCR 5380 register access functions
82  */
83 
84 static inline char macscsi_read(struct Scsi_Host *instance, int reg)
85 {
86 	return in_8(instance->base + (reg << 4));
87 }
88 
89 static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
90 {
91 	out_8(instance->base + (reg << 4), value);
92 }
93 
94 #ifndef MODULE
95 static int __init mac_scsi_setup(char *str)
96 {
97 	int ints[7];
98 
99 	(void)get_options(str, ARRAY_SIZE(ints), ints);
100 
101 	if (ints[0] < 1 || ints[0] > 6) {
102 		pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>]]]]]\n");
103 		return 0;
104 	}
105 	if (ints[0] >= 1)
106 		setup_can_queue = ints[1];
107 	if (ints[0] >= 2)
108 		setup_cmd_per_lun = ints[2];
109 	if (ints[0] >= 3)
110 		setup_sg_tablesize = ints[3];
111 	if (ints[0] >= 4)
112 		setup_hostid = ints[4];
113 	if (ints[0] >= 5)
114 		setup_use_tagged_queuing = ints[5];
115 	if (ints[0] >= 6)
116 		setup_use_pdma = ints[6];
117 	return 1;
118 }
119 
120 __setup("mac5380=", mac_scsi_setup);
121 #endif /* !MODULE */
122 
123 #ifdef RESET_BOOT
124 /*
125  * Our 'bus reset on boot' function
126  */
127 
128 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
129 {
130 	unsigned long end;
131 
132 	NCR5380_local_declare();
133 	NCR5380_setup(instance);
134 
135 	/*
136 	 * Do a SCSI reset to clean up the bus during initialization. No messing
137 	 * with the queues, interrupts, or locks necessary here.
138 	 */
139 
140 	printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
141 
142 	/* get in phase */
143 	NCR5380_write( TARGET_COMMAND_REG,
144 		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
145 
146 	/* assert RST */
147 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
148 	/* The min. reset hold time is 25us, so 40us should be enough */
149 	udelay( 50 );
150 	/* reset RST and interrupt */
151 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
152 	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
153 
154 	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
155 		barrier();
156 
157 	printk(KERN_INFO " done\n" );
158 }
159 #endif
160 
161 #ifdef PSEUDO_DMA
162 /*
163    Pseudo-DMA: (Ove Edlund)
164    The code attempts to catch bus errors that occur if one for example
165    "trips over the cable".
166    XXX: Since bus errors in the PDMA routines never happen on my
167    computer, the bus error code is untested.
168    If the code works as intended, a bus error results in Pseudo-DMA
169    being disabled, meaning that the driver switches to slow handshake.
170    If bus errors are NOT extremely rare, this has to be changed.
171 */
172 
173 #define CP_IO_TO_MEM(s,d,len)				\
174 __asm__ __volatile__					\
175     ("    cmp.w  #4,%2\n"				\
176      "    bls    8f\n"					\
177      "    move.w %1,%%d0\n"				\
178      "    neg.b  %%d0\n"				\
179      "    and.w  #3,%%d0\n"				\
180      "    sub.w  %%d0,%2\n"				\
181      "    bra    2f\n"					\
182      " 1: move.b (%0),(%1)+\n"				\
183      " 2: dbf    %%d0,1b\n"				\
184      "    move.w %2,%%d0\n"				\
185      "    lsr.w  #5,%%d0\n"				\
186      "    bra    4f\n"					\
187      " 3: move.l (%0),(%1)+\n"				\
188      "31: move.l (%0),(%1)+\n"				\
189      "32: move.l (%0),(%1)+\n"				\
190      "33: move.l (%0),(%1)+\n"				\
191      "34: move.l (%0),(%1)+\n"				\
192      "35: move.l (%0),(%1)+\n"				\
193      "36: move.l (%0),(%1)+\n"				\
194      "37: move.l (%0),(%1)+\n"				\
195      " 4: dbf    %%d0,3b\n"				\
196      "    move.w %2,%%d0\n"				\
197      "    lsr.w  #2,%%d0\n"				\
198      "    and.w  #7,%%d0\n"				\
199      "    bra    6f\n"					\
200      " 5: move.l (%0),(%1)+\n"				\
201      " 6: dbf    %%d0,5b\n"				\
202      "    and.w  #3,%2\n"				\
203      "    bra    8f\n"					\
204      " 7: move.b (%0),(%1)+\n"				\
205      " 8: dbf    %2,7b\n"				\
206      "    moveq.l #0, %2\n"				\
207      " 9: \n"						\
208      ".section .fixup,\"ax\"\n"				\
209      "    .even\n"					\
210      "90: moveq.l #1, %2\n"				\
211      "    jra 9b\n"					\
212      ".previous\n"					\
213      ".section __ex_table,\"a\"\n"			\
214      "   .align 4\n"					\
215      "   .long  1b,90b\n"				\
216      "   .long  3b,90b\n"				\
217      "   .long 31b,90b\n"				\
218      "   .long 32b,90b\n"				\
219      "   .long 33b,90b\n"				\
220      "   .long 34b,90b\n"				\
221      "   .long 35b,90b\n"				\
222      "   .long 36b,90b\n"				\
223      "   .long 37b,90b\n"				\
224      "   .long  5b,90b\n"				\
225      "   .long  7b,90b\n"				\
226      ".previous"					\
227      : "=a"(s), "=a"(d), "=d"(len)			\
228      : "0"(s), "1"(d), "2"(len)				\
229      : "d0")
230 
231 static int macscsi_pread(struct Scsi_Host *instance,
232                          unsigned char *dst, int len)
233 {
234 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
235 	unsigned char *d;
236 	unsigned char *s;
237 
238 	NCR5380_local_declare();
239 	NCR5380_setup(instance);
240 
241 	s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
242 	d = dst;
243 
244 	/* These conditions are derived from MacOS */
245 
246 	while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
247 	       !(NCR5380_read(STATUS_REG) & SR_REQ))
248 		;
249 
250 	if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
251 	    (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
252 		pr_err("Error in macscsi_pread\n");
253 		return -1;
254 	}
255 
256 	CP_IO_TO_MEM(s, d, len);
257 
258 	if (len != 0) {
259 		pr_notice("Bus error in macscsi_pread\n");
260 		return -1;
261 	}
262 
263 	return 0;
264 }
265 
266 
267 #define CP_MEM_TO_IO(s,d,len)				\
268 __asm__ __volatile__					\
269     ("    cmp.w  #4,%2\n"				\
270      "    bls    8f\n"					\
271      "    move.w %0,%%d0\n"				\
272      "    neg.b  %%d0\n"				\
273      "    and.w  #3,%%d0\n"				\
274      "    sub.w  %%d0,%2\n"				\
275      "    bra    2f\n"					\
276      " 1: move.b (%0)+,(%1)\n"				\
277      " 2: dbf    %%d0,1b\n"				\
278      "    move.w %2,%%d0\n"				\
279      "    lsr.w  #5,%%d0\n"				\
280      "    bra    4f\n"					\
281      " 3: move.l (%0)+,(%1)\n"				\
282      "31: move.l (%0)+,(%1)\n"				\
283      "32: move.l (%0)+,(%1)\n"				\
284      "33: move.l (%0)+,(%1)\n"				\
285      "34: move.l (%0)+,(%1)\n"				\
286      "35: move.l (%0)+,(%1)\n"				\
287      "36: move.l (%0)+,(%1)\n"				\
288      "37: move.l (%0)+,(%1)\n"				\
289      " 4: dbf    %%d0,3b\n"				\
290      "    move.w %2,%%d0\n"				\
291      "    lsr.w  #2,%%d0\n"				\
292      "    and.w  #7,%%d0\n"				\
293      "    bra    6f\n"					\
294      " 5: move.l (%0)+,(%1)\n"				\
295      " 6: dbf    %%d0,5b\n"				\
296      "    and.w  #3,%2\n"				\
297      "    bra    8f\n"					\
298      " 7: move.b (%0)+,(%1)\n"				\
299      " 8: dbf    %2,7b\n"				\
300      "    moveq.l #0, %2\n"				\
301      " 9: \n"						\
302      ".section .fixup,\"ax\"\n"				\
303      "    .even\n"					\
304      "90: moveq.l #1, %2\n"				\
305      "    jra 9b\n"					\
306      ".previous\n"					\
307      ".section __ex_table,\"a\"\n"			\
308      "   .align 4\n"					\
309      "   .long  1b,90b\n"				\
310      "   .long  3b,90b\n"				\
311      "   .long 31b,90b\n"				\
312      "   .long 32b,90b\n"				\
313      "   .long 33b,90b\n"				\
314      "   .long 34b,90b\n"				\
315      "   .long 35b,90b\n"				\
316      "   .long 36b,90b\n"				\
317      "   .long 37b,90b\n"				\
318      "   .long  5b,90b\n"				\
319      "   .long  7b,90b\n"				\
320      ".previous"					\
321      : "=a"(s), "=a"(d), "=d"(len)			\
322      : "0"(s), "1"(d), "2"(len)				\
323      : "d0")
324 
325 static int macscsi_pwrite(struct Scsi_Host *instance,
326                           unsigned char *src, int len)
327 {
328 	struct NCR5380_hostdata *hostdata = shost_priv(instance);
329 	unsigned char *s;
330 	unsigned char *d;
331 
332 	NCR5380_local_declare();
333 	NCR5380_setup(instance);
334 
335 	s = src;
336 	d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
337 
338 	/* These conditions are derived from MacOS */
339 
340 	while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
341 	       (!(NCR5380_read(STATUS_REG) & SR_REQ) ||
342 	        (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
343 		;
344 
345 	if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
346 		pr_err("Error in macscsi_pwrite\n");
347 		return -1;
348 	}
349 
350 	CP_MEM_TO_IO(s, d, len);
351 
352 	if (len != 0) {
353 		pr_notice("Bus error in macscsi_pwrite\n");
354 		return -1;
355 	}
356 
357 	return 0;
358 }
359 #endif
360 
361 #include "NCR5380.c"
362 
363 #define DRV_MODULE_NAME         "mac_scsi"
364 #define PFX                     DRV_MODULE_NAME ": "
365 
366 static struct scsi_host_template mac_scsi_template = {
367 	.module				= THIS_MODULE,
368 	.proc_name			= DRV_MODULE_NAME,
369 	.show_info			= macscsi_show_info,
370 	.write_info			= macscsi_write_info,
371 	.name				= "Macintosh NCR5380 SCSI",
372 	.info				= macscsi_info,
373 	.queuecommand			= macscsi_queue_command,
374 	.eh_abort_handler		= macscsi_abort,
375 	.eh_bus_reset_handler		= macscsi_bus_reset,
376 	.can_queue			= 16,
377 	.this_id			= 7,
378 	.sg_tablesize			= SG_ALL,
379 	.cmd_per_lun			= 2,
380 	.use_clustering			= DISABLE_CLUSTERING
381 };
382 
383 static int __init mac_scsi_probe(struct platform_device *pdev)
384 {
385 	struct Scsi_Host *instance;
386 	int error;
387 	int host_flags = 0;
388 	struct resource *irq, *pio_mem, *pdma_mem = NULL;
389 
390 	pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
391 	if (!pio_mem)
392 		return -ENODEV;
393 
394 #ifdef PSEUDO_DMA
395 	pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
396 #endif
397 
398 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
399 
400 	if (!hwreg_present((unsigned char *)pio_mem->start +
401 	                   (STATUS_REG << 4))) {
402 		pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
403 		return -ENODEV;
404 	}
405 
406 	if (setup_can_queue > 0)
407 		mac_scsi_template.can_queue = setup_can_queue;
408 	if (setup_cmd_per_lun > 0)
409 		mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
410 	if (setup_sg_tablesize >= 0)
411 		mac_scsi_template.sg_tablesize = setup_sg_tablesize;
412 	if (setup_hostid >= 0)
413 		mac_scsi_template.this_id = setup_hostid & 7;
414 	if (setup_use_pdma < 0)
415 		setup_use_pdma = 0;
416 
417 	instance = scsi_host_alloc(&mac_scsi_template,
418 	                           sizeof(struct NCR5380_hostdata));
419 	if (!instance)
420 		return -ENOMEM;
421 
422 	instance->base = pio_mem->start;
423 	if (irq)
424 		instance->irq = irq->start;
425 	else
426 		instance->irq = NO_IRQ;
427 
428 	if (pdma_mem && setup_use_pdma) {
429 		struct NCR5380_hostdata *hostdata = shost_priv(instance);
430 
431 		hostdata->pdma_base = (unsigned char *)pdma_mem->start;
432 	} else
433 		host_flags |= FLAG_NO_PSEUDO_DMA;
434 
435 #ifdef RESET_BOOT
436 	mac_scsi_reset_boot(instance);
437 #endif
438 
439 #ifdef SUPPORT_TAGS
440 	host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
441 #endif
442 
443 	NCR5380_init(instance, host_flags);
444 
445 	if (instance->irq != NO_IRQ) {
446 		error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
447 		                    "NCR5380", instance);
448 		if (error)
449 			goto fail_irq;
450 	}
451 
452 	error = scsi_add_host(instance, NULL);
453 	if (error)
454 		goto fail_host;
455 
456 	platform_set_drvdata(pdev, instance);
457 
458 	scsi_scan_host(instance);
459 	return 0;
460 
461 fail_host:
462 	if (instance->irq != NO_IRQ)
463 		free_irq(instance->irq, instance);
464 fail_irq:
465 	NCR5380_exit(instance);
466 	scsi_host_put(instance);
467 	return error;
468 }
469 
470 static int __exit mac_scsi_remove(struct platform_device *pdev)
471 {
472 	struct Scsi_Host *instance = platform_get_drvdata(pdev);
473 
474 	scsi_remove_host(instance);
475 	if (instance->irq != NO_IRQ)
476 		free_irq(instance->irq, instance);
477 	NCR5380_exit(instance);
478 	scsi_host_put(instance);
479 	return 0;
480 }
481 
482 static struct platform_driver mac_scsi_driver = {
483 	.remove = __exit_p(mac_scsi_remove),
484 	.driver = {
485 		.name	= DRV_MODULE_NAME,
486 		.owner	= THIS_MODULE,
487 	},
488 };
489 
490 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
491 
492 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
493 MODULE_LICENSE("GPL");
494