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 12 */ 13 14 #include <linux/types.h> 15 #include <linux/module.h> 16 #include <linux/ioport.h> 17 #include <linux/init.h> 18 #include <linux/blkdev.h> 19 #include <linux/interrupt.h> 20 #include <linux/platform_device.h> 21 22 #include <asm/hwtest.h> 23 #include <asm/io.h> 24 #include <asm/macints.h> 25 #include <asm/setup.h> 26 27 #include <scsi/scsi_host.h> 28 29 /* Definitions for the core NCR5380 driver. */ 30 31 #define NCR5380_implementation_fields unsigned char *pdma_base; \ 32 int pdma_residual 33 34 #define NCR5380_read(reg) macscsi_read(instance, reg) 35 #define NCR5380_write(reg, value) macscsi_write(instance, reg, value) 36 37 #define NCR5380_dma_xfer_len(instance, cmd, phase) \ 38 macscsi_dma_xfer_len(instance, cmd) 39 #define NCR5380_dma_recv_setup macscsi_pread 40 #define NCR5380_dma_send_setup macscsi_pwrite 41 #define NCR5380_dma_residual(instance) (hostdata->pdma_residual) 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 48 49 #include "NCR5380.h" 50 51 static int setup_can_queue = -1; 52 module_param(setup_can_queue, int, 0); 53 static int setup_cmd_per_lun = -1; 54 module_param(setup_cmd_per_lun, int, 0); 55 static int setup_sg_tablesize = -1; 56 module_param(setup_sg_tablesize, int, 0); 57 static int setup_use_pdma = -1; 58 module_param(setup_use_pdma, int, 0); 59 static int setup_hostid = -1; 60 module_param(setup_hostid, int, 0); 61 static int setup_toshiba_delay = -1; 62 module_param(setup_toshiba_delay, int, 0); 63 64 /* 65 * NCR 5380 register access functions 66 */ 67 68 static inline char macscsi_read(struct Scsi_Host *instance, int reg) 69 { 70 return in_8(instance->base + (reg << 4)); 71 } 72 73 static inline void macscsi_write(struct Scsi_Host *instance, int reg, int value) 74 { 75 out_8(instance->base + (reg << 4), value); 76 } 77 78 #ifndef MODULE 79 static int __init mac_scsi_setup(char *str) 80 { 81 int ints[8]; 82 83 (void)get_options(str, ARRAY_SIZE(ints), ints); 84 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"); 87 return 0; 88 } 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]; 97 /* ints[5] (use_tagged_queuing) is ignored */ 98 if (ints[0] >= 6) 99 setup_use_pdma = ints[6]; 100 if (ints[0] >= 7) 101 setup_toshiba_delay = ints[7]; 102 return 1; 103 } 104 105 __setup("mac5380=", mac_scsi_setup); 106 #endif /* !MODULE */ 107 108 /* Pseudo DMA asm originally by Ove Edlund */ 109 110 #define CP_IO_TO_MEM(s,d,n) \ 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" \ 147 "91: moveq.l #1, %2\n" \ 148 " jra 9b\n" \ 149 "94: moveq.l #4, %2\n" \ 150 " jra 9b\n" \ 151 ".previous\n" \ 152 ".section __ex_table,\"a\"\n" \ 153 " .align 4\n" \ 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" \ 165 ".previous" \ 166 : "=a"(s), "=a"(d), "=d"(n) \ 167 : "0"(s), "1"(d), "2"(n) \ 168 : "d0") 169 170 static int macscsi_pread(struct Scsi_Host *instance, 171 unsigned char *dst, int len) 172 { 173 struct NCR5380_hostdata *hostdata = shost_priv(instance); 174 unsigned char *s = hostdata->pdma_base + (INPUT_DATA_REG << 4); 175 unsigned char *d = dst; 176 int n = len; 177 int transferred; 178 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); 183 184 transferred = d - dst - n; 185 hostdata->pdma_residual = len - transferred; 186 187 /* No bus error. */ 188 if (n == 0) 189 return 0; 190 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; 204 } 205 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; 210 } 211 212 213 #define CP_MEM_TO_IO(s,d,n) \ 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" \ 250 "91: moveq.l #1, %2\n" \ 251 " jra 9b\n" \ 252 "94: moveq.l #4, %2\n" \ 253 " jra 9b\n" \ 254 ".previous\n" \ 255 ".section __ex_table,\"a\"\n" \ 256 " .align 4\n" \ 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" \ 268 ".previous" \ 269 : "=a"(s), "=a"(d), "=d"(n) \ 270 : "0"(s), "1"(d), "2"(n) \ 271 : "d0") 272 273 static int macscsi_pwrite(struct Scsi_Host *instance, 274 unsigned char *src, int len) 275 { 276 struct NCR5380_hostdata *hostdata = shost_priv(instance); 277 unsigned char *s = src; 278 unsigned char *d = hostdata->pdma_base + (OUTPUT_DATA_REG << 4); 279 int n = len; 280 int transferred; 281 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); 286 287 transferred = s - src - n; 288 hostdata->pdma_residual = len - transferred; 289 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; 297 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; 313 } 314 315 scmd_printk(KERN_ERR, hostdata->connected, 316 "%s: phase mismatch or !DRQ\n", __func__); 317 NCR5380_dprint(NDEBUG_PSEUDO_DMA, instance); 318 319 return -1; 320 } 321 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 327 if (hostdata->flags & FLAG_NO_PSEUDO_DMA || 328 cmd->SCp.this_residual < 16) 329 return 0; 330 331 return cmd->SCp.this_residual; 332 } 333 334 #include "NCR5380.c" 335 336 #define DRV_MODULE_NAME "mac_scsi" 337 #define PFX DRV_MODULE_NAME ": " 338 339 static struct scsi_host_template mac_scsi_template = { 340 .module = THIS_MODULE, 341 .proc_name = DRV_MODULE_NAME, 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, 349 .sg_tablesize = 1, 350 .cmd_per_lun = 2, 351 .use_clustering = DISABLE_CLUSTERING, 352 .cmd_size = NCR5380_CMD_SIZE, 353 .max_sectors = 128, 354 }; 355 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; 362 363 pio_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 364 if (!pio_mem) 365 return -ENODEV; 366 367 pdma_mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 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; 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 404 host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; 405 406 error = NCR5380_init(instance, host_flags | FLAG_LATE_DMA_SETUP); 407 if (error) 408 goto fail_init; 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 417 NCR5380_maybe_reset_bus(instance); 418 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); 433 fail_init: 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, 454 }, 455 }; 456 457 module_platform_driver_probe(mac_scsi_driver, mac_scsi_probe); 458 459 MODULE_ALIAS("platform:" DRV_MODULE_NAME); 460 MODULE_LICENSE("GPL"); 461