1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic Macintosh NCR5380 driver
7 * Copyright 2019 Finn Thain
9 * derived in part from:
12 * Generic Generic NCR5380 driver
14 * Copyright 1995, Russell King
17 #include <linux/delay.h>
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/ioport.h>
21 #include <linux/init.h>
22 #include <linux/blkdev.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
26 #include <asm/hwtest.h>
28 #include <asm/macintosh.h>
29 #include <asm/macints.h>
30 #include <asm/setup.h>
32 #include <scsi/scsi_host.h>
34 /* Definitions for the core NCR5380 driver. */
36 #define NCR5380_implementation_fields int pdma_residual
38 #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4))
39 #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value)
41 #define NCR5380_dma_xfer_len macscsi_dma_xfer_len
42 #define NCR5380_dma_recv_setup macscsi_pread
43 #define NCR5380_dma_send_setup macscsi_pwrite
44 #define NCR5380_dma_residual macscsi_dma_residual
46 #define NCR5380_intr macscsi_intr
47 #define NCR5380_queue_command macscsi_queue_command
48 #define NCR5380_abort macscsi_abort
49 #define NCR5380_host_reset macscsi_host_reset
50 #define NCR5380_info macscsi_info
54 static int setup_can_queue = -1;
55 module_param(setup_can_queue, int, 0);
56 static int setup_cmd_per_lun = -1;
57 module_param(setup_cmd_per_lun, int, 0);
58 static int setup_sg_tablesize = -1;
59 module_param(setup_sg_tablesize, int, 0);
60 static int setup_use_pdma = 512;
61 module_param(setup_use_pdma, int, 0);
62 static int setup_hostid = -1;
63 module_param(setup_hostid, int, 0);
64 static int setup_toshiba_delay = -1;
65 module_param(setup_toshiba_delay, int, 0);
68 static int __init mac_scsi_setup(char *str)
72 (void)get_options(str, ARRAY_SIZE(ints), ints);
75 pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n");
79 setup_can_queue = ints[1];
81 setup_cmd_per_lun = ints[2];
83 setup_sg_tablesize = ints[3];
85 setup_hostid = ints[4];
86 /* ints[5] (use_tagged_queuing) is ignored */
88 setup_use_pdma = ints[6];
90 setup_toshiba_delay = ints[7];
94 __setup("mac5380=", mac_scsi_setup);
98 * According to "Inside Macintosh: Devices", Mac OS requires disk drivers to
99 * specify the number of bytes between the delays expected from a SCSI target.
100 * This allows the operating system to "prevent bus errors when a target fails
101 * to deliver the next byte within the processor bus error timeout period."
102 * Linux SCSI drivers lack knowledge of the timing behaviour of SCSI targets
103 * so bus errors are unavoidable.
105 * If a MOVE.B instruction faults during a receive operation, we assume the
106 * target sent nothing and try again. That assumption probably depends on
107 * target firmware but it seems to hold up okay. If a fault happens during a
108 * send operation, the target may or may not have seen /ACK and got the byte.
109 * It's uncertain so the whole SCSI command gets retried.
111 * The NOP is needed for synchronization because the fault address in the
112 * exception stack frame may or may not be the instruction that actually
113 * caused the bus error. Post-increment addressing can't be used.
116 #define MOVE_BYTE(operands) \
118 "1: moveb " operands " \n" \
124 ".section .fixup,\"ax\" \n" \
126 "90: movel #1, %2 \n" \
130 ".section __ex_table,\"a\" \n" \
135 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
138 * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because
139 * the residual byte count would be uncertain. In that situation the MOVE_WORD
140 * macro clears n in the fixup section to abort the transfer.
143 #define MOVE_WORD(operands) \
145 "1: movew " operands " \n" \
150 ".section .fixup,\"ax\" \n" \
152 "90: movel #0, %1 \n" \
157 ".section __ex_table,\"a\" \n" \
162 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
164 #define MOVE_16_WORDS(operands) \
166 "1: movew " operands " \n" \
167 "2: movew " operands " \n" \
168 "3: movew " operands " \n" \
169 "4: movew " operands " \n" \
170 "5: movew " operands " \n" \
171 "6: movew " operands " \n" \
172 "7: movew " operands " \n" \
173 "8: movew " operands " \n" \
174 "9: movew " operands " \n" \
175 "10: movew " operands " \n" \
176 "11: movew " operands " \n" \
177 "12: movew " operands " \n" \
178 "13: movew " operands " \n" \
179 "14: movew " operands " \n" \
180 "15: movew " operands " \n" \
181 "16: movew " operands " \n" \
186 ".section .fixup,\"ax\" \n" \
188 "90: movel #0, %1 \n" \
193 ".section __ex_table,\"a\" \n" \
213 : "+a" (addr), "+r" (n), "+r" (result) : "a" (io))
215 static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n)
217 unsigned char *addr = start;
221 MOVE_BYTE("%3@,%0@");
225 if (n >= 1 && ((unsigned long)addr & 1)) {
226 MOVE_BYTE("%3@,%0@");
231 MOVE_16_WORDS("%3@,%0@+");
233 MOVE_WORD("%3@,%0@+");
235 return start - addr; /* Negated to indicate uncertain length */
237 MOVE_BYTE("%3@,%0@");
242 static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n)
244 unsigned char *addr = start;
248 MOVE_BYTE("%0@,%3@");
252 if (n >= 1 && ((unsigned long)addr & 1)) {
253 MOVE_BYTE("%0@,%3@");
258 MOVE_16_WORDS("%0@+,%3@");
260 MOVE_WORD("%0@+,%3@");
262 return start - addr - 1; /* Negated to indicate uncertain length */
264 MOVE_BYTE("%0@,%3@");
268 /* The "SCSI DMA" chip on the IIfx implements this register. */
270 #define CTRL_INTERRUPTS_ENABLE BIT(1)
271 #define CTRL_HANDSHAKE_MODE BIT(3)
273 static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value)
275 out_be32(hostdata->io + (CTRL_REG << 4), value);
278 static inline int macscsi_wait_for_drq(struct NCR5380_hostdata *hostdata)
280 unsigned int n = 1; /* effectively multiplies NCR5380_REG_POLL_TIME */
284 basr = NCR5380_read(BUS_AND_STATUS_REG);
286 if (!(basr & BASR_PHASE_MATCH))
296 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
297 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
298 "%s: DRQ timeout\n", __func__);
302 NCR5380_poll_politely2(hostdata,
303 BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ,
304 BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, 0);
308 static inline int macscsi_pread(struct NCR5380_hostdata *hostdata,
309 unsigned char *dst, int len)
311 u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4);
312 unsigned char *d = dst;
314 hostdata->pdma_residual = len;
316 while (macscsi_wait_for_drq(hostdata) == 0) {
317 int bytes, chunk_bytes;
319 if (macintosh_config->ident == MAC_MODEL_IIFX)
320 write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
321 CTRL_INTERRUPTS_ENABLE);
323 chunk_bytes = min(hostdata->pdma_residual, 512);
324 bytes = mac_pdma_recv(s, d, chunk_bytes);
326 if (macintosh_config->ident == MAC_MODEL_IIFX)
327 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
331 hostdata->pdma_residual -= bytes;
334 if (hostdata->pdma_residual == 0)
340 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
341 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
342 "%s: bus error [%d/%d] (%d/%d)\n",
343 __func__, d - dst, len, bytes, chunk_bytes);
348 if (macscsi_wait_for_drq(hostdata) <= 0)
349 set_host_byte(hostdata->connected, DID_ERROR);
356 static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata,
357 unsigned char *src, int len)
359 unsigned char *s = src;
360 u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4);
362 hostdata->pdma_residual = len;
364 while (macscsi_wait_for_drq(hostdata) == 0) {
365 int bytes, chunk_bytes;
367 if (macintosh_config->ident == MAC_MODEL_IIFX)
368 write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE |
369 CTRL_INTERRUPTS_ENABLE);
371 chunk_bytes = min(hostdata->pdma_residual, 512);
372 bytes = mac_pdma_send(s, d, chunk_bytes);
374 if (macintosh_config->ident == MAC_MODEL_IIFX)
375 write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE);
379 hostdata->pdma_residual -= bytes;
382 if (hostdata->pdma_residual == 0)
388 NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host);
389 dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host,
390 "%s: bus error [%d/%d] (%d/%d)\n",
391 __func__, s - src, len, bytes, chunk_bytes);
396 if (macscsi_wait_for_drq(hostdata) <= 0)
397 set_host_byte(hostdata->connected, DID_ERROR);
404 static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
405 struct scsi_cmnd *cmd)
407 int resid = NCR5380_to_ncmd(cmd)->this_residual;
409 if (hostdata->flags & FLAG_NO_PSEUDO_DMA || resid < setup_use_pdma)
415 static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata)
417 return hostdata->pdma_residual;
422 #define DRV_MODULE_NAME "mac_scsi"
423 #define PFX DRV_MODULE_NAME ": "
425 static struct scsi_host_template mac_scsi_template = {
426 .module = THIS_MODULE,
427 .proc_name = DRV_MODULE_NAME,
428 .name = "Macintosh NCR5380 SCSI",
429 .info = macscsi_info,
430 .queuecommand = macscsi_queue_command,
431 .eh_abort_handler = macscsi_abort,
432 .eh_host_reset_handler = macscsi_host_reset,
435 .sg_tablesize = SG_ALL,
437 .dma_boundary = PAGE_SIZE - 1,
438 .cmd_size = sizeof(struct NCR5380_cmd),
442 static int __init mac_scsi_probe(struct platform_device *pdev)
444 struct Scsi_Host *instance;
445 struct NCR5380_hostdata *hostdata;
448 struct resource *irq, *pio_mem, *pdma_mem = NULL;
450 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
454 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
456 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
458 if (!hwreg_present((unsigned char *)pio_mem->start +
459 (STATUS_REG << 4))) {
460 pr_info(PFX "no device detected at %pap\n", &pio_mem->start);
464 if (setup_can_queue > 0)
465 mac_scsi_template.can_queue = setup_can_queue;
466 if (setup_cmd_per_lun > 0)
467 mac_scsi_template.cmd_per_lun = setup_cmd_per_lun;
468 if (setup_sg_tablesize > 0)
469 mac_scsi_template.sg_tablesize = setup_sg_tablesize;
470 if (setup_hostid >= 0)
471 mac_scsi_template.this_id = setup_hostid & 7;
473 if (macintosh_config->ident == MAC_MODEL_IIFX)
474 mac_scsi_template.sg_tablesize = 1;
476 instance = scsi_host_alloc(&mac_scsi_template,
477 sizeof(struct NCR5380_hostdata));
482 instance->irq = irq->start;
484 instance->irq = NO_IRQ;
486 hostdata = shost_priv(instance);
487 hostdata->base = pio_mem->start;
488 hostdata->io = (u8 __iomem *)pio_mem->start;
490 if (pdma_mem && setup_use_pdma)
491 hostdata->pdma_io = (u8 __iomem *)pdma_mem->start;
493 host_flags |= FLAG_NO_PSEUDO_DMA;
495 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
497 if (instance->sg_tablesize > 1)
498 host_flags |= FLAG_DMA_FIXUP;
500 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP);
504 if (instance->irq != NO_IRQ) {
505 error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED,
506 "NCR5380", instance);
511 NCR5380_maybe_reset_bus(instance);
513 error = scsi_add_host(instance, NULL);
517 platform_set_drvdata(pdev, instance);
519 scsi_scan_host(instance);
523 if (instance->irq != NO_IRQ)
524 free_irq(instance->irq, instance);
526 NCR5380_exit(instance);
528 scsi_host_put(instance);
532 static void __exit mac_scsi_remove(struct platform_device *pdev)
534 struct Scsi_Host *instance = platform_get_drvdata(pdev);
536 scsi_remove_host(instance);
537 if (instance->irq != NO_IRQ)
538 free_irq(instance->irq, instance);
539 NCR5380_exit(instance);
540 scsi_host_put(instance);
544 * mac_scsi_remove() lives in .exit.text. For drivers registered via
545 * module_platform_driver_probe() this is ok because they cannot get unbound at
546 * runtime. So mark the driver struct with __refdata to prevent modpost
547 * triggering a section mismatch warning.
549 static struct platform_driver mac_scsi_driver __refdata = {
550 .remove = __exit_p(mac_scsi_remove),
552 .name = DRV_MODULE_NAME,
556 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe);
558 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
559 MODULE_DESCRIPTION("Macintosh NCR5380 SCSI driver");
560 MODULE_LICENSE("GPL");