Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Generic Macintosh NCR5380 driver |
| 4 | * |
| 5 | * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov> |
| 6 | * |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 7 | * Copyright 2019 Finn Thain |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * derived in part from: |
| 10 | */ |
| 11 | /* |
| 12 | * Generic Generic NCR5380 driver |
| 13 | * |
| 14 | * Copyright 1995, Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 17 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/ioport.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/interrupt.h> |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 26 | #include <asm/hwtest.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/io.h> |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 28 | #include <asm/macintosh.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/macints.h> |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 30 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <scsi/scsi_host.h> |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 33 | |
| 34 | /* Definitions for the core NCR5380 driver. */ |
Boaz Harrosh | e744fde | 2007-10-16 10:25:01 +0200 | [diff] [blame] | 35 | |
Finn Thain | 820682b | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 36 | #define NCR5380_implementation_fields int pdma_residual |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 37 | |
Finn Thain | 61e1ce5 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 38 | #define NCR5380_read(reg) in_8(hostdata->io + ((reg) << 4)) |
| 39 | #define NCR5380_write(reg, value) out_8(hostdata->io + ((reg) << 4), value) |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 40 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 41 | #define NCR5380_dma_xfer_len macscsi_dma_xfer_len |
Finn Thain | 6c4b88c | 2016-03-23 21:10:17 +1100 | [diff] [blame] | 42 | #define NCR5380_dma_recv_setup macscsi_pread |
| 43 | #define NCR5380_dma_send_setup macscsi_pwrite |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 44 | #define NCR5380_dma_residual macscsi_dma_residual |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 45 | |
| 46 | #define NCR5380_intr macscsi_intr |
| 47 | #define NCR5380_queue_command macscsi_queue_command |
| 48 | #define NCR5380_abort macscsi_abort |
Hannes Reinecke | 12e5fc6 | 2017-08-25 13:57:10 +0200 | [diff] [blame] | 49 | #define NCR5380_host_reset macscsi_host_reset |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 50 | #define NCR5380_info macscsi_info |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include "NCR5380.h" |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static int setup_can_queue = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 55 | module_param(setup_can_queue, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static int setup_cmd_per_lun = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 57 | module_param(setup_cmd_per_lun, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static int setup_sg_tablesize = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 59 | module_param(setup_sg_tablesize, int, 0); |
Finn Thain | 7398cee | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 60 | static int setup_use_pdma = 512; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 61 | module_param(setup_use_pdma, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static int setup_hostid = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 63 | module_param(setup_hostid, int, 0); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 64 | static int setup_toshiba_delay = -1; |
| 65 | module_param(setup_toshiba_delay, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 67 | #ifndef MODULE |
| 68 | static int __init mac_scsi_setup(char *str) |
| 69 | { |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 70 | int ints[8]; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 71 | |
| 72 | (void)get_options(str, ARRAY_SIZE(ints), ints); |
| 73 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 74 | if (ints[0] < 1) { |
| 75 | pr_err("Usage: mac5380=<can_queue>[,<cmd_per_lun>[,<sg_tablesize>[,<hostid>[,<use_tags>[,<use_pdma>[,<toshiba_delay>]]]]]]\n"); |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 76 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 78 | if (ints[0] >= 1) |
| 79 | setup_can_queue = ints[1]; |
| 80 | if (ints[0] >= 2) |
| 81 | setup_cmd_per_lun = ints[2]; |
| 82 | if (ints[0] >= 3) |
| 83 | setup_sg_tablesize = ints[3]; |
| 84 | if (ints[0] >= 4) |
| 85 | setup_hostid = ints[4]; |
Finn Thain | c4ec6f9 | 2016-03-23 21:10:22 +1100 | [diff] [blame] | 86 | /* ints[5] (use_tagged_queuing) is ignored */ |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 87 | if (ints[0] >= 6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | setup_use_pdma = ints[6]; |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 89 | if (ints[0] >= 7) |
| 90 | setup_toshiba_delay = ints[7]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | __setup("mac5380=", mac_scsi_setup); |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 95 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 97 | /* |
| 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. |
| 104 | * |
| 105 | * If a MOVE.B instruction faults, we assume that zero bytes were transferred |
| 106 | * and simply retry. That assumption probably depends on target behaviour but |
| 107 | * seems to hold up okay. The NOP provides synchronization: without it the |
| 108 | * fault can sometimes occur after the program counter has moved past the |
| 109 | * offending instruction. Post-increment addressing can't be used. |
| 110 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 112 | #define MOVE_BYTE(operands) \ |
| 113 | asm volatile ( \ |
| 114 | "1: moveb " operands " \n" \ |
| 115 | "11: nop \n" \ |
| 116 | " addq #1,%0 \n" \ |
| 117 | " subq #1,%1 \n" \ |
| 118 | "40: \n" \ |
| 119 | " \n" \ |
| 120 | ".section .fixup,\"ax\" \n" \ |
| 121 | ".even \n" \ |
| 122 | "90: movel #1, %2 \n" \ |
| 123 | " jra 40b \n" \ |
| 124 | ".previous \n" \ |
| 125 | " \n" \ |
| 126 | ".section __ex_table,\"a\" \n" \ |
| 127 | ".align 4 \n" \ |
| 128 | ".long 1b,90b \n" \ |
| 129 | ".long 11b,90b \n" \ |
| 130 | ".previous \n" \ |
| 131 | : "+a" (addr), "+r" (n), "+r" (result) : "a" (io)) |
| 132 | |
| 133 | /* |
| 134 | * If a MOVE.W (or MOVE.L) instruction faults, it cannot be retried because |
| 135 | * the residual byte count would be uncertain. In that situation the MOVE_WORD |
| 136 | * macro clears n in the fixup section to abort the transfer. |
| 137 | */ |
| 138 | |
| 139 | #define MOVE_WORD(operands) \ |
| 140 | asm volatile ( \ |
| 141 | "1: movew " operands " \n" \ |
| 142 | "11: nop \n" \ |
| 143 | " subq #2,%1 \n" \ |
| 144 | "40: \n" \ |
| 145 | " \n" \ |
| 146 | ".section .fixup,\"ax\" \n" \ |
| 147 | ".even \n" \ |
| 148 | "90: movel #0, %1 \n" \ |
| 149 | " movel #2, %2 \n" \ |
| 150 | " jra 40b \n" \ |
| 151 | ".previous \n" \ |
| 152 | " \n" \ |
| 153 | ".section __ex_table,\"a\" \n" \ |
| 154 | ".align 4 \n" \ |
| 155 | ".long 1b,90b \n" \ |
| 156 | ".long 11b,90b \n" \ |
| 157 | ".previous \n" \ |
| 158 | : "+a" (addr), "+r" (n), "+r" (result) : "a" (io)) |
| 159 | |
| 160 | #define MOVE_16_WORDS(operands) \ |
| 161 | asm volatile ( \ |
| 162 | "1: movew " operands " \n" \ |
| 163 | "2: movew " operands " \n" \ |
| 164 | "3: movew " operands " \n" \ |
| 165 | "4: movew " operands " \n" \ |
| 166 | "5: movew " operands " \n" \ |
| 167 | "6: movew " operands " \n" \ |
| 168 | "7: movew " operands " \n" \ |
| 169 | "8: movew " operands " \n" \ |
| 170 | "9: movew " operands " \n" \ |
| 171 | "10: movew " operands " \n" \ |
| 172 | "11: movew " operands " \n" \ |
| 173 | "12: movew " operands " \n" \ |
| 174 | "13: movew " operands " \n" \ |
| 175 | "14: movew " operands " \n" \ |
| 176 | "15: movew " operands " \n" \ |
| 177 | "16: movew " operands " \n" \ |
| 178 | "17: nop \n" \ |
| 179 | " subl #32,%1 \n" \ |
| 180 | "40: \n" \ |
| 181 | " \n" \ |
| 182 | ".section .fixup,\"ax\" \n" \ |
| 183 | ".even \n" \ |
| 184 | "90: movel #0, %1 \n" \ |
| 185 | " movel #2, %2 \n" \ |
| 186 | " jra 40b \n" \ |
| 187 | ".previous \n" \ |
| 188 | " \n" \ |
| 189 | ".section __ex_table,\"a\" \n" \ |
| 190 | ".align 4 \n" \ |
| 191 | ".long 1b,90b \n" \ |
| 192 | ".long 2b,90b \n" \ |
| 193 | ".long 3b,90b \n" \ |
| 194 | ".long 4b,90b \n" \ |
| 195 | ".long 5b,90b \n" \ |
| 196 | ".long 6b,90b \n" \ |
| 197 | ".long 7b,90b \n" \ |
| 198 | ".long 8b,90b \n" \ |
| 199 | ".long 9b,90b \n" \ |
| 200 | ".long 10b,90b \n" \ |
| 201 | ".long 11b,90b \n" \ |
| 202 | ".long 12b,90b \n" \ |
| 203 | ".long 13b,90b \n" \ |
| 204 | ".long 14b,90b \n" \ |
| 205 | ".long 15b,90b \n" \ |
| 206 | ".long 16b,90b \n" \ |
| 207 | ".long 17b,90b \n" \ |
| 208 | ".previous \n" \ |
| 209 | : "+a" (addr), "+r" (n), "+r" (result) : "a" (io)) |
| 210 | |
| 211 | #define MAC_PDMA_DELAY 32 |
| 212 | |
| 213 | static inline int mac_pdma_recv(void __iomem *io, unsigned char *start, int n) |
| 214 | { |
| 215 | unsigned char *addr = start; |
| 216 | int result = 0; |
| 217 | |
| 218 | if (n >= 1) { |
| 219 | MOVE_BYTE("%3@,%0@"); |
| 220 | if (result) |
| 221 | goto out; |
| 222 | } |
| 223 | if (n >= 1 && ((unsigned long)addr & 1)) { |
| 224 | MOVE_BYTE("%3@,%0@"); |
| 225 | if (result) |
| 226 | goto out; |
| 227 | } |
| 228 | while (n >= 32) |
| 229 | MOVE_16_WORDS("%3@,%0@+"); |
| 230 | while (n >= 2) |
| 231 | MOVE_WORD("%3@,%0@+"); |
| 232 | if (result) |
| 233 | return start - addr; /* Negated to indicate uncertain length */ |
| 234 | if (n == 1) |
| 235 | MOVE_BYTE("%3@,%0@"); |
| 236 | out: |
| 237 | return addr - start; |
| 238 | } |
| 239 | |
| 240 | static inline int mac_pdma_send(unsigned char *start, void __iomem *io, int n) |
| 241 | { |
| 242 | unsigned char *addr = start; |
| 243 | int result = 0; |
| 244 | |
| 245 | if (n >= 1) { |
| 246 | MOVE_BYTE("%0@,%3@"); |
| 247 | if (result) |
| 248 | goto out; |
| 249 | } |
| 250 | if (n >= 1 && ((unsigned long)addr & 1)) { |
| 251 | MOVE_BYTE("%0@,%3@"); |
| 252 | if (result) |
| 253 | goto out; |
| 254 | } |
| 255 | while (n >= 32) |
| 256 | MOVE_16_WORDS("%0@+,%3@"); |
| 257 | while (n >= 2) |
| 258 | MOVE_WORD("%0@+,%3@"); |
| 259 | if (result) |
| 260 | return start - addr; /* Negated to indicate uncertain length */ |
| 261 | if (n == 1) |
| 262 | MOVE_BYTE("%0@,%3@"); |
| 263 | out: |
| 264 | return addr - start; |
| 265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 267 | /* The "SCSI DMA" chip on the IIfx implements this register. */ |
| 268 | #define CTRL_REG 0x8 |
| 269 | #define CTRL_INTERRUPTS_ENABLE BIT(1) |
| 270 | #define CTRL_HANDSHAKE_MODE BIT(3) |
| 271 | |
| 272 | static inline void write_ctrl_reg(struct NCR5380_hostdata *hostdata, u32 value) |
| 273 | { |
| 274 | out_be32(hostdata->io + (CTRL_REG << 4), value); |
| 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 277 | static inline int macscsi_pread(struct NCR5380_hostdata *hostdata, |
| 278 | unsigned char *dst, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 280 | u8 __iomem *s = hostdata->pdma_io + (INPUT_DATA_REG << 4); |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 281 | unsigned char *d = dst; |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 282 | int result = 0; |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 283 | |
| 284 | hostdata->pdma_residual = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 286 | while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 287 | BASR_DRQ | BASR_PHASE_MATCH, |
Ahmed S. Darwish | e7734ef | 2020-12-06 08:51:57 +0100 | [diff] [blame] | 288 | BASR_DRQ | BASR_PHASE_MATCH, 0)) { |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 289 | int bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 291 | if (macintosh_config->ident == MAC_MODEL_IIFX) |
| 292 | write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE | |
| 293 | CTRL_INTERRUPTS_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 295 | bytes = mac_pdma_recv(s, d, min(hostdata->pdma_residual, 512)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 297 | if (bytes > 0) { |
| 298 | d += bytes; |
| 299 | hostdata->pdma_residual -= bytes; |
| 300 | } |
| 301 | |
| 302 | if (hostdata->pdma_residual == 0) |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 303 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 305 | if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ, |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 306 | BUS_AND_STATUS_REG, BASR_ACK, |
Ahmed S. Darwish | e7734ef | 2020-12-06 08:51:57 +0100 | [diff] [blame] | 307 | BASR_ACK, 0) < 0) |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 308 | scmd_printk(KERN_DEBUG, hostdata->connected, |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 309 | "%s: !REQ and !ACK\n", __func__); |
| 310 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 311 | goto out; |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 312 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 313 | if (bytes == 0) |
| 314 | udelay(MAC_PDMA_DELAY); |
| 315 | |
| 316 | if (bytes >= 0) |
| 317 | continue; |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 318 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 319 | dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host, |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 320 | "%s: bus error (%d/%d)\n", __func__, d - dst, len); |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 321 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host); |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 322 | result = -1; |
| 323 | goto out; |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 324 | } |
| 325 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 326 | scmd_printk(KERN_ERR, hostdata->connected, |
| 327 | "%s: phase mismatch or !DRQ\n", __func__); |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 328 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host); |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 329 | result = -1; |
| 330 | out: |
| 331 | if (macintosh_config->ident == MAC_MODEL_IIFX) |
| 332 | write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE); |
| 333 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 336 | static inline int macscsi_pwrite(struct NCR5380_hostdata *hostdata, |
| 337 | unsigned char *src, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 339 | unsigned char *s = src; |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 340 | u8 __iomem *d = hostdata->pdma_io + (OUTPUT_DATA_REG << 4); |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 341 | int result = 0; |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 342 | |
| 343 | hostdata->pdma_residual = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Finn Thain | d5d37a0 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 345 | while (!NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 346 | BASR_DRQ | BASR_PHASE_MATCH, |
Ahmed S. Darwish | e7734ef | 2020-12-06 08:51:57 +0100 | [diff] [blame] | 347 | BASR_DRQ | BASR_PHASE_MATCH, 0)) { |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 348 | int bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 350 | if (macintosh_config->ident == MAC_MODEL_IIFX) |
| 351 | write_ctrl_reg(hostdata, CTRL_HANDSHAKE_MODE | |
| 352 | CTRL_INTERRUPTS_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 354 | bytes = mac_pdma_send(s, d, min(hostdata->pdma_residual, 512)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 356 | if (bytes > 0) { |
| 357 | s += bytes; |
| 358 | hostdata->pdma_residual -= bytes; |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 359 | } |
| 360 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 361 | if (hostdata->pdma_residual == 0) { |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 362 | if (NCR5380_poll_politely(hostdata, TARGET_COMMAND_REG, |
| 363 | TCR_LAST_BYTE_SENT, |
Finn Thain | 7c1f3e3 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 364 | TCR_LAST_BYTE_SENT, |
Ahmed S. Darwish | e7734ef | 2020-12-06 08:51:57 +0100 | [diff] [blame] | 365 | 0) < 0) { |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 366 | scmd_printk(KERN_ERR, hostdata->connected, |
| 367 | "%s: Last Byte Sent timeout\n", __func__); |
Finn Thain | 7c1f3e3 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 368 | result = -1; |
| 369 | } |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 370 | goto out; |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 371 | } |
| 372 | |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 373 | if (NCR5380_poll_politely2(hostdata, STATUS_REG, SR_REQ, SR_REQ, |
| 374 | BUS_AND_STATUS_REG, BASR_ACK, |
Ahmed S. Darwish | e7734ef | 2020-12-06 08:51:57 +0100 | [diff] [blame] | 375 | BASR_ACK, 0) < 0) |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 376 | scmd_printk(KERN_DEBUG, hostdata->connected, |
| 377 | "%s: !REQ and !ACK\n", __func__); |
| 378 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 379 | goto out; |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 380 | |
| 381 | if (bytes == 0) |
| 382 | udelay(MAC_PDMA_DELAY); |
| 383 | |
| 384 | if (bytes >= 0) |
| 385 | continue; |
| 386 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 387 | dsprintk(NDEBUG_PSEUDO_DMA, hostdata->host, |
Finn Thain | 78ff751 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 388 | "%s: bus error (%d/%d)\n", __func__, s - src, len); |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 389 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host); |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 390 | result = -1; |
| 391 | goto out; |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 392 | } |
| 393 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 394 | scmd_printk(KERN_ERR, hostdata->connected, |
| 395 | "%s: phase mismatch or !DRQ\n", __func__); |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 396 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, hostdata->host); |
Finn Thain | 8fb9a64 | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 397 | result = -1; |
| 398 | out: |
| 399 | if (macintosh_config->ident == MAC_MODEL_IIFX) |
| 400 | write_ctrl_reg(hostdata, CTRL_INTERRUPTS_ENABLE); |
| 401 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 404 | static int macscsi_dma_xfer_len(struct NCR5380_hostdata *hostdata, |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 405 | struct scsi_cmnd *cmd) |
| 406 | { |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 407 | if (hostdata->flags & FLAG_NO_PSEUDO_DMA || |
Finn Thain | 7398cee | 2019-06-09 11:19:11 +1000 | [diff] [blame] | 408 | cmd->SCp.this_residual < setup_use_pdma) |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 409 | return 0; |
| 410 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 411 | return cmd->SCp.this_residual; |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 412 | } |
| 413 | |
Finn Thain | 4a98f89 | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 414 | static int macscsi_dma_residual(struct NCR5380_hostdata *hostdata) |
| 415 | { |
| 416 | return hostdata->pdma_residual; |
| 417 | } |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | #include "NCR5380.c" |
| 420 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 421 | #define DRV_MODULE_NAME "mac_scsi" |
| 422 | #define PFX DRV_MODULE_NAME ": " |
| 423 | |
| 424 | static struct scsi_host_template mac_scsi_template = { |
Finn Thain | aa2e2cb1 | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 425 | .module = THIS_MODULE, |
| 426 | .proc_name = DRV_MODULE_NAME, |
Finn Thain | aa2e2cb1 | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 427 | .name = "Macintosh NCR5380 SCSI", |
| 428 | .info = macscsi_info, |
| 429 | .queuecommand = macscsi_queue_command, |
| 430 | .eh_abort_handler = macscsi_abort, |
Hannes Reinecke | 12e5fc6 | 2017-08-25 13:57:10 +0200 | [diff] [blame] | 431 | .eh_host_reset_handler = macscsi_host_reset, |
Finn Thain | aa2e2cb1 | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 432 | .can_queue = 16, |
| 433 | .this_id = 7, |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 434 | .sg_tablesize = 1, |
Finn Thain | aa2e2cb1 | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 435 | .cmd_per_lun = 2, |
Christoph Hellwig | 4af14d1 | 2018-12-13 16:17:09 +0100 | [diff] [blame] | 436 | .dma_boundary = PAGE_SIZE - 1, |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 437 | .cmd_size = NCR5380_CMD_SIZE, |
Finn Thain | 0a4e361 | 2016-01-03 16:06:07 +1100 | [diff] [blame] | 438 | .max_sectors = 128, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 441 | static int __init mac_scsi_probe(struct platform_device *pdev) |
| 442 | { |
| 443 | struct Scsi_Host *instance; |
Finn Thain | 820682b | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 444 | struct NCR5380_hostdata *hostdata; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 445 | int error; |
| 446 | int host_flags = 0; |
| 447 | struct resource *irq, *pio_mem, *pdma_mem = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 449 | pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 450 | if (!pio_mem) |
| 451 | return -ENODEV; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 452 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 453 | pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 454 | |
| 455 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 456 | |
| 457 | if (!hwreg_present((unsigned char *)pio_mem->start + |
| 458 | (STATUS_REG << 4))) { |
| 459 | pr_info(PFX "no device detected at %pap\n", &pio_mem->start); |
| 460 | return -ENODEV; |
| 461 | } |
| 462 | |
| 463 | if (setup_can_queue > 0) |
| 464 | mac_scsi_template.can_queue = setup_can_queue; |
| 465 | if (setup_cmd_per_lun > 0) |
| 466 | mac_scsi_template.cmd_per_lun = setup_cmd_per_lun; |
Finn Thain | 79172ab | 2019-11-02 12:06:54 +1100 | [diff] [blame] | 467 | if (setup_sg_tablesize > 0) |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 468 | mac_scsi_template.sg_tablesize = setup_sg_tablesize; |
| 469 | if (setup_hostid >= 0) |
| 470 | mac_scsi_template.this_id = setup_hostid & 7; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 471 | |
| 472 | instance = scsi_host_alloc(&mac_scsi_template, |
| 473 | sizeof(struct NCR5380_hostdata)); |
| 474 | if (!instance) |
| 475 | return -ENOMEM; |
| 476 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 477 | if (irq) |
| 478 | instance->irq = irq->start; |
| 479 | else |
| 480 | instance->irq = NO_IRQ; |
| 481 | |
Finn Thain | 820682b | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 482 | hostdata = shost_priv(instance); |
| 483 | hostdata->base = pio_mem->start; |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 484 | hostdata->io = (u8 __iomem *)pio_mem->start; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 485 | |
Finn Thain | 820682b | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 486 | if (pdma_mem && setup_use_pdma) |
Finn Thain | 4ab2a78 | 2017-01-15 18:50:57 -0500 | [diff] [blame] | 487 | hostdata->pdma_io = (u8 __iomem *)pdma_mem->start; |
Finn Thain | 820682b | 2016-10-10 00:46:53 -0400 | [diff] [blame] | 488 | else |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 489 | host_flags |= FLAG_NO_PSEUDO_DMA; |
| 490 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 491 | host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 492 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 493 | error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 494 | if (error) |
| 495 | goto fail_init; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 496 | |
| 497 | if (instance->irq != NO_IRQ) { |
| 498 | error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED, |
| 499 | "NCR5380", instance); |
| 500 | if (error) |
| 501 | goto fail_irq; |
| 502 | } |
| 503 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 504 | NCR5380_maybe_reset_bus(instance); |
| 505 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 506 | error = scsi_add_host(instance, NULL); |
| 507 | if (error) |
| 508 | goto fail_host; |
| 509 | |
| 510 | platform_set_drvdata(pdev, instance); |
| 511 | |
| 512 | scsi_scan_host(instance); |
| 513 | return 0; |
| 514 | |
| 515 | fail_host: |
| 516 | if (instance->irq != NO_IRQ) |
| 517 | free_irq(instance->irq, instance); |
| 518 | fail_irq: |
| 519 | NCR5380_exit(instance); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 520 | fail_init: |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 521 | scsi_host_put(instance); |
| 522 | return error; |
| 523 | } |
| 524 | |
| 525 | static int __exit mac_scsi_remove(struct platform_device *pdev) |
| 526 | { |
| 527 | struct Scsi_Host *instance = platform_get_drvdata(pdev); |
| 528 | |
| 529 | scsi_remove_host(instance); |
| 530 | if (instance->irq != NO_IRQ) |
| 531 | free_irq(instance->irq, instance); |
| 532 | NCR5380_exit(instance); |
| 533 | scsi_host_put(instance); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static struct platform_driver mac_scsi_driver = { |
| 538 | .remove = __exit_p(mac_scsi_remove), |
| 539 | .driver = { |
| 540 | .name = DRV_MODULE_NAME, |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 541 | }, |
| 542 | }; |
| 543 | |
| 544 | module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe); |
| 545 | |
| 546 | MODULE_ALIAS("platform:" DRV_MODULE_NAME); |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 547 | MODULE_LICENSE("GPL"); |