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
24 * TODO : flesh out DMA support, find some one actually using this (I have
25 * a memory mapped Trantor board that works fine)
29 * The card is detected and initialized in one of several ways :
30 * 1. With command line overrides - NCR5380=port,irq may be
31 * used on the LILO command line to override the defaults.
33 * 2. With the GENERIC_NCR5380_OVERRIDE compile time define. This is
34 * specified as an array of address, irq, dma, board tuples. Ie, for
35 * one board at 0x350, IRQ5, no dma, I could say
36 * -DGENERIC_NCR5380_OVERRIDE={{0xcc000, 5, DMA_NONE, BOARD_NCR5380}}
38 * -1 should be specified for no or DMA interrupt, -2 to autoprobe for an
39 * IRQ line if overridden on the command line.
41 * 3. When included as a module, with arguments passed on the command line:
42 * ncr_irq=xx the interrupt
43 * ncr_addr=xx the port or base address (for port or memory
46 * ncr_5380=1 to set up for a NCR5380 board
47 * ncr_53c400=1 to set up for a NCR53C400 board
49 * modprobe g_NCR5380 ncr_irq=5 ncr_addr=0x350 ncr_5380=1
50 * for a port mapped NCR5380 board or
51 * modprobe g_NCR5380 ncr_irq=255 ncr_addr=0xc8000 ncr_53c400=1
52 * for a memory mapped NCR53C400 board with interrupts disabled.
54 * 255 should be specified for no or DMA interrupt, 254 to autoprobe for an
55 * IRQ line if overridden on the command line.
61 #ifdef CONFIG_SCSI_GENERIC_NCR53C400
66 #include <linux/blkdev.h>
67 #include <linux/module.h>
68 #include <scsi/scsi_host.h>
69 #include "g_NCR5380.h"
71 #include <linux/init.h>
72 #include <linux/ioport.h>
73 #include <linux/isapnp.h>
74 #include <linux/interrupt.h>
80 static int ncr_53c400;
81 static int ncr_53c400a;
84 static struct override {
85 NCR5380_map_type NCR5380_map_name;
88 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
90 #ifdef GENERIC_NCR5380_OVERRIDE
91 [] __initdata = GENERIC_NCR5380_OVERRIDE;
93 [1] __initdata = { { 0,},};
96 #define NO_OVERRIDES ARRAY_SIZE(overrides)
101 * internal_setup - handle lilo command string override
102 * @board: BOARD_* identifier for the board
104 * @ints: numeric parameters
106 * Do LILO command line initialization of the overrides array. Display
112 static void __init internal_setup(int board, char *str, int *ints)
114 static int commandline_current;
117 if (ints[0] != 2 && ints[0] != 3) {
118 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
122 case BOARD_NCR53C400:
124 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
128 case BOARD_NCR53C400A:
130 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
136 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
142 if (commandline_current < NO_OVERRIDES) {
143 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
144 overrides[commandline_current].irq = ints[2];
146 overrides[commandline_current].dma = ints[3];
148 overrides[commandline_current].dma = DMA_NONE;
149 overrides[commandline_current].board = board;
150 ++commandline_current;
156 * do_NCR53C80_setup - set up entry point
159 * Setup function invoked at boot to parse the ncr5380= command
163 static int __init do_NCR5380_setup(char *str)
167 get_options(str, ARRAY_SIZE(ints), ints);
168 internal_setup(BOARD_NCR5380, str, ints);
173 * do_NCR53C400_setup - set up entry point
175 * @ints: integer parameters from kernel setup code
177 * Setup function invoked at boot to parse the ncr53c400= command
181 static int __init do_NCR53C400_setup(char *str)
185 get_options(str, ARRAY_SIZE(ints), ints);
186 internal_setup(BOARD_NCR53C400, str, ints);
191 * do_NCR53C400A_setup - set up entry point
193 * @ints: integer parameters from kernel setup code
195 * Setup function invoked at boot to parse the ncr53c400a= command
199 static int __init do_NCR53C400A_setup(char *str)
203 get_options(str, ARRAY_SIZE(ints), ints);
204 internal_setup(BOARD_NCR53C400A, str, ints);
209 * do_DTC3181E_setup - set up entry point
211 * @ints: integer parameters from kernel setup code
213 * Setup function invoked at boot to parse the dtc3181e= command
217 static int __init do_DTC3181E_setup(char *str)
221 get_options(str, ARRAY_SIZE(ints), ints);
222 internal_setup(BOARD_DTC3181E, str, ints);
229 * generic_NCR5380_detect - look for NCR5380 controllers
230 * @tpnt: the scsi template
232 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
233 * and DTC436(ISAPnP) controllers. If overrides have been set we use
239 static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
241 static int current_override;
244 #ifndef SCSI_G_NCR5380_MEM
246 unsigned long region_size = 16;
248 static unsigned int __initdata ncr_53c400a_ports[] = {
249 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
251 static unsigned int __initdata dtc_3181e_ports[] = {
252 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
255 struct Scsi_Host *instance;
256 struct NCR5380_hostdata *hostdata;
257 #ifdef SCSI_G_NCR5380_MEM
263 overrides[0].irq = ncr_irq;
265 overrides[0].dma = ncr_dma;
267 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
269 overrides[0].board = BOARD_NCR5380;
271 overrides[0].board = BOARD_NCR53C400;
272 else if (ncr_53c400a)
273 overrides[0].board = BOARD_NCR53C400A;
275 overrides[0].board = BOARD_DTC3181E;
276 #ifndef SCSI_G_NCR5380_MEM
277 if (!current_override && isapnp_present()) {
278 struct pnp_dev *dev = NULL;
280 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
281 if (count >= NO_OVERRIDES)
283 if (pnp_device_attach(dev) < 0)
285 if (pnp_activate_dev(dev) < 0) {
286 printk(KERN_ERR "dtc436e probe: activate failed\n");
287 pnp_device_detach(dev);
290 if (!pnp_port_valid(dev, 0)) {
291 printk(KERN_ERR "dtc436e probe: no valid port\n");
292 pnp_device_detach(dev);
295 if (pnp_irq_valid(dev, 0))
296 overrides[count].irq = pnp_irq(dev, 0);
298 overrides[count].irq = NO_IRQ;
299 if (pnp_dma_valid(dev, 0))
300 overrides[count].dma = pnp_dma(dev, 0);
302 overrides[count].dma = DMA_NONE;
303 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
304 overrides[count].board = BOARD_DTC3181E;
310 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
311 if (!(overrides[current_override].NCR5380_map_name))
316 switch (overrides[current_override].board) {
318 flags = FLAG_NO_PSEUDO_DMA;
320 case BOARD_NCR53C400:
322 flags = FLAG_NO_DMA_FIXUP;
325 case BOARD_NCR53C400A:
326 flags = FLAG_NO_DMA_FIXUP;
327 ports = ncr_53c400a_ports;
330 flags = FLAG_NO_DMA_FIXUP;
331 ports = dtc_3181e_ports;
335 #ifndef SCSI_G_NCR5380_MEM
337 /* wakeup sequence for the NCR53C400A and DTC3181E */
339 /* Disable the adapter and look for a free io port */
347 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
348 for (i = 0; ports[i]; i++) {
349 if (!request_region(ports[i], 16, "ncr53c80"))
351 if (overrides[current_override].NCR5380_map_name == ports[i])
353 release_region(ports[i], 16);
355 for (i = 0; ports[i]; i++) {
356 if (!request_region(ports[i], 16, "ncr53c80"))
358 if (inb(ports[i]) == 0xff)
360 release_region(ports[i], 16);
363 /* At this point we have our region reserved */
369 outb(0x80 | i, 0x379); /* set io port to be used */
370 outb(0xc0, ports[i] + 9);
371 if (inb(ports[i] + 9) != 0x80)
374 overrides[current_override].NCR5380_map_name = ports[i];
380 /* Not a 53C400A style setup - just grab */
381 if(!(request_region(overrides[current_override].NCR5380_map_name, NCR5380_region_size, "ncr5380")))
383 region_size = NCR5380_region_size;
386 base = overrides[current_override].NCR5380_map_name;
387 if (!request_mem_region(base, NCR5380_region_size, "ncr5380"))
389 iomem = ioremap(base, NCR5380_region_size);
391 release_mem_region(base, NCR5380_region_size);
395 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
396 if (instance == NULL)
398 hostdata = shost_priv(instance);
400 #ifndef SCSI_G_NCR5380_MEM
401 instance->io_port = overrides[current_override].NCR5380_map_name;
402 instance->n_io_port = region_size;
403 hostdata->io_width = 1; /* 8-bit PDMA by default */
406 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
409 switch (overrides[current_override].board) {
410 case BOARD_NCR53C400:
411 instance->io_port += 8;
412 hostdata->c400_ctl_status = 0;
413 hostdata->c400_blk_cnt = 1;
414 hostdata->c400_host_buf = 4;
417 hostdata->io_width = 2; /* 16-bit PDMA */
419 case BOARD_NCR53C400A:
420 hostdata->c400_ctl_status = 9;
421 hostdata->c400_blk_cnt = 10;
422 hostdata->c400_host_buf = 8;
426 instance->base = overrides[current_override].NCR5380_map_name;
427 hostdata->iomem = iomem;
428 switch (overrides[current_override].board) {
429 case BOARD_NCR53C400:
430 hostdata->c400_ctl_status = 0x100;
431 hostdata->c400_blk_cnt = 0x101;
432 hostdata->c400_host_buf = 0x104;
435 case BOARD_NCR53C400A:
436 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
441 if (NCR5380_init(instance, flags))
444 switch (overrides[current_override].board) {
445 case BOARD_NCR53C400:
447 case BOARD_NCR53C400A:
448 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
451 NCR5380_maybe_reset_bus(instance);
453 if (overrides[current_override].irq != IRQ_AUTO)
454 instance->irq = overrides[current_override].irq;
456 instance->irq = NCR5380_probe_irq(instance, 0xffff);
458 /* Compatibility with documented NCR5380 kernel parameters */
459 if (instance->irq == 255)
460 instance->irq = NO_IRQ;
462 if (instance->irq != NO_IRQ)
463 if (request_irq(instance->irq, generic_NCR5380_intr,
464 0, "NCR5380", instance)) {
465 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
466 instance->irq = NO_IRQ;
469 if (instance->irq == NO_IRQ) {
470 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
471 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
480 scsi_unregister(instance);
482 #ifndef SCSI_G_NCR5380_MEM
483 release_region(overrides[current_override].NCR5380_map_name, region_size);
486 release_mem_region(base, NCR5380_region_size);
492 * generic_NCR5380_release_resources - free resources
493 * @instance: host adapter to clean up
495 * Free the generic interface resources from this adapter.
500 static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
502 if (instance->irq != NO_IRQ)
503 free_irq(instance->irq, instance);
504 NCR5380_exit(instance);
505 #ifndef SCSI_G_NCR5380_MEM
506 release_region(instance->io_port, instance->n_io_port);
508 iounmap(((struct NCR5380_hostdata *)instance->hostdata)->iomem);
509 release_mem_region(instance->base, NCR5380_region_size);
516 * generic_NCR5380_biosparam
517 * @disk: disk to compute geometry for
518 * @dev: device identifier for this disk
519 * @ip: sizes to fill in
521 * Generates a BIOS / DOS compatible H-C-S mapping for the specified
524 * XXX Most SCSI boards use this mapping, I could be incorrect. Someone
525 * using hard disks on a trantor should verify that this mapping
526 * corresponds to that used by the BIOS / ASPI driver by running the linux
527 * fdisk program and matching the H_C_S coordinates to what DOS uses.
533 generic_NCR5380_biosparam(struct scsi_device *sdev, struct block_device *bdev,
534 sector_t capacity, int *ip)
538 ip[2] = capacity >> 11;
546 * NCR5380_pread - pseudo DMA read
547 * @instance: adapter to read from
548 * @dst: buffer to read into
549 * @len: buffer length
551 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
555 static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
557 struct NCR5380_hostdata *hostdata = shost_priv(instance);
558 int blocks = len / 128;
561 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
562 NCR5380_write(hostdata->c400_blk_cnt, blocks);
564 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
566 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
567 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
570 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
571 ; /* FIXME - no timeout */
573 #ifndef SCSI_G_NCR5380_MEM
574 if (hostdata->io_width == 2)
575 insw(instance->io_port + hostdata->c400_host_buf,
578 insb(instance->io_port + hostdata->c400_host_buf,
581 /* implies SCSI_G_NCR5380_MEM */
582 memcpy_fromio(dst + start,
583 hostdata->iomem + NCR53C400_host_buffer, 128);
590 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
591 ; /* FIXME - no timeout */
593 #ifndef SCSI_G_NCR5380_MEM
594 if (hostdata->io_width == 2)
595 insw(instance->io_port + hostdata->c400_host_buf,
598 insb(instance->io_port + hostdata->c400_host_buf,
601 /* implies SCSI_G_NCR5380_MEM */
602 memcpy_fromio(dst + start,
603 hostdata->iomem + NCR53C400_host_buffer, 128);
609 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
610 printk("53C400r: no 53C80 gated irq after transfer");
614 * DON'T DO THIS - THEY NEVER ARRIVE!
616 printk("53C400r: Waiting for 53C80 registers\n");
617 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)
620 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
621 printk(KERN_ERR "53C400r: no end dma signal\n");
627 * NCR5380_write - pseudo DMA write
628 * @instance: adapter to read from
629 * @dst: buffer to read into
630 * @len: buffer length
632 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
636 static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
638 struct NCR5380_hostdata *hostdata = shost_priv(instance);
639 int blocks = len / 128;
643 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
644 NCR5380_write(hostdata->c400_blk_cnt, blocks);
646 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
647 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
651 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
653 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
655 #ifndef SCSI_G_NCR5380_MEM
656 if (hostdata->io_width == 2)
657 outsw(instance->io_port + hostdata->c400_host_buf,
660 outsb(instance->io_port + hostdata->c400_host_buf,
663 /* implies SCSI_G_NCR5380_MEM */
664 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
671 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
672 ; // FIXME - no timeout
674 #ifndef SCSI_G_NCR5380_MEM
675 if (hostdata->io_width == 2)
676 outsw(instance->io_port + hostdata->c400_host_buf,
679 outsb(instance->io_port + hostdata->c400_host_buf,
682 /* implies SCSI_G_NCR5380_MEM */
683 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
691 printk("53C400w: waiting for registers to be available\n");
692 THEY NEVER DO ! while (NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG);
693 printk("53C400w: Got em\n");
696 /* Let's wait for this instead - could be ugly */
697 /* All documentation says to check for this. Maybe my hardware is too
698 * fast. Waiting for it seems to work fine! KLL
700 while (!(i = NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ)) {
701 udelay(4); /* DTC436 chip hangs without this */
702 /* FIXME - no timeout */
706 * I know. i is certainly != 0 here but the loop is new. See previous
710 if (!((i = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_END_DMA_TRANSFER))
711 printk(KERN_ERR "53C400w: No END OF DMA bit - WHOOPS! BASR=%0x\n", i);
713 printk(KERN_ERR "53C400w: no 53C80 gated irq after transfer (last block)\n");
716 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
717 printk(KERN_ERR "53C400w: no end dma signal\n");
720 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
725 static int generic_NCR5380_dma_xfer_len(struct scsi_cmnd *cmd)
727 int transfersize = cmd->transfersize;
729 /* Limit transfers to 32K, for xx400 & xx406
730 * pseudoDMA that transfers in 128 bytes blocks.
732 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
733 !(cmd->SCp.this_residual % transfersize))
734 transfersize = 32 * 1024;
736 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
737 if (transfersize % 128)
743 #endif /* PSEUDO_DMA */
746 * Include the NCR5380 core code that we build our driver around
751 static struct scsi_host_template driver_template = {
752 .proc_name = DRV_MODULE_NAME,
753 .name = "Generic NCR5380/NCR53C400 SCSI",
754 .detect = generic_NCR5380_detect,
755 .release = generic_NCR5380_release_resources,
756 .info = generic_NCR5380_info,
757 .queuecommand = generic_NCR5380_queue_command,
758 .eh_abort_handler = generic_NCR5380_abort,
759 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
760 .bios_param = NCR5380_BIOSPARAM,
763 .sg_tablesize = SG_ALL,
765 .use_clustering = DISABLE_CLUSTERING,
766 .cmd_size = NCR5380_CMD_SIZE,
770 #include "scsi_module.c"
772 module_param(ncr_irq, int, 0);
773 module_param(ncr_dma, int, 0);
774 module_param(ncr_addr, int, 0);
775 module_param(ncr_5380, int, 0);
776 module_param(ncr_53c400, int, 0);
777 module_param(ncr_53c400a, int, 0);
778 module_param(dtc_3181e, int, 0);
779 MODULE_LICENSE("GPL");
781 #if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
782 static struct isapnp_device_id id_table[] = {
784 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
785 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
790 MODULE_DEVICE_TABLE(isapnp, id_table);
793 __setup("ncr5380=", do_NCR5380_setup);
794 __setup("ncr53c400=", do_NCR53C400_setup);
795 __setup("ncr53c400a=", do_NCR53C400A_setup);
796 __setup("dtc3181e=", do_DTC3181E_setup);