2 * Generic Macintosh NCR5380 driver
6 * derived in part from:
9 * Generic Generic NCR5380 driver
11 * Copyright 1995, Russell King
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>
23 #include <asm/hwtest.h>
25 #include <asm/macints.h>
26 #include <asm/setup.h>
28 #include <scsi/scsi_host.h>
30 /* Definitions for the core NCR5380 driver. */
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
38 #define NCR5380_read(reg) macscsi_read(_instance, reg)
39 #define NCR5380_write(reg, value) macscsi_write(_instance, reg, value)
41 #define NCR5380_pread macscsi_pread
42 #define NCR5380_pwrite macscsi_pwrite
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
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);
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... */
75 #define AFTER_RESET_DELAY (5*HZ/2)
77 #define AFTER_RESET_DELAY (HZ/2)
81 * NCR 5380 register access functions
84 static inline char macscsi_read(struct Scsi_Host *instance, int reg)
86 return in_8(instance->base + (reg << 4));
89 static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
91 out_8(instance->base + (reg << 4), value);
95 static int __init mac_scsi_setup(char *str)
99 (void)get_options(str, ARRAY_SIZE(ints), ints);
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");
106 setup_can_queue = ints[1];
108 setup_cmd_per_lun = ints[2];
110 setup_sg_tablesize = ints[3];
112 setup_hostid = ints[4];
114 setup_use_tagged_queuing = ints[5];
116 setup_use_pdma = ints[6];
120 __setup("mac5380=", mac_scsi_setup);
125 * Our 'bus reset on boot' function
128 static void mac_scsi_reset_boot(struct Scsi_Host *instance)
132 NCR5380_local_declare();
133 NCR5380_setup(instance);
136 * Do a SCSI reset to clean up the bus during initialization. No messing
137 * with the queues, interrupts, or locks necessary here.
140 printk(KERN_INFO "Macintosh SCSI: resetting the SCSI bus..." );
143 NCR5380_write( TARGET_COMMAND_REG,
144 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
147 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
148 /* The min. reset hold time is 25us, so 40us should be enough */
150 /* reset RST and interrupt */
151 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
152 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
154 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
157 printk(KERN_INFO " done\n" );
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.
173 #define CP_IO_TO_MEM(s,d,len) \
174 __asm__ __volatile__ \
177 " move.w %1,%%d0\n" \
182 " 1: move.b (%0),(%1)+\n" \
183 " 2: dbf %%d0,1b\n" \
184 " move.w %2,%%d0\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" \
200 " 5: move.l (%0),(%1)+\n" \
201 " 6: dbf %%d0,5b\n" \
204 " 7: move.b (%0),(%1)+\n" \
206 " moveq.l #0, %2\n" \
208 ".section .fixup,\"ax\"\n" \
210 "90: moveq.l #1, %2\n" \
213 ".section __ex_table,\"a\"\n" \
227 : "=a"(s), "=a"(d), "=d"(len) \
228 : "0"(s), "1"(d), "2"(len) \
231 static int macscsi_pread(struct Scsi_Host *instance,
232 unsigned char *dst, int len)
234 struct NCR5380_hostdata *hostdata = shost_priv(instance);
238 NCR5380_local_declare();
239 NCR5380_setup(instance);
241 s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
244 /* These conditions are derived from MacOS */
246 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
247 !(NCR5380_read(STATUS_REG) & SR_REQ))
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");
256 CP_IO_TO_MEM(s, d, len);
259 pr_notice("Bus error in macscsi_pread\n");
267 #define CP_MEM_TO_IO(s,d,len) \
268 __asm__ __volatile__ \
271 " move.w %0,%%d0\n" \
276 " 1: move.b (%0)+,(%1)\n" \
277 " 2: dbf %%d0,1b\n" \
278 " move.w %2,%%d0\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" \
294 " 5: move.l (%0)+,(%1)\n" \
295 " 6: dbf %%d0,5b\n" \
298 " 7: move.b (%0)+,(%1)\n" \
300 " moveq.l #0, %2\n" \
302 ".section .fixup,\"ax\"\n" \
304 "90: moveq.l #1, %2\n" \
307 ".section __ex_table,\"a\"\n" \
321 : "=a"(s), "=a"(d), "=d"(len) \
322 : "0"(s), "1"(d), "2"(len) \
325 static int macscsi_pwrite(struct Scsi_Host *instance,
326 unsigned char *src, int len)
328 struct NCR5380_hostdata *hostdata = shost_priv(instance);
332 NCR5380_local_declare();
333 NCR5380_setup(instance);
336 d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
338 /* These conditions are derived from MacOS */
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)))
345 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
346 pr_err("Error in macscsi_pwrite\n");
350 CP_MEM_TO_IO(s, d, len);
353 pr_notice("Bus error in macscsi_pwrite\n");
363 #define DRV_MODULE_NAME "mac_scsi"
364 #define PFX DRV_MODULE_NAME ": "
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,
378 .sg_tablesize = SG_ALL,
380 .use_clustering = DISABLE_CLUSTERING
383 static int __init mac_scsi_probe(struct platform_device *pdev)
385 struct Scsi_Host *instance;
388 struct resource *irq, *pio_mem, *pdma_mem = NULL;
390 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
395 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
398 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
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);
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)
417 instance = scsi_host_alloc(&mac_scsi_template,
418 sizeof(struct NCR5380_hostdata));
422 instance->base = pio_mem->start;
424 instance->irq = irq->start;
426 instance->irq = NO_IRQ;
428 if (pdma_mem && setup_use_pdma) {
429 struct NCR5380_hostdata *hostdata = shost_priv(instance);
431 hostdata->pdma_base = (unsigned char *)pdma_mem->start;
433 host_flags |= FLAG_NO_PSEUDO_DMA;
436 mac_scsi_reset_boot(instance);
440 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
443 NCR5380_init(instance, host_flags);
445 if (instance->irq != NO_IRQ) {
446 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
447 "NCR5380", instance);
452 error = scsi_add_host(instance, NULL);
456 platform_set_drvdata(pdev, instance);
458 scsi_scan_host(instance);
462 if (instance->irq != NO_IRQ)
463 free_irq(instance->irq, instance);
465 NCR5380_exit(instance);
466 scsi_host_put(instance);
470 static int __exit mac_scsi_remove(struct platform_device *pdev)
472 struct Scsi_Host *instance = platform_get_drvdata(pdev);
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);
482 static struct platform_driver mac_scsi_driver = {
483 .remove = __exit_p(mac_scsi_remove),
485 .name = DRV_MODULE_NAME,
489 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
491 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
492 MODULE_LICENSE("GPL");