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/macintosh.h>
28 #include <asm/macints.h>
29 #include <asm/setup.h>
31 #include <scsi/scsi_host.h>
33 /* Definitions for the core NCR5380 driver. */
35 #define NCR5380_implementation_fields int pdma_residual
37 #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
38 #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
40 #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
41 #define NCR5380_dma_recv_setup macscsi_pread
42 #define NCR5380_dma_send_setup macscsi_pwrite
43 #define NCR5380_dma_residual macscsi_dma_residual
45 #define NCR5380_intr macscsi_intr
46 #define NCR5380_queue_command macscsi_queue_command
47 #define NCR5380_abort macscsi_abort
48 #define NCR5380_host_reset macscsi_host_reset
49 #define NCR5380_info macscsi_info
53 static int setup_can_queue = -1;
54 module_param(setup_can_queue, int, 0);
55 static int setup_cmd_per_lun = -1;
56 module_param(setup_cmd_per_lun, int, 0);
57 static int setup_sg_tablesize = -1;
58 module_param(setup_sg_tablesize, int, 0);
59 static int setup_use_pdma = 512;
60 module_param(setup_use_pdma, int, 0);
61 static int setup_hostid = -1;
62 module_param(setup_hostid, int, 0);
63 static int setup_toshiba_delay = -1;
64 module_param(setup_toshiba_delay, int, 0);
67 static int __init mac_scsi_setup(char *str)
71 (void)get_options(str, ARRAY_SIZE(ints), ints);
74 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
78 setup_can_queue = ints[1];
80 setup_cmd_per_lun = ints[2];
82 setup_sg_tablesize = ints[3];
84 setup_hostid = ints[4];
85 /* ints[5] (use_tagged_queuing) is ignored */
87 setup_use_pdma = ints[6];
89 setup_toshiba_delay = ints[7];
93 __setup("mac5380=", mac_scsi_setup);
97 * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
98 * specify the number of bytes between the delays expected from a SCSI target.
99 * This allows the operating system to "prevent bus errors when a target fails
100 * to deliver the next byte within the processor bus error timeout period."
101 * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
102 * so bus errors are unavoidable.
104 * If a MOVE.B instruction faults, we assume that zero bytes were transferred
105 * and simply retry. That assumption probably depends on target behaviour but
106 * seems to hold up okay. The NOP provides synchronization: without it the
107 * fault can sometimes occur after the program counter has moved past the
108 * offending instruction. Post-increment addressing can't be used.
111 #define MOVE_BYTE(operands) \
113 "1: moveb " operands " \n" \
119 ".section .fixup,\"ax\" \n" \
121 "90: movel #1, %2 \n" \
125 ".section __ex_table,\"a\" \n" \
130 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
133 * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
134 * the residual byte count would be uncertain. In that situation the MOVE_WORD
135 * macro clears n in the fixup section to abort the transfer.
138 #define MOVE_WORD(operands) \
140 "1: movew " operands " \n" \
145 ".section .fixup,\"ax\" \n" \
147 "90: movel #0, %1 \n" \
152 ".section __ex_table,\"a\" \n" \
157 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
159 #define MOVE_16_WORDS(operands) \
161 "1: movew " operands " \n" \
162 "2: movew " operands " \n" \
163 "3: movew " operands " \n" \
164 "4: movew " operands " \n" \
165 "5: movew " operands " \n" \
166 "6: movew " operands " \n" \
167 "7: movew " operands " \n" \
168 "8: movew " operands " \n" \
169 "9: movew " operands " \n" \
170 "10: movew " operands " \n" \
171 "11: movew " operands " \n" \
172 "12: movew " operands " \n" \
173 "13: movew " operands " \n" \
174 "14: movew " operands " \n" \
175 "15: movew " operands " \n" \
176 "16: movew " operands " \n" \
181 ".section .fixup,\"ax\" \n" \
183 "90: movel #0, %1 \n" \
188 ".section __ex_table,\"a\" \n" \
208 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
210 #define MAC_PDMA_DELAY 32
212 static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
214 unsigned char *addr = start;
218 MOVE_BYTE("%3@,%0@");
222 if (n >= 1 && ((unsigned long)addr & 1)) {
223 MOVE_BYTE("%3@,%0@");
228 MOVE_16_WORDS("%3@,%0@+");
230 MOVE_WORD("%3@,%0@+");
232 return start - addr; /* Negated to indicate uncertain length */
234 MOVE_BYTE("%3@,%0@");
239 static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
241 unsigned char *addr = start;
245 MOVE_BYTE("%0@,%3@");
249 if (n >= 1 && ((unsigned long)addr & 1)) {
250 MOVE_BYTE("%0@,%3@");
255 MOVE_16_WORDS("%0@+,%3@");
257 MOVE_WORD("%0@+,%3@");
259 return start - addr; /* Negated to indicate uncertain length */
261 MOVE_BYTE("%0@,%3@");
266 /* The "SCSI DMA" chip on the IIfx implements this register. */
268 #define CTRL_INTERRUPTS_ENABLE BIT(1)
269 #define CTRL_HANDSHAKE_MODE BIT(3)
271 static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
273 out_be32(hostdata->io + (CTRL_REG << 4), value);
276 static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
277 unsigned char *dst, int len)
279 u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
280 unsigned char *d = dst;
283 hostdata->pdma_residual = len;
285 while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
286 BASR_DRQ | BASR_PHASE_MATCH,
287 BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
290 if (macintosh_config->ident == MAC_MODEL_IIFX)
291 write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
292 CTRL_INTERRUPTS_ENABLE);
294 bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512));
298 hostdata->pdma_residual -= bytes;
301 if (hostdata->pdma_residual == 0)
304 if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
305 BUS_AND_STATUS_REG, BASR_ACK,
306 BASR_ACK, HZ / 64) < 0)
307 scmd_printk(KERN_DEBUG, hostdata->connected,
308 "%s: !REQ and !ACK\n", __func__);
309 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
313 udelay(MAC_PDMA_DELAY);
318 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
319 "%s: bus error (%d/%d)\n", __func__, d - dst, len);
320 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
325 scmd_printk(KERN_ERR, hostdata->connected,
326 "%s: phase mismatch or !DRQ\n", __func__);
327 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
330 if (macintosh_config->ident == MAC_MODEL_IIFX)
331 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
335 static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
336 unsigned char *src, int len)
338 unsigned char *s = src;
339 u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
342 hostdata->pdma_residual = len;
344 while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG,
345 BASR_DRQ | BASR_PHASE_MATCH,
346 BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) {
349 if (macintosh_config->ident == MAC_MODEL_IIFX)
350 write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
351 CTRL_INTERRUPTS_ENABLE);
353 bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512));
357 hostdata->pdma_residual -= bytes;
360 if (hostdata->pdma_residual == 0) {
361 if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG,
365 scmd_printk(KERN_ERR, hostdata->connected,
366 "%s: Last Byte Sent timeout\n", __func__);
372 if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ,
373 BUS_AND_STATUS_REG, BASR_ACK,
374 BASR_ACK, HZ / 64) < 0)
375 scmd_printk(KERN_DEBUG, hostdata->connected,
376 "%s: !REQ and !ACK\n", __func__);
377 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH))
381 udelay(MAC_PDMA_DELAY);
386 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
387 "%s: bus error (%d/%d)\n", __func__, s - src, len);
388 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
393 scmd_printk(KERN_ERR, hostdata->connected,
394 "%s: phase mismatch or !DRQ\n", __func__);
395 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
398 if (macintosh_config->ident == MAC_MODEL_IIFX)
399 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
403 static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
404 struct scsi_cmnd *cmd)
406 if (hostdata->flags & FLAG_NO_PSEUDO_DMA ||
407 cmd->SCp.this_residual < setup_use_pdma)
410 return cmd->SCp.this_residual;
413 static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
415 return hostdata->pdma_residual;
420 #define DRV_MODULE_NAME "mac_scsi"
421 #define PFX DRV_MODULE_NAME ": "
423 static struct scsi_host_template mac_scsi_template = {
424 .module = THIS_MODULE,
425 .proc_name = DRV_MODULE_NAME,
426 .name = "Macintosh NCR5380 SCSI",
427 .info = macscsi_info,
428 .queuecommand = macscsi_queue_command,
429 .eh_abort_handler = macscsi_abort,
430 .eh_host_reset_handler = macscsi_host_reset,
435 .dma_boundary = PAGE_SIZE - 1,
436 .cmd_size = NCR5380_CMD_SIZE,
440 static int __init mac_scsi_probe(struct platform_device *pdev)
442 struct Scsi_Host *instance;
443 struct NCR5380_hostdata *hostdata;
446 struct resource *irq, *pio_mem, *pdma_mem = NULL;
448 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
452 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
454 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
456 if (!hwreg_present((unsigned char *)pio_mem->start +
457 (STATUS_REG << 4))) {
458 pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
462 if (setup_can_queue > 0)
463 mac_scsi_template.can_queue = setup_can_queue;
464 if (setup_cmd_per_lun > 0)
465 mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
466 if (setup_sg_tablesize >= 0)
467 mac_scsi_template.sg_tablesize = setup_sg_tablesize;
468 if (setup_hostid >= 0)
469 mac_scsi_template.this_id = setup_hostid & 7;
471 instance = scsi_host_alloc(&mac_scsi_template,
472 sizeof(struct NCR5380_hostdata));
477 instance->irq = irq->start;
479 instance->irq = NO_IRQ;
481 hostdata = shost_priv(instance);
482 hostdata->base = pio_mem->start;
483 hostdata->io = (u8 __iomem *)pio_mem->start;
485 if (pdma_mem && setup_use_pdma)
486 hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
488 host_flags |= FLAG_NO_PSEUDO_DMA;
490 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
492 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
496 if (instance->irq != NO_IRQ) {
497 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
498 "NCR5380", instance);
503 NCR5380_maybe_reset_bus(instance);
505 error = scsi_add_host(instance, NULL);
509 platform_set_drvdata(pdev, instance);
511 scsi_scan_host(instance);
515 if (instance->irq != NO_IRQ)
516 free_irq(instance->irq, instance);
518 NCR5380_exit(instance);
520 scsi_host_put(instance);
524 static int __exit mac_scsi_remove(struct platform_device *pdev)
526 struct Scsi_Host *instance = platform_get_drvdata(pdev);
528 scsi_remove_host(instance);
529 if (instance->irq != NO_IRQ)
530 free_irq(instance->irq, instance);
531 NCR5380_exit(instance);
532 scsi_host_put(instance);
536 static struct platform_driver mac_scsi_driver = {
537 .remove = __exit_p(mac_scsi_remove),
539 .name = DRV_MODULE_NAME,
543 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
545 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
546 MODULE_LICENSE("GPL");