2 * Generic Generic NCR5380 driver
4 * Copyright 1993, Drew Eckhardt
6 * (Unix and Linux consulting and custom programming)
10 * NCR53C400 extensions (c) 1994,1995,1996, Kevin Lentin
11 * K.Lentin@cs.monash.edu.au
13 * NCR53C400A extensions (c) 1996, Ingmar Baumgart
14 * ingmar@gonzo.schwaben.de
16 * DTC3181E extensions (c) 1997, Ronald van Cuijlenborg
17 * ronald.van.cuijlenborg@tip.nl or nutty@dds.nl
19 * Added ISAPNP support for DTC436 adapters,
20 * Thomas Sailer, sailer@ife.ee.ethz.ch
22 * See Documentation/scsi/g_NCR5380.txt for more info.
26 #include <linux/blkdev.h>
27 #include <linux/module.h>
28 #include <scsi/scsi_host.h>
29 #include "g_NCR5380.h"
31 #include <linux/init.h>
32 #include <linux/ioport.h>
33 #include <linux/isa.h>
34 #include <linux/pnp.h>
35 #include <linux/interrupt.h>
39 /* old-style parameters for compatibility */
40 static int ncr_irq
= -1;
43 static int ncr_53c400
;
44 static int ncr_53c400a
;
47 module_param(ncr_irq
, int, 0);
48 module_param(ncr_addr
, int, 0);
49 module_param(ncr_5380
, int, 0);
50 module_param(ncr_53c400
, int, 0);
51 module_param(ncr_53c400a
, int, 0);
52 module_param(dtc_3181e
, int, 0);
53 module_param(hp_c2502
, int, 0);
55 static int irq
[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
56 module_param_array(irq
, int, NULL
, 0);
57 MODULE_PARM_DESC(irq
, "IRQ number(s) (0=none, 254=auto [default])");
59 static int base
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
60 module_param_array(base
, int, NULL
, 0);
61 MODULE_PARM_DESC(base
, "base address(es)");
63 static int card
[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
64 module_param_array(card
, int, NULL
, 0);
65 MODULE_PARM_DESC(card
, "card type (0=NCR5380, 1=NCR53C400, 2=NCR53C400A, 3=DTC3181E, 4=HP C2502)");
67 MODULE_ALIAS("g_NCR5380_mmio");
68 MODULE_LICENSE("GPL");
70 static void g_NCR5380_trigger_irq(struct Scsi_Host
*instance
)
72 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
75 * An interrupt is triggered whenever BSY = false, SEL = true
76 * and a bit set in the SELECT_ENABLE_REG is asserted on the
79 * Note that the bus is only driven when the phase control signals
80 * (I/O, C/D, and MSG) match those in the TCR.
82 NCR5380_write(TARGET_COMMAND_REG
,
83 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) & PHASE_MASK
));
84 NCR5380_write(SELECT_ENABLE_REG
, hostdata
->id_mask
);
85 NCR5380_write(OUTPUT_DATA_REG
, hostdata
->id_mask
);
86 NCR5380_write(INITIATOR_COMMAND_REG
,
87 ICR_BASE
| ICR_ASSERT_DATA
| ICR_ASSERT_SEL
);
91 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
);
92 NCR5380_write(SELECT_ENABLE_REG
, 0);
93 NCR5380_write(TARGET_COMMAND_REG
, 0);
97 * g_NCR5380_probe_irq - find the IRQ of a NCR5380 or equivalent
98 * @instance: SCSI host instance
100 * Autoprobe for the IRQ line used by the card by triggering an IRQ
101 * and then looking to see what interrupt actually turned up.
104 static int g_NCR5380_probe_irq(struct Scsi_Host
*instance
)
106 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
109 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
110 irq_mask
= probe_irq_on();
111 g_NCR5380_trigger_irq(instance
);
112 irq
= probe_irq_off(irq_mask
);
113 NCR5380_read(RESET_PARITY_INTERRUPT_REG
);
121 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
122 * to ports 0x779 and 0x379.
124 static void magic_configure(int idx
, u8 irq
, u8 magic
[])
128 outb(magic
[0], 0x779);
129 outb(magic
[1], 0x379);
130 outb(magic
[2], 0x379);
131 outb(magic
[3], 0x379);
132 outb(magic
[4], 0x379);
137 if (idx
>= 0 && idx
<= 7)
138 cfg
= 0x80 | idx
| (irq
<< 4);
142 static irqreturn_t
legacy_empty_irq_handler(int irq
, void *dev_id
)
147 static int legacy_find_free_irq(int *irq_table
)
149 while (*irq_table
!= -1) {
150 if (!request_irq(*irq_table
, legacy_empty_irq_handler
,
151 IRQF_PROBE_SHARED
, "Test IRQ",
152 (void *)irq_table
)) {
153 free_irq(*irq_table
, (void *) irq_table
);
161 static unsigned int ncr_53c400a_ports
[] = {
162 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
164 static unsigned int dtc_3181e_ports
[] = {
165 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
167 static u8 ncr_53c400a_magic
[] = { /* 53C400A & DTC436 */
168 0x59, 0xb9, 0xc5, 0xae, 0xa6
170 static u8 hp_c2502_magic
[] = { /* HP C2502 */
171 0x0f, 0x22, 0xf0, 0x20, 0x80
173 static int hp_c2502_irqs
[] = {
177 static int generic_NCR5380_init_one(struct scsi_host_template
*tpnt
,
178 struct device
*pdev
, int base
, int irq
, int board
)
180 bool is_pmio
= base
<= 0xffff;
183 unsigned int *ports
= NULL
;
187 unsigned long region_size
;
188 struct Scsi_Host
*instance
;
189 struct NCR5380_hostdata
*hostdata
;
194 flags
= FLAG_NO_PSEUDO_DMA
| FLAG_DMA_FIXUP
;
196 case BOARD_NCR53C400A
:
197 ports
= ncr_53c400a_ports
;
198 magic
= ncr_53c400a_magic
;
201 ports
= ncr_53c400a_ports
;
202 magic
= hp_c2502_magic
;
205 ports
= dtc_3181e_ports
;
206 magic
= ncr_53c400a_magic
;
210 if (is_pmio
&& ports
&& magic
) {
211 /* wakeup sequence for the NCR53C400A and DTC3181E */
213 /* Disable the adapter and look for a free io port */
214 magic_configure(-1, 0, magic
);
218 for (i
= 0; ports
[i
]; i
++) {
219 if (base
== ports
[i
]) { /* index found */
220 if (!request_region(ports
[i
],
228 for (i
= 0; ports
[i
]; i
++) {
229 if (!request_region(ports
[i
], region_size
,
232 if (inb(ports
[i
]) == 0xff)
234 release_region(ports
[i
], region_size
);
237 /* At this point we have our region reserved */
238 magic_configure(i
, 0, magic
); /* no IRQ yet */
240 outb(0xc0, base
+ 9);
241 if (inb(base
+ 9) != 0x80) {
248 } else if (is_pmio
) {
249 /* NCR5380 - no configuration, just grab */
251 if (!base
|| !request_region(base
, region_size
, "ncr5380"))
254 region_size
= NCR53C400_region_size
;
255 if (!request_mem_region(base
, region_size
, "ncr5380"))
260 iomem
= ioport_map(base
, region_size
);
262 iomem
= ioremap(base
, region_size
);
269 instance
= scsi_host_alloc(tpnt
, sizeof(struct NCR5380_hostdata
));
270 if (instance
== NULL
) {
274 hostdata
= shost_priv(instance
);
276 hostdata
->io
= iomem
;
277 hostdata
->region_size
= region_size
;
280 hostdata
->io_port
= base
;
281 hostdata
->io_width
= 1; /* 8-bit PDMA by default */
282 hostdata
->offset
= 0;
285 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
289 case BOARD_NCR53C400
:
290 hostdata
->io_port
+= 8;
291 hostdata
->c400_ctl_status
= 0;
292 hostdata
->c400_blk_cnt
= 1;
293 hostdata
->c400_host_buf
= 4;
296 hostdata
->io_width
= 2; /* 16-bit PDMA */
298 case BOARD_NCR53C400A
:
300 hostdata
->c400_ctl_status
= 9;
301 hostdata
->c400_blk_cnt
= 10;
302 hostdata
->c400_host_buf
= 8;
306 hostdata
->base
= base
;
307 hostdata
->offset
= NCR53C400_mem_base
;
309 case BOARD_NCR53C400
:
310 hostdata
->c400_ctl_status
= 0x100;
311 hostdata
->c400_blk_cnt
= 0x101;
312 hostdata
->c400_host_buf
= 0x104;
315 case BOARD_NCR53C400A
:
317 pr_err(DRV_MODULE_NAME
": unknown register offsets\n");
323 /* Check for vacant slot */
324 NCR5380_write(MODE_REG
, 0);
325 if (NCR5380_read(MODE_REG
) != 0) {
330 ret
= NCR5380_init(instance
, flags
| FLAG_LATE_DMA_SETUP
);
335 case BOARD_NCR53C400
:
337 case BOARD_NCR53C400A
:
339 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
);
342 NCR5380_maybe_reset_bus(instance
);
344 /* Compatibility with documented NCR5380 kernel parameters */
345 if (irq
== 255 || irq
== 0)
350 if (board
== BOARD_HP_C2502
) {
351 int *irq_table
= hp_c2502_irqs
;
359 board_irq
= legacy_find_free_irq(irq_table
);
362 while (*irq_table
!= -1)
363 if (*irq_table
++ == irq
)
367 if (board_irq
<= 0) {
372 magic_configure(port_idx
, board_irq
, magic
);
375 if (irq
== IRQ_AUTO
) {
376 instance
->irq
= g_NCR5380_probe_irq(instance
);
377 if (instance
->irq
== NO_IRQ
)
378 shost_printk(KERN_INFO
, instance
, "no irq detected\n");
381 if (instance
->irq
== NO_IRQ
)
382 shost_printk(KERN_INFO
, instance
, "no irq provided\n");
385 if (instance
->irq
!= NO_IRQ
) {
386 if (request_irq(instance
->irq
, generic_NCR5380_intr
,
387 0, "NCR5380", instance
)) {
388 instance
->irq
= NO_IRQ
;
389 shost_printk(KERN_INFO
, instance
,
390 "irq %d denied\n", instance
->irq
);
392 shost_printk(KERN_INFO
, instance
,
393 "irq %d acquired\n", instance
->irq
);
397 ret
= scsi_add_host(instance
, pdev
);
400 scsi_scan_host(instance
);
401 dev_set_drvdata(pdev
, instance
);
405 if (instance
->irq
!= NO_IRQ
)
406 free_irq(instance
->irq
, instance
);
407 NCR5380_exit(instance
);
409 scsi_host_put(instance
);
414 release_region(base
, region_size
);
416 release_mem_region(base
, region_size
);
420 static void generic_NCR5380_release_resources(struct Scsi_Host
*instance
)
422 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
423 void __iomem
*iomem
= hostdata
->io
;
424 unsigned long io_port
= hostdata
->io_port
;
425 unsigned long base
= hostdata
->base
;
426 unsigned long region_size
= hostdata
->region_size
;
428 scsi_remove_host(instance
);
429 if (instance
->irq
!= NO_IRQ
)
430 free_irq(instance
->irq
, instance
);
431 NCR5380_exit(instance
);
432 scsi_host_put(instance
);
435 release_region(io_port
, region_size
);
437 release_mem_region(base
, region_size
);
441 * generic_NCR5380_pread - pseudo DMA read
442 * @hostdata: scsi host private data
443 * @dst: buffer to read into
444 * @len: buffer length
446 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
450 static inline int generic_NCR5380_pread(struct NCR5380_hostdata
*hostdata
,
451 unsigned char *dst
, int len
)
453 int blocks
= len
/ 128;
456 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
| CSR_TRANS_DIR
);
457 NCR5380_write(hostdata
->c400_blk_cnt
, blocks
);
459 if (NCR5380_read(hostdata
->c400_blk_cnt
) == 0)
461 if (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
) {
462 printk(KERN_ERR
"53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start
, blocks
);
465 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
466 ; /* FIXME - no timeout */
468 if (hostdata
->io_port
&& hostdata
->io_width
== 2)
469 insw(hostdata
->io_port
+ hostdata
->c400_host_buf
,
471 else if (hostdata
->io_port
)
472 insb(hostdata
->io_port
+ hostdata
->c400_host_buf
,
475 memcpy_fromio(dst
+ start
,
476 hostdata
->io
+ NCR53C400_host_buffer
, 128);
483 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
484 ; /* FIXME - no timeout */
486 if (hostdata
->io_port
&& hostdata
->io_width
== 2)
487 insw(hostdata
->io_port
+ hostdata
->c400_host_buf
,
489 else if (hostdata
->io_port
)
490 insb(hostdata
->io_port
+ hostdata
->c400_host_buf
,
493 memcpy_fromio(dst
+ start
,
494 hostdata
->io
+ NCR53C400_host_buffer
, 128);
500 if (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
))
501 printk("53C400r: no 53C80 gated irq after transfer");
503 /* wait for 53C80 registers to be available */
504 while (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_53C80_REG
))
507 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_END_DMA_TRANSFER
))
508 printk(KERN_ERR
"53C400r: no end dma signal\n");
514 * generic_NCR5380_pwrite - pseudo DMA write
515 * @hostdata: scsi host private data
516 * @dst: buffer to read into
517 * @len: buffer length
519 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
523 static inline int generic_NCR5380_pwrite(struct NCR5380_hostdata
*hostdata
,
524 unsigned char *src
, int len
)
526 int blocks
= len
/ 128;
529 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
);
530 NCR5380_write(hostdata
->c400_blk_cnt
, blocks
);
532 if (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
) {
533 printk(KERN_ERR
"53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start
, blocks
);
537 if (NCR5380_read(hostdata
->c400_blk_cnt
) == 0)
539 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
542 if (hostdata
->io_port
&& hostdata
->io_width
== 2)
543 outsw(hostdata
->io_port
+ hostdata
->c400_host_buf
,
545 else if (hostdata
->io_port
)
546 outsb(hostdata
->io_port
+ hostdata
->c400_host_buf
,
549 memcpy_toio(hostdata
->io
+ NCR53C400_host_buffer
,
556 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
557 ; // FIXME - no timeout
559 if (hostdata
->io_port
&& hostdata
->io_width
== 2)
560 outsw(hostdata
->io_port
+ hostdata
->c400_host_buf
,
562 else if (hostdata
->io_port
)
563 outsb(hostdata
->io_port
+ hostdata
->c400_host_buf
,
566 memcpy_toio(hostdata
->io
+ NCR53C400_host_buffer
,
573 /* wait for 53C80 registers to be available */
574 while (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_53C80_REG
)) {
575 udelay(4); /* DTC436 chip hangs without this */
576 /* FIXME - no timeout */
579 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_END_DMA_TRANSFER
)) {
580 printk(KERN_ERR
"53C400w: no end dma signal\n");
583 while (!(NCR5380_read(TARGET_COMMAND_REG
) & TCR_LAST_BYTE_SENT
))
588 static int generic_NCR5380_dma_xfer_len(struct NCR5380_hostdata
*hostdata
,
589 struct scsi_cmnd
*cmd
)
591 int transfersize
= cmd
->transfersize
;
593 if (hostdata
->flags
& FLAG_NO_PSEUDO_DMA
)
596 /* Limit transfers to 32K, for xx400 & xx406
597 * pseudoDMA that transfers in 128 bytes blocks.
599 if (transfersize
> 32 * 1024 && cmd
->SCp
.this_residual
&&
600 !(cmd
->SCp
.this_residual
% transfersize
))
601 transfersize
= 32 * 1024;
603 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
604 if (transfersize
% 128)
611 * Include the NCR5380 core code that we build our driver around
616 static struct scsi_host_template driver_template
= {
617 .module
= THIS_MODULE
,
618 .proc_name
= DRV_MODULE_NAME
,
619 .name
= "Generic NCR5380/NCR53C400 SCSI",
620 .info
= generic_NCR5380_info
,
621 .queuecommand
= generic_NCR5380_queue_command
,
622 .eh_abort_handler
= generic_NCR5380_abort
,
623 .eh_bus_reset_handler
= generic_NCR5380_bus_reset
,
626 .sg_tablesize
= SG_ALL
,
628 .use_clustering
= DISABLE_CLUSTERING
,
629 .cmd_size
= NCR5380_CMD_SIZE
,
634 static int generic_NCR5380_isa_match(struct device
*pdev
, unsigned int ndev
)
636 int ret
= generic_NCR5380_init_one(&driver_template
, pdev
, base
[ndev
],
637 irq
[ndev
], card
[ndev
]);
640 printk(KERN_WARNING
"Card not found at address 0x%03x\n",
648 static int generic_NCR5380_isa_remove(struct device
*pdev
,
651 generic_NCR5380_release_resources(dev_get_drvdata(pdev
));
652 dev_set_drvdata(pdev
, NULL
);
656 static struct isa_driver generic_NCR5380_isa_driver
= {
657 .match
= generic_NCR5380_isa_match
,
658 .remove
= generic_NCR5380_isa_remove
,
660 .name
= DRV_MODULE_NAME
665 static struct pnp_device_id generic_NCR5380_pnp_ids
[] = {
666 { .id
= "DTC436e", .driver_data
= BOARD_DTC3181E
},
669 MODULE_DEVICE_TABLE(pnp
, generic_NCR5380_pnp_ids
);
671 static int generic_NCR5380_pnp_probe(struct pnp_dev
*pdev
,
672 const struct pnp_device_id
*id
)
676 if (pnp_activate_dev(pdev
) < 0)
679 base
= pnp_port_start(pdev
, 0);
680 irq
= pnp_irq(pdev
, 0);
682 return generic_NCR5380_init_one(&driver_template
, &pdev
->dev
, base
, irq
,
686 static void generic_NCR5380_pnp_remove(struct pnp_dev
*pdev
)
688 generic_NCR5380_release_resources(pnp_get_drvdata(pdev
));
689 pnp_set_drvdata(pdev
, NULL
);
692 static struct pnp_driver generic_NCR5380_pnp_driver
= {
693 .name
= DRV_MODULE_NAME
,
694 .id_table
= generic_NCR5380_pnp_ids
,
695 .probe
= generic_NCR5380_pnp_probe
,
696 .remove
= generic_NCR5380_pnp_remove
,
698 #endif /* defined(CONFIG_PNP) */
700 static int pnp_registered
, isa_registered
;
702 static int __init
generic_NCR5380_init(void)
706 /* compatibility with old-style parameters */
707 if (irq
[0] == -1 && base
[0] == 0 && card
[0] == -1) {
711 card
[0] = BOARD_NCR5380
;
713 card
[0] = BOARD_NCR53C400
;
715 card
[0] = BOARD_NCR53C400A
;
717 card
[0] = BOARD_DTC3181E
;
719 card
[0] = BOARD_HP_C2502
;
723 if (!pnp_register_driver(&generic_NCR5380_pnp_driver
))
726 ret
= isa_register_driver(&generic_NCR5380_isa_driver
, MAX_CARDS
);
730 return (pnp_registered
|| isa_registered
) ? 0 : ret
;
733 static void __exit
generic_NCR5380_exit(void)
737 pnp_unregister_driver(&generic_NCR5380_pnp_driver
);
740 isa_unregister_driver(&generic_NCR5380_isa_driver
);
743 module_init(generic_NCR5380_init
);
744 module_exit(generic_NCR5380_exit
);