Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic Macintosh NCR5380 driver |
| 3 | * |
| 4 | * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov> |
| 5 | * |
| 6 | * derived in part from: |
| 7 | */ |
| 8 | /* |
| 9 | * Generic Generic NCR5380 driver |
| 10 | * |
| 11 | * Copyright 1995, Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/ioport.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/interrupt.h> |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 22 | #include <asm/hwtest.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/macints.h> |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 25 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <scsi/scsi_host.h> |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 28 | |
| 29 | /* Definitions for the core NCR5380 driver. */ |
Boaz Harrosh | e744fde | 2007-10-16 10:25:01 +0200 | [diff] [blame] | 30 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 31 | #define NCR5380_implementation_fields unsigned char *pdma_base; \ |
| 32 | int pdma_residual |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 33 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 34 | #define NCR5380_read(reg) macscsi_read(instance, reg) |
| 35 | #define NCR5380_write(reg, value) macscsi_write(instance, reg, value) |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 36 | |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 37 | #define NCR5380_dma_xfer_len(instance, cmd, phase) \ |
| 38 | macscsi_dma_xfer_len(instance, cmd) |
Finn Thain | 6c4b88c | 2016-03-23 21:10:17 +1100 | [diff] [blame] | 39 | #define NCR5380_dma_recv_setup macscsi_pread |
| 40 | #define NCR5380_dma_send_setup macscsi_pwrite |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 41 | #define NCR5380_dma_residual(instance) (hostdata->pdma_residual) |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 42 | |
| 43 | #define NCR5380_intr macscsi_intr |
| 44 | #define NCR5380_queue_command macscsi_queue_command |
| 45 | #define NCR5380_abort macscsi_abort |
| 46 | #define NCR5380_bus_reset macscsi_bus_reset |
| 47 | #define NCR5380_info macscsi_info |
Finn Thain | 92de383 | 2014-11-12 16:12:04 +1100 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include "NCR5380.h" |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static int setup_can_queue = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 52 | module_param(setup_can_queue, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | static int setup_cmd_per_lun = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 54 | module_param(setup_cmd_per_lun, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | static int setup_sg_tablesize = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 56 | module_param(setup_sg_tablesize, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static int setup_use_pdma = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 58 | module_param(setup_use_pdma, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static int setup_hostid = -1; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 60 | module_param(setup_hostid, int, 0); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 61 | static int setup_toshiba_delay = -1; |
| 62 | module_param(setup_toshiba_delay, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* |
| 65 | * NCR 5380 register access functions |
| 66 | */ |
| 67 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 68 | static inline char macscsi_read(struct Scsi_Host *instance, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 70 | return in_8(instance->base + (reg << 4)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 73 | static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 75 | out_8(instance->base + (reg << 4), value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
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 | #ifndef MODULE |
| 79 | static int __init mac_scsi_setup(char *str) |
| 80 | { |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 81 | int ints[8]; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 82 | |
| 83 | (void)get_options(str, ARRAY_SIZE(ints), ints); |
| 84 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 85 | if (ints[0] < 1) { |
| 86 | 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] | 87 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 89 | if (ints[0] >= 1) |
| 90 | setup_can_queue = ints[1]; |
| 91 | if (ints[0] >= 2) |
| 92 | setup_cmd_per_lun = ints[2]; |
| 93 | if (ints[0] >= 3) |
| 94 | setup_sg_tablesize = ints[3]; |
| 95 | if (ints[0] >= 4) |
| 96 | setup_hostid = ints[4]; |
Finn Thain | c4ec6f9 | 2016-03-23 21:10:22 +1100 | [diff] [blame] | 97 | /* ints[5] (use_tagged_queuing) is ignored */ |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 98 | if (ints[0] >= 6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | setup_use_pdma = ints[6]; |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 100 | if (ints[0] >= 7) |
| 101 | setup_toshiba_delay = ints[7]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | __setup("mac5380=", mac_scsi_setup); |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 106 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 108 | /* Pseudo DMA asm originally by Ove Edlund */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 110 | #define CP_IO_TO_MEM(s,d,n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | __asm__ __volatile__ \ |
| 112 | (" cmp.w #4,%2\n" \ |
| 113 | " bls 8f\n" \ |
| 114 | " move.w %1,%%d0\n" \ |
| 115 | " neg.b %%d0\n" \ |
| 116 | " and.w #3,%%d0\n" \ |
| 117 | " sub.w %%d0,%2\n" \ |
| 118 | " bra 2f\n" \ |
| 119 | " 1: move.b (%0),(%1)+\n" \ |
| 120 | " 2: dbf %%d0,1b\n" \ |
| 121 | " move.w %2,%%d0\n" \ |
| 122 | " lsr.w #5,%%d0\n" \ |
| 123 | " bra 4f\n" \ |
| 124 | " 3: move.l (%0),(%1)+\n" \ |
| 125 | "31: move.l (%0),(%1)+\n" \ |
| 126 | "32: move.l (%0),(%1)+\n" \ |
| 127 | "33: move.l (%0),(%1)+\n" \ |
| 128 | "34: move.l (%0),(%1)+\n" \ |
| 129 | "35: move.l (%0),(%1)+\n" \ |
| 130 | "36: move.l (%0),(%1)+\n" \ |
| 131 | "37: move.l (%0),(%1)+\n" \ |
| 132 | " 4: dbf %%d0,3b\n" \ |
| 133 | " move.w %2,%%d0\n" \ |
| 134 | " lsr.w #2,%%d0\n" \ |
| 135 | " and.w #7,%%d0\n" \ |
| 136 | " bra 6f\n" \ |
| 137 | " 5: move.l (%0),(%1)+\n" \ |
| 138 | " 6: dbf %%d0,5b\n" \ |
| 139 | " and.w #3,%2\n" \ |
| 140 | " bra 8f\n" \ |
| 141 | " 7: move.b (%0),(%1)+\n" \ |
| 142 | " 8: dbf %2,7b\n" \ |
| 143 | " moveq.l #0, %2\n" \ |
| 144 | " 9: \n" \ |
| 145 | ".section .fixup,\"ax\"\n" \ |
| 146 | " .even\n" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 147 | "91: moveq.l #1, %2\n" \ |
| 148 | " jra 9b\n" \ |
| 149 | "94: moveq.l #4, %2\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | " jra 9b\n" \ |
| 151 | ".previous\n" \ |
| 152 | ".section __ex_table,\"a\"\n" \ |
| 153 | " .align 4\n" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 154 | " .long 1b,91b\n" \ |
| 155 | " .long 3b,94b\n" \ |
| 156 | " .long 31b,94b\n" \ |
| 157 | " .long 32b,94b\n" \ |
| 158 | " .long 33b,94b\n" \ |
| 159 | " .long 34b,94b\n" \ |
| 160 | " .long 35b,94b\n" \ |
| 161 | " .long 36b,94b\n" \ |
| 162 | " .long 37b,94b\n" \ |
| 163 | " .long 5b,94b\n" \ |
| 164 | " .long 7b,91b\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | ".previous" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 166 | : "=a"(s), "=a"(d), "=d"(n) \ |
| 167 | : "0"(s), "1"(d), "2"(n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | : "d0") |
| 169 | |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 170 | static int macscsi_pread(struct Scsi_Host *instance, |
| 171 | unsigned char *dst, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 173 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 174 | unsigned char *s = hostdata->pdma_base + (INPUT_DATA_REG << 4); |
| 175 | unsigned char *d = dst; |
| 176 | int n = len; |
| 177 | int transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 179 | while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, |
| 180 | BASR_DRQ | BASR_PHASE_MATCH, |
| 181 | BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) { |
| 182 | CP_IO_TO_MEM(s, d, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 184 | transferred = d - dst - n; |
| 185 | hostdata->pdma_residual = len - transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 187 | /* No bus error. */ |
| 188 | if (n == 0) |
| 189 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 191 | /* Target changed phase early? */ |
| 192 | if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ, |
| 193 | BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0) |
| 194 | scmd_printk(KERN_ERR, hostdata->connected, |
| 195 | "%s: !REQ and !ACK\n", __func__); |
| 196 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) |
| 197 | return 0; |
| 198 | |
| 199 | dsprintk(NDEBUG_PSEUDO_DMA, instance, |
| 200 | "%s: bus error (%d/%d)\n", __func__, transferred, len); |
| 201 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance); |
| 202 | d = dst + transferred; |
| 203 | n = len - transferred; |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 204 | } |
| 205 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 206 | scmd_printk(KERN_ERR, hostdata->connected, |
| 207 | "%s: phase mismatch or !DRQ\n", __func__); |
| 208 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance); |
| 209 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 213 | #define CP_MEM_TO_IO(s,d,n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | __asm__ __volatile__ \ |
| 215 | (" cmp.w #4,%2\n" \ |
| 216 | " bls 8f\n" \ |
| 217 | " move.w %0,%%d0\n" \ |
| 218 | " neg.b %%d0\n" \ |
| 219 | " and.w #3,%%d0\n" \ |
| 220 | " sub.w %%d0,%2\n" \ |
| 221 | " bra 2f\n" \ |
| 222 | " 1: move.b (%0)+,(%1)\n" \ |
| 223 | " 2: dbf %%d0,1b\n" \ |
| 224 | " move.w %2,%%d0\n" \ |
| 225 | " lsr.w #5,%%d0\n" \ |
| 226 | " bra 4f\n" \ |
| 227 | " 3: move.l (%0)+,(%1)\n" \ |
| 228 | "31: move.l (%0)+,(%1)\n" \ |
| 229 | "32: move.l (%0)+,(%1)\n" \ |
| 230 | "33: move.l (%0)+,(%1)\n" \ |
| 231 | "34: move.l (%0)+,(%1)\n" \ |
| 232 | "35: move.l (%0)+,(%1)\n" \ |
| 233 | "36: move.l (%0)+,(%1)\n" \ |
| 234 | "37: move.l (%0)+,(%1)\n" \ |
| 235 | " 4: dbf %%d0,3b\n" \ |
| 236 | " move.w %2,%%d0\n" \ |
| 237 | " lsr.w #2,%%d0\n" \ |
| 238 | " and.w #7,%%d0\n" \ |
| 239 | " bra 6f\n" \ |
| 240 | " 5: move.l (%0)+,(%1)\n" \ |
| 241 | " 6: dbf %%d0,5b\n" \ |
| 242 | " and.w #3,%2\n" \ |
| 243 | " bra 8f\n" \ |
| 244 | " 7: move.b (%0)+,(%1)\n" \ |
| 245 | " 8: dbf %2,7b\n" \ |
| 246 | " moveq.l #0, %2\n" \ |
| 247 | " 9: \n" \ |
| 248 | ".section .fixup,\"ax\"\n" \ |
| 249 | " .even\n" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 250 | "91: moveq.l #1, %2\n" \ |
| 251 | " jra 9b\n" \ |
| 252 | "94: moveq.l #4, %2\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | " jra 9b\n" \ |
| 254 | ".previous\n" \ |
| 255 | ".section __ex_table,\"a\"\n" \ |
| 256 | " .align 4\n" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 257 | " .long 1b,91b\n" \ |
| 258 | " .long 3b,94b\n" \ |
| 259 | " .long 31b,94b\n" \ |
| 260 | " .long 32b,94b\n" \ |
| 261 | " .long 33b,94b\n" \ |
| 262 | " .long 34b,94b\n" \ |
| 263 | " .long 35b,94b\n" \ |
| 264 | " .long 36b,94b\n" \ |
| 265 | " .long 37b,94b\n" \ |
| 266 | " .long 5b,94b\n" \ |
| 267 | " .long 7b,91b\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | ".previous" \ |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 269 | : "=a"(s), "=a"(d), "=d"(n) \ |
| 270 | : "0"(s), "1"(d), "2"(n) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | : "d0") |
| 272 | |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 273 | static int macscsi_pwrite(struct Scsi_Host *instance, |
| 274 | unsigned char *src, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 276 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 277 | unsigned char *s = src; |
| 278 | unsigned char *d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4); |
| 279 | int n = len; |
| 280 | int transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 282 | while (!NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, |
| 283 | BASR_DRQ | BASR_PHASE_MATCH, |
| 284 | BASR_DRQ | BASR_PHASE_MATCH, HZ / 64)) { |
| 285 | CP_MEM_TO_IO(s, d, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 287 | transferred = s - src - n; |
| 288 | hostdata->pdma_residual = len - transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 290 | /* Target changed phase early? */ |
| 291 | if (NCR5380_poll_politely2(instance, STATUS_REG, SR_REQ, SR_REQ, |
| 292 | BUS_AND_STATUS_REG, BASR_ACK, BASR_ACK, HZ / 64) < 0) |
| 293 | scmd_printk(KERN_ERR, hostdata->connected, |
| 294 | "%s: !REQ and !ACK\n", __func__); |
| 295 | if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)) |
| 296 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 298 | /* No bus error. */ |
| 299 | if (n == 0) { |
| 300 | if (NCR5380_poll_politely(instance, TARGET_COMMAND_REG, |
| 301 | TCR_LAST_BYTE_SENT, |
| 302 | TCR_LAST_BYTE_SENT, HZ / 64) < 0) |
| 303 | scmd_printk(KERN_ERR, hostdata->connected, |
| 304 | "%s: Last Byte Sent timeout\n", __func__); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | dsprintk(NDEBUG_PSEUDO_DMA, instance, |
| 309 | "%s: bus error (%d/%d)\n", __func__, transferred, len); |
| 310 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance); |
| 311 | s = src + transferred; |
| 312 | n = len - transferred; |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 313 | } |
| 314 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 315 | scmd_printk(KERN_ERR, hostdata->connected, |
| 316 | "%s: phase mismatch or !DRQ\n", __func__); |
| 317 | NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance); |
Finn Thain | ffdede6 | 2014-11-12 16:12:06 +1100 | [diff] [blame] | 318 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 319 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 322 | static int macscsi_dma_xfer_len(struct Scsi_Host *instance, |
| 323 | struct scsi_cmnd *cmd) |
| 324 | { |
| 325 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 326 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 327 | if (hostdata->flags & FLAG_NO_PSEUDO_DMA || |
| 328 | cmd->SCp.this_residual < 16) |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 329 | return 0; |
| 330 | |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 331 | return cmd->SCp.this_residual; |
Finn Thain | 7e9ec8d | 2016-03-23 21:10:11 +1100 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | #include "NCR5380.c" |
| 335 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 336 | #define DRV_MODULE_NAME "mac_scsi" |
| 337 | #define PFX DRV_MODULE_NAME ": " |
| 338 | |
| 339 | static struct scsi_host_template mac_scsi_template = { |
Finn Thain | aa2e2cb | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 340 | .module = THIS_MODULE, |
| 341 | .proc_name = DRV_MODULE_NAME, |
Finn Thain | aa2e2cb | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 342 | .name = "Macintosh NCR5380 SCSI", |
| 343 | .info = macscsi_info, |
| 344 | .queuecommand = macscsi_queue_command, |
| 345 | .eh_abort_handler = macscsi_abort, |
| 346 | .eh_bus_reset_handler = macscsi_bus_reset, |
| 347 | .can_queue = 16, |
| 348 | .this_id = 7, |
Finn Thain | 3a0f64b | 2016-03-23 21:10:31 +1100 | [diff] [blame] | 349 | .sg_tablesize = 1, |
Finn Thain | aa2e2cb | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 350 | .cmd_per_lun = 2, |
| 351 | .use_clustering = DISABLE_CLUSTERING, |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 352 | .cmd_size = NCR5380_CMD_SIZE, |
Finn Thain | 0a4e361 | 2016-01-03 16:06:07 +1100 | [diff] [blame] | 353 | .max_sectors = 128, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 356 | static int __init mac_scsi_probe(struct platform_device *pdev) |
| 357 | { |
| 358 | struct Scsi_Host *instance; |
| 359 | int error; |
| 360 | int host_flags = 0; |
| 361 | struct resource *irq, *pio_mem, *pdma_mem = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 363 | pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 364 | if (!pio_mem) |
| 365 | return -ENODEV; |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 366 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 367 | pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 368 | |
| 369 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 370 | |
| 371 | if (!hwreg_present((unsigned char *)pio_mem->start + |
| 372 | (STATUS_REG << 4))) { |
| 373 | pr_info(PFX "no device detected at %pap\n", &pio_mem->start); |
| 374 | return -ENODEV; |
| 375 | } |
| 376 | |
| 377 | if (setup_can_queue > 0) |
| 378 | mac_scsi_template.can_queue = setup_can_queue; |
| 379 | if (setup_cmd_per_lun > 0) |
| 380 | mac_scsi_template.cmd_per_lun = setup_cmd_per_lun; |
| 381 | if (setup_sg_tablesize >= 0) |
| 382 | mac_scsi_template.sg_tablesize = setup_sg_tablesize; |
| 383 | if (setup_hostid >= 0) |
| 384 | mac_scsi_template.this_id = setup_hostid & 7; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 385 | |
| 386 | instance = scsi_host_alloc(&mac_scsi_template, |
| 387 | sizeof(struct NCR5380_hostdata)); |
| 388 | if (!instance) |
| 389 | return -ENOMEM; |
| 390 | |
| 391 | instance->base = pio_mem->start; |
| 392 | if (irq) |
| 393 | instance->irq = irq->start; |
| 394 | else |
| 395 | instance->irq = NO_IRQ; |
| 396 | |
| 397 | if (pdma_mem && setup_use_pdma) { |
| 398 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 399 | |
| 400 | hostdata->pdma_base = (unsigned char *)pdma_mem->start; |
| 401 | } else |
| 402 | host_flags |= FLAG_NO_PSEUDO_DMA; |
| 403 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 404 | host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 405 | |
Finn Thain | 8053b0e | 2016-03-23 21:10:19 +1100 | [diff] [blame] | 406 | error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 407 | if (error) |
| 408 | goto fail_init; |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 409 | |
| 410 | if (instance->irq != NO_IRQ) { |
| 411 | error = request_irq(instance->irq, macscsi_intr, IRQF_SHARED, |
| 412 | "NCR5380", instance); |
| 413 | if (error) |
| 414 | goto fail_irq; |
| 415 | } |
| 416 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 417 | NCR5380_maybe_reset_bus(instance); |
| 418 | |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 419 | error = scsi_add_host(instance, NULL); |
| 420 | if (error) |
| 421 | goto fail_host; |
| 422 | |
| 423 | platform_set_drvdata(pdev, instance); |
| 424 | |
| 425 | scsi_scan_host(instance); |
| 426 | return 0; |
| 427 | |
| 428 | fail_host: |
| 429 | if (instance->irq != NO_IRQ) |
| 430 | free_irq(instance->irq, instance); |
| 431 | fail_irq: |
| 432 | NCR5380_exit(instance); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 433 | fail_init: |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 434 | scsi_host_put(instance); |
| 435 | return error; |
| 436 | } |
| 437 | |
| 438 | static int __exit mac_scsi_remove(struct platform_device *pdev) |
| 439 | { |
| 440 | struct Scsi_Host *instance = platform_get_drvdata(pdev); |
| 441 | |
| 442 | scsi_remove_host(instance); |
| 443 | if (instance->irq != NO_IRQ) |
| 444 | free_irq(instance->irq, instance); |
| 445 | NCR5380_exit(instance); |
| 446 | scsi_host_put(instance); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static struct platform_driver mac_scsi_driver = { |
| 451 | .remove = __exit_p(mac_scsi_remove), |
| 452 | .driver = { |
| 453 | .name = DRV_MODULE_NAME, |
Finn Thain | cbad48d | 2014-11-12 16:12:07 +1100 | [diff] [blame] | 454 | }, |
| 455 | }; |
| 456 | |
| 457 | module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe); |
| 458 | |
| 459 | MODULE_ALIAS("platform:" DRV_MODULE_NAME); |
Finn Thain | 6e9ae6d | 2014-11-12 16:12:05 +1100 | [diff] [blame] | 460 | MODULE_LICENSE("GPL"); |