2 * Generic Macintosh NCR5380 driver
4 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
6 * Copyright 2019 Finn Thain
8 * derived in part from:
11 * Generic Generic NCR5380 driver
13 * Copyright 1995, Russell King
16 #include <linux/delay.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/blkdev.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
25 #include <asm/hwtest.h>
27 #include <asm/macints.h>
28 #include <asm/setup.h>
30 #include <scsi/scsi_host.h>
32 /* Definitions for the core NCR5380 driver. */
34 #define NCR5380_implementation_fields int pdma_residual
36 #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
37 #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
39 #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
40 #define NCR5380_dma_recv_setup macscsi_pread
41 #define NCR5380_dma_send_setup macscsi_pwrite
42 #define NCR5380_dma_residual macscsi_dma_residual
44 #define NCR5380_intr macscsi_intr
45 #define NCR5380_queue_command macscsi_queue_command
46 #define NCR5380_abort macscsi_abort
47 #define NCR5380_host_reset macscsi_host_reset
48 #define NCR5380_info macscsi_info
52 static int setup_can_queue
= -1;
53 module_param(setup_can_queue
, int, 0);
54 static int setup_cmd_per_lun
= -1;
55 module_param(setup_cmd_per_lun
, int, 0);
56 static int setup_sg_tablesize
= -1;
57 module_param(setup_sg_tablesize
, int, 0);
58 static int setup_use_pdma
= 512;
59 module_param(setup_use_pdma
, int, 0);
60 static int setup_hostid
= -1;
61 module_param(setup_hostid
, int, 0);
62 static int setup_toshiba_delay
= -1;
63 module_param(setup_toshiba_delay
, int, 0);
66 static int __init
mac_scsi_setup(char *str
)
70 (void)get_options(str
, ARRAY_SIZE(ints
), ints
);
73 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
77 setup_can_queue
= ints
[1];
79 setup_cmd_per_lun
= ints
[2];
81 setup_sg_tablesize
= ints
[3];
83 setup_hostid
= ints
[4];
84 /* ints[5] (use_tagged_queuing) is ignored */
86 setup_use_pdma
= ints
[6];
88 setup_toshiba_delay
= ints
[7];
92 __setup("mac5380=", mac_scsi_setup
);
96 * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
97 * specify the number of bytes between the delays expected from a SCSI target.
98 * This allows the operating system to "prevent bus errors when a target fails
99 * to deliver the next byte within the processor bus error timeout period."
100 * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
101 * so bus errors are unavoidable.
103 * If a MOVE.B instruction faults, we assume that zero bytes were transferred
104 * and simply retry. That assumption probably depends on target behaviour but
105 * seems to hold up okay. The NOP provides synchronization: without it the
106 * fault can sometimes occur after the program counter has moved past the
107 * offending instruction. Post-increment addressing can't be used.
110 #define MOVE_BYTE(operands) \
112 "1: moveb " operands " \n" \
118 ".section .fixup,\"ax\" \n" \
120 "90: movel #1, %2 \n" \
124 ".section __ex_table,\"a\" \n" \
129 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
132 * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
133 * the residual byte count would be uncertain. In that situation the MOVE_WORD
134 * macro clears n in the fixup section to abort the transfer.
137 #define MOVE_WORD(operands) \
139 "1: movew " operands " \n" \
144 ".section .fixup,\"ax\" \n" \
146 "90: movel #0, %1 \n" \
151 ".section __ex_table,\"a\" \n" \
156 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
158 #define MOVE_16_WORDS(operands) \
160 "1: movew " operands " \n" \
161 "2: movew " operands " \n" \
162 "3: movew " operands " \n" \
163 "4: movew " operands " \n" \
164 "5: movew " operands " \n" \
165 "6: movew " operands " \n" \
166 "7: movew " operands " \n" \
167 "8: movew " operands " \n" \
168 "9: movew " operands " \n" \
169 "10: movew " operands " \n" \
170 "11: movew " operands " \n" \
171 "12: movew " operands " \n" \
172 "13: movew " operands " \n" \
173 "14: movew " operands " \n" \
174 "15: movew " operands " \n" \
175 "16: movew " operands " \n" \
180 ".section .fixup,\"ax\" \n" \
182 "90: movel #0, %1 \n" \
187 ".section __ex_table,\"a\" \n" \
207 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
209 #define MAC_PDMA_DELAY 32
211 static inline int mac_pdma_recv(void __iomem
*io
, unsigned char *start
, int n
)
213 unsigned char *addr
= start
;
217 MOVE_BYTE("%3@,%0@");
221 if (n
>= 1 && ((unsigned long)addr
& 1)) {
222 MOVE_BYTE("%3@,%0@");
227 MOVE_16_WORDS("%3@,%0@+");
229 MOVE_WORD("%3@,%0@+");
231 return start
- addr
; /* Negated to indicate uncertain length */
233 MOVE_BYTE("%3@,%0@");
238 static inline int mac_pdma_send(unsigned char *start
, void __iomem
*io
, int n
)
240 unsigned char *addr
= start
;
244 MOVE_BYTE("%0@,%3@");
248 if (n
>= 1 && ((unsigned long)addr
& 1)) {
249 MOVE_BYTE("%0@,%3@");
254 MOVE_16_WORDS("%0@+,%3@");
256 MOVE_WORD("%0@+,%3@");
258 return start
- addr
; /* Negated to indicate uncertain length */
260 MOVE_BYTE("%0@,%3@");
265 static inline int macscsi_pread(struct NCR5380_hostdata
*hostdata
,
266 unsigned char *dst
, int len
)
268 u8 __iomem
*s
= hostdata
->pdma_io
+ (INPUT_DATA_REG
<< 4);
269 unsigned char *d
= dst
;
271 hostdata
->pdma_residual
= len
;
273 while (!NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
274 BASR_DRQ
| BASR_PHASE_MATCH
,
275 BASR_DRQ
| BASR_PHASE_MATCH
, HZ
/ 64)) {
278 bytes
= mac_pdma_recv(s
, d
, min(hostdata
->pdma_residual
, 512));
282 hostdata
->pdma_residual
-= bytes
;
285 if (hostdata
->pdma_residual
== 0)
288 if (NCR5380_poll_politely2(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
,
289 BUS_AND_STATUS_REG
, BASR_ACK
,
290 BASR_ACK
, HZ
/ 64) < 0)
291 scmd_printk(KERN_DEBUG
, hostdata
->connected
,
292 "%s: !REQ and !ACK\n", __func__
);
293 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
))
297 udelay(MAC_PDMA_DELAY
);
302 dsprintk(NDEBUG_PSEUDO_DMA
, hostdata
->host
,
303 "%s: bus error (%d/%d)\n", __func__
, d
- dst
, len
);
304 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
308 scmd_printk(KERN_ERR
, hostdata
->connected
,
309 "%s: phase mismatch or !DRQ\n", __func__
);
310 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
314 static inline int macscsi_pwrite(struct NCR5380_hostdata
*hostdata
,
315 unsigned char *src
, int len
)
317 unsigned char *s
= src
;
318 u8 __iomem
*d
= hostdata
->pdma_io
+ (OUTPUT_DATA_REG
<< 4);
320 hostdata
->pdma_residual
= len
;
322 while (!NCR5380_poll_politely(hostdata
, BUS_AND_STATUS_REG
,
323 BASR_DRQ
| BASR_PHASE_MATCH
,
324 BASR_DRQ
| BASR_PHASE_MATCH
, HZ
/ 64)) {
327 bytes
= mac_pdma_send(s
, d
, min(hostdata
->pdma_residual
, 512));
331 hostdata
->pdma_residual
-= bytes
;
334 if (hostdata
->pdma_residual
== 0) {
335 if (NCR5380_poll_politely(hostdata
, TARGET_COMMAND_REG
,
337 TCR_LAST_BYTE_SENT
, HZ
/ 64) < 0)
338 scmd_printk(KERN_ERR
, hostdata
->connected
,
339 "%s: Last Byte Sent timeout\n", __func__
);
343 if (NCR5380_poll_politely2(hostdata
, STATUS_REG
, SR_REQ
, SR_REQ
,
344 BUS_AND_STATUS_REG
, BASR_ACK
,
345 BASR_ACK
, HZ
/ 64) < 0)
346 scmd_printk(KERN_DEBUG
, hostdata
->connected
,
347 "%s: !REQ and !ACK\n", __func__
);
348 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_PHASE_MATCH
))
352 udelay(MAC_PDMA_DELAY
);
357 dsprintk(NDEBUG_PSEUDO_DMA
, hostdata
->host
,
358 "%s: bus error (%d/%d)\n", __func__
, s
- src
, len
);
359 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
363 scmd_printk(KERN_ERR
, hostdata
->connected
,
364 "%s: phase mismatch or !DRQ\n", __func__
);
365 NCR5380_dprint(NDEBUG_PSEUDO_DMA
, hostdata
->host
);
369 static int macscsi_dma_xfer_len(struct NCR5380_hostdata
*hostdata
,
370 struct scsi_cmnd
*cmd
)
372 if (hostdata
->flags
& FLAG_NO_PSEUDO_DMA
||
373 cmd
->SCp
.this_residual
< setup_use_pdma
)
376 return cmd
->SCp
.this_residual
;
379 static int macscsi_dma_residual(struct NCR5380_hostdata
*hostdata
)
381 return hostdata
->pdma_residual
;
386 #define DRV_MODULE_NAME "mac_scsi"
387 #define PFX DRV_MODULE_NAME ": "
389 static struct scsi_host_template mac_scsi_template
= {
390 .module
= THIS_MODULE
,
391 .proc_name
= DRV_MODULE_NAME
,
392 .name
= "Macintosh NCR5380 SCSI",
393 .info
= macscsi_info
,
394 .queuecommand
= macscsi_queue_command
,
395 .eh_abort_handler
= macscsi_abort
,
396 .eh_host_reset_handler
= macscsi_host_reset
,
401 .use_clustering
= DISABLE_CLUSTERING
,
402 .cmd_size
= NCR5380_CMD_SIZE
,
406 static int __init
mac_scsi_probe(struct platform_device
*pdev
)
408 struct Scsi_Host
*instance
;
409 struct NCR5380_hostdata
*hostdata
;
412 struct resource
*irq
, *pio_mem
, *pdma_mem
= NULL
;
414 pio_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
418 pdma_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
420 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
422 if (!hwreg_present((unsigned char *)pio_mem
->start
+
423 (STATUS_REG
<< 4))) {
424 pr_info(PFX
"no device detected at %pap\n", &pio_mem
->start
);
428 if (setup_can_queue
> 0)
429 mac_scsi_template
.can_queue
= setup_can_queue
;
430 if (setup_cmd_per_lun
> 0)
431 mac_scsi_template
.cmd_per_lun
= setup_cmd_per_lun
;
432 if (setup_sg_tablesize
> 0)
433 mac_scsi_template
.sg_tablesize
= setup_sg_tablesize
;
434 if (setup_hostid
>= 0)
435 mac_scsi_template
.this_id
= setup_hostid
& 7;
437 instance
= scsi_host_alloc(&mac_scsi_template
,
438 sizeof(struct NCR5380_hostdata
));
443 instance
->irq
= irq
->start
;
445 instance
->irq
= NO_IRQ
;
447 hostdata
= shost_priv(instance
);
448 hostdata
->base
= pio_mem
->start
;
449 hostdata
->io
= (u8 __iomem
*)pio_mem
->start
;
451 if (pdma_mem
&& setup_use_pdma
)
452 hostdata
->pdma_io
= (u8 __iomem
*)pdma_mem
->start
;
454 host_flags
|= FLAG_NO_PSEUDO_DMA
;
456 host_flags
|= setup_toshiba_delay
> 0 ? FLAG_TOSHIBA_DELAY
: 0;
458 error
= NCR5380_init(instance
, host_flags
| FLAG_LATE_DMA_SETUP
);
462 if (instance
->irq
!= NO_IRQ
) {
463 error
= request_irq(instance
->irq
, macscsi_intr
, IRQF_SHARED
,
464 "NCR5380", instance
);
469 NCR5380_maybe_reset_bus(instance
);
471 error
= scsi_add_host(instance
, NULL
);
475 platform_set_drvdata(pdev
, instance
);
477 scsi_scan_host(instance
);
481 if (instance
->irq
!= NO_IRQ
)
482 free_irq(instance
->irq
, instance
);
484 NCR5380_exit(instance
);
486 scsi_host_put(instance
);
490 static int __exit
mac_scsi_remove(struct platform_device
*pdev
)
492 struct Scsi_Host
*instance
= platform_get_drvdata(pdev
);
494 scsi_remove_host(instance
);
495 if (instance
->irq
!= NO_IRQ
)
496 free_irq(instance
->irq
, instance
);
497 NCR5380_exit(instance
);
498 scsi_host_put(instance
);
502 static struct platform_driver mac_scsi_driver
= {
503 .remove
= __exit_p(mac_scsi_remove
),
505 .name
= DRV_MODULE_NAME
,
509 module_platform_driver_probe(mac_scsi_driver
, mac_scsi_probe
);
511 MODULE_ALIAS("platform:" DRV_MODULE_NAME
);
512 MODULE_LICENSE("GPL");