2 * Generic Macintosh NCR5380 driver
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
6 * derived in part from:
9 * Generic Generic NCR5380 driver
11 * Copyright 1995, Russell King
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/blkdev.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
22 #include <asm/hwtest.h>
24 #include <asm/macints.h>
25 #include <asm/setup.h>
27 #include <scsi/scsi_host.h>
29 /* Definitions for the core NCR5380 driver. */
31 #define NCR5380_implementation_fields unsigned char *pdma_base
33 #define NCR5380_read(reg) macscsi_read(instance, reg)
34 #define NCR5380_write(reg, value) macscsi_write(instance, reg, value)
36 #define NCR5380_dma_xfer_len(instance, cmd, phase) \
37 macscsi_dma_xfer_len(instance, cmd)
38 #define NCR5380_dma_recv_setup macscsi_pread
39 #define NCR5380_dma_send_setup macscsi_pwrite
41 #define NCR5380_intr macscsi_intr
42 #define NCR5380_queue_command macscsi_queue_command
43 #define NCR5380_abort macscsi_abort
44 #define NCR5380_bus_reset macscsi_bus_reset
45 #define NCR5380_info macscsi_info
49 static int setup_can_queue = -1;
50 module_param(setup_can_queue, int, 0);
51 static int setup_cmd_per_lun = -1;
52 module_param(setup_cmd_per_lun, int, 0);
53 static int setup_sg_tablesize = -1;
54 module_param(setup_sg_tablesize, int, 0);
55 static int setup_use_pdma = -1;
56 module_param(setup_use_pdma, int, 0);
57 static int setup_use_tagged_queuing = -1;
58 module_param(setup_use_tagged_queuing, int, 0);
59 static int setup_hostid = -1;
60 module_param(setup_hostid, int, 0);
61 static int setup_toshiba_delay = -1;
62 module_param(setup_toshiba_delay, int, 0);
65 * NCR 5380 register access functions
68 static inline char macscsi_read(struct Scsi_Host *instance, int reg)
70 return in_8(instance->base + (reg << 4));
73 static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value)
75 out_8(instance->base + (reg << 4), value);
79 static int __init mac_scsi_setup(char *str)
83 (void)get_options(str, ARRAY_SIZE(ints), ints);
86 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
90 setup_can_queue = ints[1];
92 setup_cmd_per_lun = ints[2];
94 setup_sg_tablesize = ints[3];
96 setup_hostid = ints[4];
98 setup_use_tagged_queuing = ints[5];
100 setup_use_pdma = ints[6];
102 setup_toshiba_delay = ints[7];
106 __setup("mac5380=", mac_scsi_setup);
110 Pseudo-DMA: (Ove Edlund)
111 The code attempts to catch bus errors that occur if one for example
112 "trips over the cable".
113 XXX: Since bus errors in the PDMA routines never happen on my
114 computer, the bus error code is untested.
115 If the code works as intended, a bus error results in Pseudo-DMA
116 being disabled, meaning that the driver switches to slow handshake.
117 If bus errors are NOT extremely rare, this has to be changed.
120 #define CP_IO_TO_MEM(s,d,len) \
121 __asm__ __volatile__ \
124 " move.w %1,%%d0\n" \
129 " 1: move.b (%0),(%1)+\n" \
130 " 2: dbf %%d0,1b\n" \
131 " move.w %2,%%d0\n" \
134 " 3: move.l (%0),(%1)+\n" \
135 "31: move.l (%0),(%1)+\n" \
136 "32: move.l (%0),(%1)+\n" \
137 "33: move.l (%0),(%1)+\n" \
138 "34: move.l (%0),(%1)+\n" \
139 "35: move.l (%0),(%1)+\n" \
140 "36: move.l (%0),(%1)+\n" \
141 "37: move.l (%0),(%1)+\n" \
142 " 4: dbf %%d0,3b\n" \
143 " move.w %2,%%d0\n" \
147 " 5: move.l (%0),(%1)+\n" \
148 " 6: dbf %%d0,5b\n" \
151 " 7: move.b (%0),(%1)+\n" \
153 " moveq.l #0, %2\n" \
155 ".section .fixup,\"ax\"\n" \
157 "90: moveq.l #1, %2\n" \
160 ".section __ex_table,\"a\"\n" \
174 : "=a"(s), "=a"(d), "=d"(len) \
175 : "0"(s), "1"(d), "2"(len) \
178 static int macscsi_pread(struct Scsi_Host *instance,
179 unsigned char *dst, int len)
181 struct NCR5380_hostdata *hostdata = shost_priv(instance);
185 s = hostdata->pdma_base + (INPUT_DATA_REG << 4);
188 /* These conditions are derived from MacOS */
190 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
191 !(NCR5380_read(STATUS_REG) & SR_REQ))
194 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
195 (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) {
196 pr_err("Error in macscsi_pread\n");
200 CP_IO_TO_MEM(s, d, len);
203 pr_notice("Bus error in macscsi_pread\n");
211 #define CP_MEM_TO_IO(s,d,len) \
212 __asm__ __volatile__ \
215 " move.w %0,%%d0\n" \
220 " 1: move.b (%0)+,(%1)\n" \
221 " 2: dbf %%d0,1b\n" \
222 " move.w %2,%%d0\n" \
225 " 3: move.l (%0)+,(%1)\n" \
226 "31: move.l (%0)+,(%1)\n" \
227 "32: move.l (%0)+,(%1)\n" \
228 "33: move.l (%0)+,(%1)\n" \
229 "34: move.l (%0)+,(%1)\n" \
230 "35: move.l (%0)+,(%1)\n" \
231 "36: move.l (%0)+,(%1)\n" \
232 "37: move.l (%0)+,(%1)\n" \
233 " 4: dbf %%d0,3b\n" \
234 " move.w %2,%%d0\n" \
238 " 5: move.l (%0)+,(%1)\n" \
239 " 6: dbf %%d0,5b\n" \
242 " 7: move.b (%0)+,(%1)\n" \
244 " moveq.l #0, %2\n" \
246 ".section .fixup,\"ax\"\n" \
248 "90: moveq.l #1, %2\n" \
251 ".section __ex_table,\"a\"\n" \
265 : "=a"(s), "=a"(d), "=d"(len) \
266 : "0"(s), "1"(d), "2"(len) \
269 static int macscsi_pwrite(struct Scsi_Host *instance,
270 unsigned char *src, int len)
272 struct NCR5380_hostdata *hostdata = shost_priv(instance);
277 d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4);
279 /* These conditions are derived from MacOS */
281 while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) &&
282 (!(NCR5380_read(STATUS_REG) & SR_REQ) ||
283 (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)))
286 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)) {
287 pr_err("Error in macscsi_pwrite\n");
291 CP_MEM_TO_IO(s, d, len);
294 pr_notice("Bus error in macscsi_pwrite\n");
301 static int macscsi_dma_xfer_len(struct Scsi_Host *instance,
302 struct scsi_cmnd *cmd)
304 struct NCR5380_hostdata *hostdata = shost_priv(instance);
306 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
309 return cmd->transfersize;
314 #define DRV_MODULE_NAME "mac_scsi"
315 #define PFX DRV_MODULE_NAME ": "
317 static struct scsi_host_template mac_scsi_template = {
318 .module = THIS_MODULE,
319 .proc_name = DRV_MODULE_NAME,
320 .name = "Macintosh NCR5380 SCSI",
321 .info = macscsi_info,
322 .queuecommand = macscsi_queue_command,
323 .eh_abort_handler = macscsi_abort,
324 .eh_bus_reset_handler = macscsi_bus_reset,
327 .sg_tablesize = SG_ALL,
329 .use_clustering = DISABLE_CLUSTERING,
330 .cmd_size = NCR5380_CMD_SIZE,
334 static int __init mac_scsi_probe(struct platform_device *pdev)
336 struct Scsi_Host *instance;
339 struct resource *irq, *pio_mem, *pdma_mem = NULL;
341 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
347 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
349 if (!hwreg_present((unsigned char *)pio_mem->start +
350 (STATUS_REG << 4))) {
351 pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
355 if (setup_can_queue > 0)
356 mac_scsi_template.can_queue = setup_can_queue;
357 if (setup_cmd_per_lun > 0)
358 mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
359 if (setup_sg_tablesize >= 0)
360 mac_scsi_template.sg_tablesize = setup_sg_tablesize;
361 if (setup_hostid >= 0)
362 mac_scsi_template.this_id = setup_hostid & 7;
363 if (setup_use_pdma < 0)
366 instance = scsi_host_alloc(&mac_scsi_template,
367 sizeof(struct NCR5380_hostdata));
371 instance->base = pio_mem->start;
373 instance->irq = irq->start;
375 instance->irq = NO_IRQ;
377 if (pdma_mem && setup_use_pdma) {
378 struct NCR5380_hostdata *hostdata = shost_priv(instance);
380 hostdata->pdma_base = (unsigned char *)pdma_mem->start;
382 host_flags |= FLAG_NO_PSEUDO_DMA;
385 host_flags |= setup_use_tagged_queuing > 0 ? FLAG_TAGGED_QUEUING : 0;
387 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
389 error = NCR5380_init(instance, host_flags);
393 if (instance->irq != NO_IRQ) {
394 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
395 "NCR5380", instance);
400 NCR5380_maybe_reset_bus(instance);
402 error = scsi_add_host(instance, NULL);
406 platform_set_drvdata(pdev, instance);
408 scsi_scan_host(instance);
412 if (instance->irq != NO_IRQ)
413 free_irq(instance->irq, instance);
415 NCR5380_exit(instance);
417 scsi_host_put(instance);
421 static int __exit mac_scsi_remove(struct platform_device *pdev)
423 struct Scsi_Host *instance = platform_get_drvdata(pdev);
425 scsi_remove_host(instance);
426 if (instance->irq != NO_IRQ)
427 free_irq(instance->irq, instance);
428 NCR5380_exit(instance);
429 scsi_host_put(instance);
433 static struct platform_driver mac_scsi_driver = {
434 .remove = __exit_p(mac_scsi_remove),
436 .name = DRV_MODULE_NAME,
440 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
442 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
443 MODULE_LICENSE("GPL");