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/isapnp.h>
34 #include <linux/interrupt.h>
40 static int ncr_53c400
;
41 static int ncr_53c400a
;
45 static struct override
{
46 NCR5380_map_type NCR5380_map_name
;
49 int board
; /* Use NCR53c400, Ricoh, etc. extensions ? */
51 #ifdef GENERIC_NCR5380_OVERRIDE
52 [] __initdata
= GENERIC_NCR5380_OVERRIDE
;
54 [1] __initdata
= { { 0,},};
57 #define NO_OVERRIDES ARRAY_SIZE(overrides)
62 * internal_setup - handle lilo command string override
63 * @board: BOARD_* identifier for the board
65 * @ints: numeric parameters
67 * Do LILO command line initialization of the overrides array. Display
73 static void __init
internal_setup(int board
, char *str
, int *ints
)
75 static int commandline_current
;
78 if (ints
[0] != 2 && ints
[0] != 3) {
79 printk(KERN_ERR
"generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name
) ",irq,dma\n");
85 printk(KERN_ERR
"generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name
) ",irq\n");
89 case BOARD_NCR53C400A
:
91 printk(KERN_ERR
"generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name
) ",irq\n");
97 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name
) ",irq\n");
103 if (commandline_current
< NO_OVERRIDES
) {
104 overrides
[commandline_current
].NCR5380_map_name
= (NCR5380_map_type
) ints
[1];
105 overrides
[commandline_current
].irq
= ints
[2];
107 overrides
[commandline_current
].dma
= ints
[3];
109 overrides
[commandline_current
].dma
= DMA_NONE
;
110 overrides
[commandline_current
].board
= board
;
111 ++commandline_current
;
117 * do_NCR53C80_setup - set up entry point
120 * Setup function invoked at boot to parse the ncr5380= command
124 static int __init
do_NCR5380_setup(char *str
)
128 get_options(str
, ARRAY_SIZE(ints
), ints
);
129 internal_setup(BOARD_NCR5380
, str
, ints
);
134 * do_NCR53C400_setup - set up entry point
136 * @ints: integer parameters from kernel setup code
138 * Setup function invoked at boot to parse the ncr53c400= command
142 static int __init
do_NCR53C400_setup(char *str
)
146 get_options(str
, ARRAY_SIZE(ints
), ints
);
147 internal_setup(BOARD_NCR53C400
, str
, ints
);
152 * do_NCR53C400A_setup - set up entry point
154 * @ints: integer parameters from kernel setup code
156 * Setup function invoked at boot to parse the ncr53c400a= command
160 static int __init
do_NCR53C400A_setup(char *str
)
164 get_options(str
, ARRAY_SIZE(ints
), ints
);
165 internal_setup(BOARD_NCR53C400A
, str
, ints
);
170 * do_DTC3181E_setup - set up entry point
172 * @ints: integer parameters from kernel setup code
174 * Setup function invoked at boot to parse the dtc3181e= command
178 static int __init
do_DTC3181E_setup(char *str
)
182 get_options(str
, ARRAY_SIZE(ints
), ints
);
183 internal_setup(BOARD_DTC3181E
, str
, ints
);
189 #ifndef SCSI_G_NCR5380_MEM
191 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
192 * to ports 0x779 and 0x379.
194 static void magic_configure(int idx
, u8 irq
, u8 magic
[])
198 outb(magic
[0], 0x779);
199 outb(magic
[1], 0x379);
200 outb(magic
[2], 0x379);
201 outb(magic
[3], 0x379);
202 outb(magic
[4], 0x379);
204 /* allowed IRQs for HP C2502 */
205 if (irq
!= 2 && irq
!= 3 && irq
!= 4 && irq
!= 5 && irq
!= 7)
207 if (idx
>= 0 && idx
<= 7)
208 cfg
= 0x80 | idx
| (irq
<< 4);
214 * generic_NCR5380_detect - look for NCR5380 controllers
215 * @tpnt: the scsi template
217 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
218 * and DTC436(ISAPnP) controllers. If overrides have been set we use
224 static int __init
generic_NCR5380_detect(struct scsi_host_template
*tpnt
)
226 static int current_override
;
230 #ifndef SCSI_G_NCR5380_MEM
233 unsigned long region_size
;
235 static unsigned int __initdata ncr_53c400a_ports
[] = {
236 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
238 static unsigned int __initdata dtc_3181e_ports
[] = {
239 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
241 static u8 ncr_53c400a_magic
[] __initdata
= { /* 53C400A & DTC436 */
242 0x59, 0xb9, 0xc5, 0xae, 0xa6
244 static u8 hp_c2502_magic
[] __initdata
= { /* HP C2502 */
245 0x0f, 0x22, 0xf0, 0x20, 0x80
248 struct Scsi_Host
*instance
;
249 struct NCR5380_hostdata
*hostdata
;
250 #ifdef SCSI_G_NCR5380_MEM
253 resource_size_t iomem_size
;
257 overrides
[0].irq
= ncr_irq
;
259 overrides
[0].dma
= ncr_dma
;
261 overrides
[0].NCR5380_map_name
= (NCR5380_map_type
) ncr_addr
;
263 overrides
[0].board
= BOARD_NCR5380
;
265 overrides
[0].board
= BOARD_NCR53C400
;
266 else if (ncr_53c400a
)
267 overrides
[0].board
= BOARD_NCR53C400A
;
269 overrides
[0].board
= BOARD_DTC3181E
;
271 overrides
[0].board
= BOARD_HP_C2502
;
272 #ifndef SCSI_G_NCR5380_MEM
273 if (!current_override
&& isapnp_present()) {
274 struct pnp_dev
*dev
= NULL
;
276 while ((dev
= pnp_find_dev(NULL
, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev
))) {
277 if (count
>= NO_OVERRIDES
)
279 if (pnp_device_attach(dev
) < 0)
281 if (pnp_activate_dev(dev
) < 0) {
282 printk(KERN_ERR
"dtc436e probe: activate failed\n");
283 pnp_device_detach(dev
);
286 if (!pnp_port_valid(dev
, 0)) {
287 printk(KERN_ERR
"dtc436e probe: no valid port\n");
288 pnp_device_detach(dev
);
291 if (pnp_irq_valid(dev
, 0))
292 overrides
[count
].irq
= pnp_irq(dev
, 0);
294 overrides
[count
].irq
= NO_IRQ
;
295 if (pnp_dma_valid(dev
, 0))
296 overrides
[count
].dma
= pnp_dma(dev
, 0);
298 overrides
[count
].dma
= DMA_NONE
;
299 overrides
[count
].NCR5380_map_name
= (NCR5380_map_type
) pnp_port_start(dev
, 0);
300 overrides
[count
].board
= BOARD_DTC3181E
;
306 for (count
= 0; current_override
< NO_OVERRIDES
; ++current_override
) {
307 if (!(overrides
[current_override
].NCR5380_map_name
))
312 switch (overrides
[current_override
].board
) {
314 flags
= FLAG_NO_PSEUDO_DMA
| FLAG_DMA_FIXUP
;
316 case BOARD_NCR53C400A
:
317 ports
= ncr_53c400a_ports
;
318 magic
= ncr_53c400a_magic
;
321 ports
= ncr_53c400a_ports
;
322 magic
= hp_c2502_magic
;
325 ports
= dtc_3181e_ports
;
326 magic
= ncr_53c400a_magic
;
330 #ifndef SCSI_G_NCR5380_MEM
331 if (ports
&& magic
) {
332 /* wakeup sequence for the NCR53C400A and DTC3181E */
334 /* Disable the adapter and look for a free io port */
335 magic_configure(-1, 0, magic
);
339 if (overrides
[current_override
].NCR5380_map_name
!= PORT_AUTO
)
340 for (i
= 0; ports
[i
]; i
++) {
341 if (!request_region(ports
[i
], region_size
, "ncr53c80"))
343 if (overrides
[current_override
].NCR5380_map_name
== ports
[i
])
345 release_region(ports
[i
], region_size
);
347 for (i
= 0; ports
[i
]; i
++) {
348 if (!request_region(ports
[i
], region_size
, "ncr53c80"))
350 if (inb(ports
[i
]) == 0xff)
352 release_region(ports
[i
], region_size
);
355 /* At this point we have our region reserved */
356 magic_configure(i
, 0, magic
); /* no IRQ yet */
357 outb(0xc0, ports
[i
] + 9);
358 if (inb(ports
[i
] + 9) != 0x80)
360 overrides
[current_override
].NCR5380_map_name
= ports
[i
];
367 /* Not a 53C400A style setup - just grab */
369 if (!request_region(overrides
[current_override
].NCR5380_map_name
,
370 region_size
, "ncr5380"))
374 base
= overrides
[current_override
].NCR5380_map_name
;
375 iomem_size
= NCR53C400_region_size
;
376 if (!request_mem_region(base
, iomem_size
, "ncr5380"))
378 iomem
= ioremap(base
, iomem_size
);
380 release_mem_region(base
, iomem_size
);
384 instance
= scsi_register(tpnt
, sizeof(struct NCR5380_hostdata
));
385 if (instance
== NULL
)
387 hostdata
= shost_priv(instance
);
389 #ifndef SCSI_G_NCR5380_MEM
390 instance
->io_port
= overrides
[current_override
].NCR5380_map_name
;
391 instance
->n_io_port
= region_size
;
392 hostdata
->io_width
= 1; /* 8-bit PDMA by default */
395 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
398 switch (overrides
[current_override
].board
) {
399 case BOARD_NCR53C400
:
400 instance
->io_port
+= 8;
401 hostdata
->c400_ctl_status
= 0;
402 hostdata
->c400_blk_cnt
= 1;
403 hostdata
->c400_host_buf
= 4;
406 hostdata
->io_width
= 2; /* 16-bit PDMA */
408 case BOARD_NCR53C400A
:
410 hostdata
->c400_ctl_status
= 9;
411 hostdata
->c400_blk_cnt
= 10;
412 hostdata
->c400_host_buf
= 8;
416 instance
->base
= overrides
[current_override
].NCR5380_map_name
;
417 hostdata
->iomem
= iomem
;
418 hostdata
->iomem_size
= iomem_size
;
419 switch (overrides
[current_override
].board
) {
420 case BOARD_NCR53C400
:
421 hostdata
->c400_ctl_status
= 0x100;
422 hostdata
->c400_blk_cnt
= 0x101;
423 hostdata
->c400_host_buf
= 0x104;
426 case BOARD_NCR53C400A
:
428 pr_err(DRV_MODULE_NAME
": unknown register offsets\n");
433 if (NCR5380_init(instance
, flags
| FLAG_LATE_DMA_SETUP
))
436 switch (overrides
[current_override
].board
) {
437 case BOARD_NCR53C400
:
439 case BOARD_NCR53C400A
:
441 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
);
444 NCR5380_maybe_reset_bus(instance
);
446 if (overrides
[current_override
].irq
!= IRQ_AUTO
)
447 instance
->irq
= overrides
[current_override
].irq
;
449 instance
->irq
= NCR5380_probe_irq(instance
, 0xffff);
451 /* Compatibility with documented NCR5380 kernel parameters */
452 if (instance
->irq
== 255)
453 instance
->irq
= NO_IRQ
;
455 if (instance
->irq
!= NO_IRQ
) {
456 #ifndef SCSI_G_NCR5380_MEM
457 /* set IRQ for HP C2502 */
458 if (overrides
[current_override
].board
== BOARD_HP_C2502
)
459 magic_configure(port_idx
, instance
->irq
, magic
);
461 if (request_irq(instance
->irq
, generic_NCR5380_intr
,
462 0, "NCR5380", instance
)) {
463 printk(KERN_WARNING
"scsi%d : IRQ%d not free, interrupts disabled\n", instance
->host_no
, instance
->irq
);
464 instance
->irq
= NO_IRQ
;
468 if (instance
->irq
== NO_IRQ
) {
469 printk(KERN_INFO
"scsi%d : interrupts not enabled. for better interactive performance,\n", instance
->host_no
);
470 printk(KERN_INFO
"scsi%d : please jumper the board for a free IRQ.\n", instance
->host_no
);
479 scsi_unregister(instance
);
481 #ifndef SCSI_G_NCR5380_MEM
482 release_region(overrides
[current_override
].NCR5380_map_name
, region_size
);
485 release_mem_region(base
, iomem_size
);
491 * generic_NCR5380_release_resources - free resources
492 * @instance: host adapter to clean up
494 * Free the generic interface resources from this adapter.
499 static int generic_NCR5380_release_resources(struct Scsi_Host
*instance
)
501 if (instance
->irq
!= NO_IRQ
)
502 free_irq(instance
->irq
, instance
);
503 NCR5380_exit(instance
);
504 #ifndef SCSI_G_NCR5380_MEM
505 release_region(instance
->io_port
, instance
->n_io_port
);
508 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
510 iounmap(hostdata
->iomem
);
511 release_mem_region(instance
->base
, hostdata
->iomem_size
);
518 * generic_NCR5380_pread - pseudo DMA read
519 * @instance: adapter to read from
520 * @dst: buffer to read into
521 * @len: buffer length
523 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
527 static inline int generic_NCR5380_pread(struct Scsi_Host
*instance
,
528 unsigned char *dst
, int len
)
530 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
531 int blocks
= len
/ 128;
534 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
| CSR_TRANS_DIR
);
535 NCR5380_write(hostdata
->c400_blk_cnt
, blocks
);
537 if (NCR5380_read(hostdata
->c400_blk_cnt
) == 0)
539 if (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
) {
540 printk(KERN_ERR
"53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start
, blocks
);
543 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
544 ; /* FIXME - no timeout */
546 #ifndef SCSI_G_NCR5380_MEM
547 if (hostdata
->io_width
== 2)
548 insw(instance
->io_port
+ hostdata
->c400_host_buf
,
551 insb(instance
->io_port
+ hostdata
->c400_host_buf
,
554 /* implies SCSI_G_NCR5380_MEM */
555 memcpy_fromio(dst
+ start
,
556 hostdata
->iomem
+ NCR53C400_host_buffer
, 128);
563 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
564 ; /* FIXME - no timeout */
566 #ifndef SCSI_G_NCR5380_MEM
567 if (hostdata
->io_width
== 2)
568 insw(instance
->io_port
+ hostdata
->c400_host_buf
,
571 insb(instance
->io_port
+ hostdata
->c400_host_buf
,
574 /* implies SCSI_G_NCR5380_MEM */
575 memcpy_fromio(dst
+ start
,
576 hostdata
->iomem
+ NCR53C400_host_buffer
, 128);
582 if (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
))
583 printk("53C400r: no 53C80 gated irq after transfer");
585 /* wait for 53C80 registers to be available */
586 while (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_53C80_REG
))
589 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_END_DMA_TRANSFER
))
590 printk(KERN_ERR
"53C400r: no end dma signal\n");
596 * generic_NCR5380_pwrite - pseudo DMA write
597 * @instance: adapter to read from
598 * @dst: buffer to read into
599 * @len: buffer length
601 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
605 static inline int generic_NCR5380_pwrite(struct Scsi_Host
*instance
,
606 unsigned char *src
, int len
)
608 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
609 int blocks
= len
/ 128;
612 NCR5380_write(hostdata
->c400_ctl_status
, CSR_BASE
);
613 NCR5380_write(hostdata
->c400_blk_cnt
, blocks
);
615 if (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_GATED_53C80_IRQ
) {
616 printk(KERN_ERR
"53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start
, blocks
);
620 if (NCR5380_read(hostdata
->c400_blk_cnt
) == 0)
622 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
624 #ifndef SCSI_G_NCR5380_MEM
625 if (hostdata
->io_width
== 2)
626 outsw(instance
->io_port
+ hostdata
->c400_host_buf
,
629 outsb(instance
->io_port
+ hostdata
->c400_host_buf
,
632 /* implies SCSI_G_NCR5380_MEM */
633 memcpy_toio(hostdata
->iomem
+ NCR53C400_host_buffer
,
640 while (NCR5380_read(hostdata
->c400_ctl_status
) & CSR_HOST_BUF_NOT_RDY
)
641 ; // FIXME - no timeout
643 #ifndef SCSI_G_NCR5380_MEM
644 if (hostdata
->io_width
== 2)
645 outsw(instance
->io_port
+ hostdata
->c400_host_buf
,
648 outsb(instance
->io_port
+ hostdata
->c400_host_buf
,
651 /* implies SCSI_G_NCR5380_MEM */
652 memcpy_toio(hostdata
->iomem
+ NCR53C400_host_buffer
,
659 /* wait for 53C80 registers to be available */
660 while (!(NCR5380_read(hostdata
->c400_ctl_status
) & CSR_53C80_REG
)) {
661 udelay(4); /* DTC436 chip hangs without this */
662 /* FIXME - no timeout */
665 if (!(NCR5380_read(BUS_AND_STATUS_REG
) & BASR_END_DMA_TRANSFER
)) {
666 printk(KERN_ERR
"53C400w: no end dma signal\n");
669 while (!(NCR5380_read(TARGET_COMMAND_REG
) & TCR_LAST_BYTE_SENT
))
674 static int generic_NCR5380_dma_xfer_len(struct Scsi_Host
*instance
,
675 struct scsi_cmnd
*cmd
)
677 struct NCR5380_hostdata
*hostdata
= shost_priv(instance
);
678 int transfersize
= cmd
->transfersize
;
680 if (hostdata
->flags
& FLAG_NO_PSEUDO_DMA
)
683 /* Limit transfers to 32K, for xx400 & xx406
684 * pseudoDMA that transfers in 128 bytes blocks.
686 if (transfersize
> 32 * 1024 && cmd
->SCp
.this_residual
&&
687 !(cmd
->SCp
.this_residual
% transfersize
))
688 transfersize
= 32 * 1024;
690 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
691 if (transfersize
% 128)
698 * Include the NCR5380 core code that we build our driver around
703 static struct scsi_host_template driver_template
= {
704 .proc_name
= DRV_MODULE_NAME
,
705 .name
= "Generic NCR5380/NCR53C400 SCSI",
706 .detect
= generic_NCR5380_detect
,
707 .release
= generic_NCR5380_release_resources
,
708 .info
= generic_NCR5380_info
,
709 .queuecommand
= generic_NCR5380_queue_command
,
710 .eh_abort_handler
= generic_NCR5380_abort
,
711 .eh_bus_reset_handler
= generic_NCR5380_bus_reset
,
714 .sg_tablesize
= SG_ALL
,
716 .use_clustering
= DISABLE_CLUSTERING
,
717 .cmd_size
= NCR5380_CMD_SIZE
,
721 #include "scsi_module.c"
723 module_param(ncr_irq
, int, 0);
724 module_param(ncr_dma
, int, 0);
725 module_param(ncr_addr
, int, 0);
726 module_param(ncr_5380
, int, 0);
727 module_param(ncr_53c400
, int, 0);
728 module_param(ncr_53c400a
, int, 0);
729 module_param(dtc_3181e
, int, 0);
730 module_param(hp_c2502
, int, 0);
731 MODULE_LICENSE("GPL");
733 #if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
734 static struct isapnp_device_id id_table
[] = {
736 ISAPNP_ANY_ID
, ISAPNP_ANY_ID
,
737 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
742 MODULE_DEVICE_TABLE(isapnp
, id_table
);
745 __setup("ncr5380=", do_NCR5380_setup
);
746 __setup("ncr53c400=", do_NCR53C400_setup
);
747 __setup("ncr53c400a=", do_NCR53C400A_setup
);
748 __setup("dtc3181e=", do_DTC3181E_setup
);