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.
59 /* settings for DTC3181E card with only Mustek scanner attached */
60 #define USLEEP_POLL msecs_to_jiffies(10)
61 #define USLEEP_SLEEP msecs_to_jiffies(200)
65 #ifdef CONFIG_SCSI_GENERIC_NCR53C400
70 #include <linux/signal.h>
71 #include <linux/blkdev.h>
72 #include <scsi/scsi_host.h>
73 #include "g_NCR5380.h"
75 #include <linux/stat.h>
76 #include <linux/init.h>
77 #include <linux/ioport.h>
78 #include <linux/isapnp.h>
79 #include <linux/delay.h>
80 #include <linux/interrupt.h>
86 static int ncr_53c400;
87 static int ncr_53c400a;
90 static struct override {
91 NCR5380_map_type NCR5380_map_name;
94 int board; /* Use NCR53c400, Ricoh, etc. extensions ? */
96 #ifdef GENERIC_NCR5380_OVERRIDE
97 [] __initdata = GENERIC_NCR5380_OVERRIDE;
99 [1] __initdata = { { 0,},};
102 #define NO_OVERRIDES ARRAY_SIZE(overrides)
107 * internal_setup - handle lilo command string override
108 * @board: BOARD_* identifier for the board
110 * @ints: numeric parameters
112 * Do LILO command line initialization of the overrides array. Display
118 static void __init internal_setup(int board, char *str, int *ints)
120 static int commandline_current;
123 if (ints[0] != 2 && ints[0] != 3) {
124 printk(KERN_ERR "generic_NCR5380_setup : usage ncr5380=" STRVAL(NCR5380_map_name) ",irq,dma\n");
128 case BOARD_NCR53C400:
130 printk(KERN_ERR "generic_NCR53C400_setup : usage ncr53c400=" STRVAL(NCR5380_map_name) ",irq\n");
134 case BOARD_NCR53C400A:
136 printk(KERN_ERR "generic_NCR53C400A_setup : usage ncr53c400a=" STRVAL(NCR5380_map_name) ",irq\n");
142 printk("generic_DTC3181E_setup : usage dtc3181e=" STRVAL(NCR5380_map_name) ",irq\n");
148 if (commandline_current < NO_OVERRIDES) {
149 overrides[commandline_current].NCR5380_map_name = (NCR5380_map_type) ints[1];
150 overrides[commandline_current].irq = ints[2];
152 overrides[commandline_current].dma = ints[3];
154 overrides[commandline_current].dma = DMA_NONE;
155 overrides[commandline_current].board = board;
156 ++commandline_current;
162 * do_NCR53C80_setup - set up entry point
165 * Setup function invoked at boot to parse the ncr5380= command
169 static int __init do_NCR5380_setup(char *str)
173 get_options(str, ARRAY_SIZE(ints), ints);
174 internal_setup(BOARD_NCR5380, str, ints);
179 * do_NCR53C400_setup - set up entry point
181 * @ints: integer parameters from kernel setup code
183 * Setup function invoked at boot to parse the ncr53c400= command
187 static int __init do_NCR53C400_setup(char *str)
191 get_options(str, ARRAY_SIZE(ints), ints);
192 internal_setup(BOARD_NCR53C400, str, ints);
197 * do_NCR53C400A_setup - set up entry point
199 * @ints: integer parameters from kernel setup code
201 * Setup function invoked at boot to parse the ncr53c400a= command
205 static int __init do_NCR53C400A_setup(char *str)
209 get_options(str, ARRAY_SIZE(ints), ints);
210 internal_setup(BOARD_NCR53C400A, str, ints);
215 * do_DTC3181E_setup - set up entry point
217 * @ints: integer parameters from kernel setup code
219 * Setup function invoked at boot to parse the dtc3181e= command
223 static int __init do_DTC3181E_setup(char *str)
227 get_options(str, ARRAY_SIZE(ints), ints);
228 internal_setup(BOARD_DTC3181E, str, ints);
235 * generic_NCR5380_detect - look for NCR5380 controllers
236 * @tpnt: the scsi template
238 * Scan for the present of NCR5380, NCR53C400, NCR53C400A, DTC3181E
239 * and DTC436(ISAPnP) controllers. If overrides have been set we use
245 static int __init generic_NCR5380_detect(struct scsi_host_template *tpnt)
247 static int current_override;
250 #ifndef SCSI_G_NCR5380_MEM
252 unsigned long region_size = 16;
254 static unsigned int __initdata ncr_53c400a_ports[] = {
255 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
257 static unsigned int __initdata dtc_3181e_ports[] = {
258 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
261 struct Scsi_Host *instance;
262 #ifdef SCSI_G_NCR5380_MEM
268 overrides[0].irq = ncr_irq;
270 overrides[0].dma = ncr_dma;
272 overrides[0].NCR5380_map_name = (NCR5380_map_type) ncr_addr;
274 overrides[0].board = BOARD_NCR5380;
276 overrides[0].board = BOARD_NCR53C400;
277 else if (ncr_53c400a)
278 overrides[0].board = BOARD_NCR53C400A;
280 overrides[0].board = BOARD_DTC3181E;
281 #ifndef SCSI_G_NCR5380_MEM
282 if (!current_override && isapnp_present()) {
283 struct pnp_dev *dev = NULL;
285 while ((dev = pnp_find_dev(NULL, ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e), dev))) {
286 if (count >= NO_OVERRIDES)
288 if (pnp_device_attach(dev) < 0)
290 if (pnp_activate_dev(dev) < 0) {
291 printk(KERN_ERR "dtc436e probe: activate failed\n");
292 pnp_device_detach(dev);
295 if (!pnp_port_valid(dev, 0)) {
296 printk(KERN_ERR "dtc436e probe: no valid port\n");
297 pnp_device_detach(dev);
300 if (pnp_irq_valid(dev, 0))
301 overrides[count].irq = pnp_irq(dev, 0);
303 overrides[count].irq = NO_IRQ;
304 if (pnp_dma_valid(dev, 0))
305 overrides[count].dma = pnp_dma(dev, 0);
307 overrides[count].dma = DMA_NONE;
308 overrides[count].NCR5380_map_name = (NCR5380_map_type) pnp_port_start(dev, 0);
309 overrides[count].board = BOARD_DTC3181E;
314 tpnt->proc_name = "g_NCR5380";
316 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
317 if (!(overrides[current_override].NCR5380_map_name))
322 switch (overrides[current_override].board) {
324 flags = FLAG_NO_PSEUDO_DMA;
326 case BOARD_NCR53C400:
328 flags = FLAG_NCR53C400;
331 case BOARD_NCR53C400A:
332 flags = FLAG_NO_PSEUDO_DMA;
333 ports = ncr_53c400a_ports;
336 flags = FLAG_NO_PSEUDO_DMA | FLAG_DTC3181E;
337 ports = dtc_3181e_ports;
341 #ifndef SCSI_G_NCR5380_MEM
343 /* wakeup sequence for the NCR53C400A and DTC3181E */
345 /* Disable the adapter and look for a free io port */
353 if (overrides[current_override].NCR5380_map_name != PORT_AUTO)
354 for (i = 0; ports[i]; i++) {
355 if (!request_region(ports[i], 16, "ncr53c80"))
357 if (overrides[current_override].NCR5380_map_name == ports[i])
359 release_region(ports[i], 16);
361 for (i = 0; ports[i]; i++) {
362 if (!request_region(ports[i], 16, "ncr53c80"))
364 if (inb(ports[i]) == 0xff)
366 release_region(ports[i], 16);
369 /* At this point we have our region reserved */
375 outb(0x80 | i, 0x379); /* set io port to be used */
376 outb(0xc0, ports[i] + 9);
377 if (inb(ports[i] + 9) != 0x80)
380 overrides[current_override].NCR5380_map_name = ports[i];
386 /* Not a 53C400A style setup - just grab */
387 if(!(request_region(overrides[current_override].NCR5380_map_name, NCR5380_region_size, "ncr5380")))
389 region_size = NCR5380_region_size;
392 base = overrides[current_override].NCR5380_map_name;
393 if (!request_mem_region(base, NCR5380_region_size, "ncr5380"))
395 iomem = ioremap(base, NCR5380_region_size);
397 release_mem_region(base, NCR5380_region_size);
401 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
402 if (instance == NULL)
405 #ifndef SCSI_G_NCR5380_MEM
406 instance->io_port = overrides[current_override].NCR5380_map_name;
407 instance->n_io_port = region_size;
410 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
413 if (overrides[current_override].board == BOARD_NCR53C400)
414 instance->io_port += 8;
416 instance->base = overrides[current_override].NCR5380_map_name;
417 ((struct NCR5380_hostdata *)instance->hostdata)->iomem = iomem;
420 if (NCR5380_init(instance, flags))
423 if (overrides[current_override].board == BOARD_NCR53C400)
424 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
426 NCR5380_maybe_reset_bus(instance);
428 if (overrides[current_override].irq != IRQ_AUTO)
429 instance->irq = overrides[current_override].irq;
431 instance->irq = NCR5380_probe_irq(instance, 0xffff);
433 /* Compatibility with documented NCR5380 kernel parameters */
434 if (instance->irq == 255)
435 instance->irq = NO_IRQ;
437 if (instance->irq != NO_IRQ)
438 if (request_irq(instance->irq, generic_NCR5380_intr,
439 0, "NCR5380", instance)) {
440 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
441 instance->irq = NO_IRQ;
444 if (instance->irq == NO_IRQ) {
445 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
446 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
455 scsi_unregister(instance);
457 #ifndef SCSI_G_NCR5380_MEM
458 release_region(overrides[current_override].NCR5380_map_name, region_size);
461 release_mem_region(base, NCR5380_region_size);
467 * generic_NCR5380_release_resources - free resources
468 * @instance: host adapter to clean up
470 * Free the generic interface resources from this adapter.
475 static int generic_NCR5380_release_resources(struct Scsi_Host *instance)
477 if (instance->irq != NO_IRQ)
478 free_irq(instance->irq, instance);
479 NCR5380_exit(instance);
480 #ifndef SCSI_G_NCR5380_MEM
481 release_region(instance->io_port, instance->n_io_port);
483 iounmap(((struct NCR5380_hostdata *)instance->hostdata)->iomem);
484 release_mem_region(instance->base, NCR5380_region_size);
491 * generic_NCR5380_biosparam
492 * @disk: disk to compute geometry for
493 * @dev: device identifier for this disk
494 * @ip: sizes to fill in
496 * Generates a BIOS / DOS compatible H-C-S mapping for the specified
499 * XXX Most SCSI boards use this mapping, I could be incorrect. Someone
500 * using hard disks on a trantor should verify that this mapping
501 * corresponds to that used by the BIOS / ASPI driver by running the linux
502 * fdisk program and matching the H_C_S coordinates to what DOS uses.
508 generic_NCR5380_biosparam(struct scsi_device *sdev, struct block_device *bdev,
509 sector_t capacity, int *ip)
513 ip[2] = capacity >> 11;
521 * NCR5380_pread - pseudo DMA read
522 * @instance: adapter to read from
523 * @dst: buffer to read into
524 * @len: buffer length
526 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
530 static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
532 #ifdef SCSI_G_NCR5380_MEM
533 struct NCR5380_hostdata *hostdata = shost_priv(instance);
535 int blocks = len / 128;
539 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE | CSR_TRANS_DIR);
540 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
542 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
545 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
546 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
549 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY);
551 #ifndef SCSI_G_NCR5380_MEM
554 for (i = 0; i < 128; i++)
555 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
558 /* implies SCSI_G_NCR5380_MEM */
559 memcpy_fromio(dst + start,
560 hostdata->iomem + NCR53C400_host_buffer, 128);
567 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
569 // FIXME - no timeout
572 #ifndef SCSI_G_NCR5380_MEM
575 for (i = 0; i < 128; i++)
576 dst[start + i] = NCR5380_read(C400_HOST_BUFFER);
579 /* implies SCSI_G_NCR5380_MEM */
580 memcpy_fromio(dst + start,
581 hostdata->iomem + NCR53C400_host_buffer, 128);
587 if (!(NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
588 printk("53C400r: no 53C80 gated irq after transfer");
592 * DON'T DO THIS - THEY NEVER ARRIVE!
594 printk("53C400r: Waiting for 53C80 registers\n");
595 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG)
598 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
599 printk(KERN_ERR "53C400r: no end dma signal\n");
601 NCR5380_write(MODE_REG, MR_BASE);
602 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
607 * NCR5380_write - pseudo DMA write
608 * @instance: adapter to read from
609 * @dst: buffer to read into
610 * @len: buffer length
612 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
616 static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
618 #ifdef SCSI_G_NCR5380_MEM
619 struct NCR5380_hostdata *hostdata = shost_priv(instance);
621 int blocks = len / 128;
626 NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
627 NCR5380_write(C400_BLOCK_COUNTER_REG, blocks);
629 if (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ) {
630 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
634 if ((bl = NCR5380_read(C400_BLOCK_COUNTER_REG)) == 0) {
637 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
639 #ifndef SCSI_G_NCR5380_MEM
641 for (i = 0; i < 128; i++)
642 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
645 /* implies SCSI_G_NCR5380_MEM */
646 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
653 while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_HOST_BUF_NOT_RDY)
654 ; // FIXME - no timeout
656 #ifndef SCSI_G_NCR5380_MEM
658 for (i = 0; i < 128; i++)
659 NCR5380_write(C400_HOST_BUFFER, src[start + i]);
662 /* implies SCSI_G_NCR5380_MEM */
663 memcpy_toio(hostdata->iomem + NCR53C400_host_buffer,
671 printk("53C400w: waiting for registers to be available\n");
672 THEY NEVER DO ! while (NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_53C80_REG);
673 printk("53C400w: Got em\n");
676 /* Let's wait for this instead - could be ugly */
677 /* All documentation says to check for this. Maybe my hardware is too
678 * fast. Waiting for it seems to work fine! KLL
680 while (!(i = NCR5380_read(C400_CONTROL_STATUS_REG) & CSR_GATED_53C80_IRQ))
681 ; // FIXME - no timeout
684 * I know. i is certainly != 0 here but the loop is new. See previous
688 if (!((i = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_END_DMA_TRANSFER))
689 printk(KERN_ERR "53C400w: No END OF DMA bit - WHOOPS! BASR=%0x\n", i);
691 printk(KERN_ERR "53C400w: no 53C80 gated irq after transfer (last block)\n");
694 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
695 printk(KERN_ERR "53C400w: no end dma signal\n");
698 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
702 #endif /* PSEUDO_DMA */
705 * Include the NCR5380 core code that we build our driver around
710 static struct scsi_host_template driver_template = {
711 .show_info = generic_NCR5380_show_info,
712 .name = "Generic NCR5380/NCR53C400 SCSI",
713 .detect = generic_NCR5380_detect,
714 .release = generic_NCR5380_release_resources,
715 .info = generic_NCR5380_info,
716 .queuecommand = generic_NCR5380_queue_command,
717 .eh_abort_handler = generic_NCR5380_abort,
718 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
719 .bios_param = NCR5380_BIOSPARAM,
720 .can_queue = CAN_QUEUE,
722 .sg_tablesize = SG_ALL,
723 .cmd_per_lun = CMD_PER_LUN,
724 .use_clustering = DISABLE_CLUSTERING,
726 #include <linux/module.h>
727 #include "scsi_module.c"
729 module_param(ncr_irq, int, 0);
730 module_param(ncr_dma, int, 0);
731 module_param(ncr_addr, int, 0);
732 module_param(ncr_5380, int, 0);
733 module_param(ncr_53c400, int, 0);
734 module_param(ncr_53c400a, int, 0);
735 module_param(dtc_3181e, int, 0);
736 MODULE_LICENSE("GPL");
738 #if !defined(SCSI_G_NCR5380_MEM) && defined(MODULE)
739 static struct isapnp_device_id id_table[] = {
741 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
742 ISAPNP_VENDOR('D', 'T', 'C'), ISAPNP_FUNCTION(0x436e),
747 MODULE_DEVICE_TABLE(isapnp, id_table);
750 __setup("ncr5380=", do_NCR5380_setup);
751 __setup("ncr53c400=", do_NCR53C400_setup);
752 __setup("ncr53c400a=", do_NCR53C400A_setup);
753 __setup("dtc3181e=", do_DTC3181E_setup);