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 */
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[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
56 module_param_array(irq, int, NULL, 0);
57 MODULE_PARM_DESC(irq, "IRQ number(s)");
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");
71 * Configure I/O address of 53C400A or DTC436 by writing magic numbers
72 * to ports 0x779 and 0x379.
74 static void magic_configure(int idx, u8 irq, u8 magic[])
78 outb(magic[0], 0x779);
79 outb(magic[1], 0x379);
80 outb(magic[2], 0x379);
81 outb(magic[3], 0x379);
82 outb(magic[4], 0x379);
84 /* allowed IRQs for HP C2502 */
85 if (irq != 2 && irq != 3 && irq != 4 && irq != 5 && irq != 7)
87 if (idx >= 0 && idx <= 7)
88 cfg = 0x80 | idx | (irq << 4);
92 static unsigned int ncr_53c400a_ports[] = {
93 0x280, 0x290, 0x300, 0x310, 0x330, 0x340, 0x348, 0x350, 0
95 static unsigned int dtc_3181e_ports[] = {
96 0x220, 0x240, 0x280, 0x2a0, 0x2c0, 0x300, 0x320, 0x340, 0
98 static u8 ncr_53c400a_magic[] = { /* 53C400A & DTC436 */
99 0x59, 0xb9, 0xc5, 0xae, 0xa6
101 static u8 hp_c2502_magic[] = { /* HP C2502 */
102 0x0f, 0x22, 0xf0, 0x20, 0x80
105 static int generic_NCR5380_init_one(struct scsi_host_template *tpnt,
106 struct device *pdev, int base, int irq, int board)
108 bool is_pmio = base <= 0xffff;
111 unsigned int *ports = NULL;
115 unsigned long region_size;
116 struct Scsi_Host *instance;
117 struct NCR5380_hostdata *hostdata;
122 flags = FLAG_NO_PSEUDO_DMA | FLAG_DMA_FIXUP;
124 case BOARD_NCR53C400A:
125 ports = ncr_53c400a_ports;
126 magic = ncr_53c400a_magic;
129 ports = ncr_53c400a_ports;
130 magic = hp_c2502_magic;
133 ports = dtc_3181e_ports;
134 magic = ncr_53c400a_magic;
138 if (is_pmio && ports && magic) {
139 /* wakeup sequence for the NCR53C400A and DTC3181E */
141 /* Disable the adapter and look for a free io port */
142 magic_configure(-1, 0, magic);
146 for (i = 0; ports[i]; i++) {
147 if (base == ports[i]) { /* index found */
148 if (!request_region(ports[i],
156 for (i = 0; ports[i]; i++) {
157 if (!request_region(ports[i], region_size,
160 if (inb(ports[i]) == 0xff)
162 release_region(ports[i], region_size);
165 /* At this point we have our region reserved */
166 magic_configure(i, 0, magic); /* no IRQ yet */
168 outb(0xc0, base + 9);
169 if (inb(base + 9) != 0x80) {
176 } else if (is_pmio) {
177 /* NCR5380 - no configuration, just grab */
179 if (!base || !request_region(base, region_size, "ncr5380"))
182 region_size = NCR53C400_region_size;
183 if (!request_mem_region(base, region_size, "ncr5380"))
188 iomem = ioport_map(base, region_size);
190 iomem = ioremap(base, region_size);
197 instance = scsi_host_alloc(tpnt, sizeof(struct NCR5380_hostdata));
198 if (instance == NULL) {
202 hostdata = shost_priv(instance);
204 hostdata->io = iomem;
205 hostdata->region_size = region_size;
208 hostdata->io_port = base;
209 hostdata->io_width = 1; /* 8-bit PDMA by default */
210 hostdata->offset = 0;
213 * On NCR53C400 boards, NCR5380 registers are mapped 8 past
217 case BOARD_NCR53C400:
218 hostdata->io_port += 8;
219 hostdata->c400_ctl_status = 0;
220 hostdata->c400_blk_cnt = 1;
221 hostdata->c400_host_buf = 4;
224 hostdata->io_width = 2; /* 16-bit PDMA */
226 case BOARD_NCR53C400A:
228 hostdata->c400_ctl_status = 9;
229 hostdata->c400_blk_cnt = 10;
230 hostdata->c400_host_buf = 8;
234 hostdata->base = base;
235 hostdata->offset = NCR53C400_mem_base;
237 case BOARD_NCR53C400:
238 hostdata->c400_ctl_status = 0x100;
239 hostdata->c400_blk_cnt = 0x101;
240 hostdata->c400_host_buf = 0x104;
243 case BOARD_NCR53C400A:
245 pr_err(DRV_MODULE_NAME ": unknown register offsets\n");
251 ret = NCR5380_init(instance, flags | FLAG_LATE_DMA_SETUP);
256 case BOARD_NCR53C400:
258 case BOARD_NCR53C400A:
260 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
263 NCR5380_maybe_reset_bus(instance);
268 instance->irq = NCR5380_probe_irq(instance, 0xffff);
270 /* Compatibility with documented NCR5380 kernel parameters */
271 if (instance->irq == 255)
272 instance->irq = NO_IRQ;
274 if (instance->irq != NO_IRQ) {
275 /* set IRQ for HP C2502 */
276 if (board == BOARD_HP_C2502)
277 magic_configure(port_idx, instance->irq, magic);
278 if (request_irq(instance->irq, generic_NCR5380_intr,
279 0, "NCR5380", instance)) {
280 printk(KERN_WARNING "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
281 instance->irq = NO_IRQ;
285 if (instance->irq == NO_IRQ) {
286 printk(KERN_INFO "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
287 printk(KERN_INFO "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
290 ret = scsi_add_host(instance, pdev);
293 scsi_scan_host(instance);
294 dev_set_drvdata(pdev, instance);
298 if (instance->irq != NO_IRQ)
299 free_irq(instance->irq, instance);
300 NCR5380_exit(instance);
302 scsi_host_put(instance);
307 release_region(base, region_size);
309 release_mem_region(base, region_size);
313 static void generic_NCR5380_release_resources(struct Scsi_Host *instance)
315 struct NCR5380_hostdata *hostdata = shost_priv(instance);
316 void __iomem *iomem = hostdata->io;
317 unsigned long io_port = hostdata->io_port;
318 unsigned long base = hostdata->base;
319 unsigned long region_size = hostdata->region_size;
321 scsi_remove_host(instance);
322 if (instance->irq != NO_IRQ)
323 free_irq(instance->irq, instance);
324 NCR5380_exit(instance);
325 scsi_host_put(instance);
328 release_region(io_port, region_size);
330 release_mem_region(base, region_size);
334 * generic_NCR5380_pread - pseudo DMA read
335 * @hostdata: scsi host private data
336 * @dst: buffer to read into
337 * @len: buffer length
339 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
343 static inline int generic_NCR5380_pread(struct NCR5380_hostdata *hostdata,
344 unsigned char *dst, int len)
346 int blocks = len / 128;
349 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE | CSR_TRANS_DIR);
350 NCR5380_write(hostdata->c400_blk_cnt, blocks);
352 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
354 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
355 printk(KERN_ERR "53C400r: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
358 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
359 ; /* FIXME - no timeout */
361 if (hostdata->io_port && hostdata->io_width == 2)
362 insw(hostdata->io_port + hostdata->c400_host_buf,
364 else if (hostdata->io_port)
365 insb(hostdata->io_port + hostdata->c400_host_buf,
368 memcpy_fromio(dst + start,
369 hostdata->io + NCR53C400_host_buffer, 128);
376 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
377 ; /* FIXME - no timeout */
379 if (hostdata->io_port && hostdata->io_width == 2)
380 insw(hostdata->io_port + hostdata->c400_host_buf,
382 else if (hostdata->io_port)
383 insb(hostdata->io_port + hostdata->c400_host_buf,
386 memcpy_fromio(dst + start,
387 hostdata->io + NCR53C400_host_buffer, 128);
393 if (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ))
394 printk("53C400r: no 53C80 gated irq after transfer");
396 /* wait for 53C80 registers to be available */
397 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG))
400 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER))
401 printk(KERN_ERR "53C400r: no end dma signal\n");
407 * generic_NCR5380_pwrite - pseudo DMA write
408 * @hostdata: scsi host private data
409 * @dst: buffer to read into
410 * @len: buffer length
412 * Perform a pseudo DMA mode read from an NCR53C400 or equivalent
416 static inline int generic_NCR5380_pwrite(struct NCR5380_hostdata *hostdata,
417 unsigned char *src, int len)
419 int blocks = len / 128;
422 NCR5380_write(hostdata->c400_ctl_status, CSR_BASE);
423 NCR5380_write(hostdata->c400_blk_cnt, blocks);
425 if (NCR5380_read(hostdata->c400_ctl_status) & CSR_GATED_53C80_IRQ) {
426 printk(KERN_ERR "53C400w: Got 53C80_IRQ start=%d, blocks=%d\n", start, blocks);
430 if (NCR5380_read(hostdata->c400_blk_cnt) == 0)
432 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
435 if (hostdata->io_port && hostdata->io_width == 2)
436 outsw(hostdata->io_port + hostdata->c400_host_buf,
438 else if (hostdata->io_port)
439 outsb(hostdata->io_port + hostdata->c400_host_buf,
442 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
449 while (NCR5380_read(hostdata->c400_ctl_status) & CSR_HOST_BUF_NOT_RDY)
450 ; // FIXME - no timeout
452 if (hostdata->io_port && hostdata->io_width == 2)
453 outsw(hostdata->io_port + hostdata->c400_host_buf,
455 else if (hostdata->io_port)
456 outsb(hostdata->io_port + hostdata->c400_host_buf,
459 memcpy_toio(hostdata->io + NCR53C400_host_buffer,
466 /* wait for 53C80 registers to be available */
467 while (!(NCR5380_read(hostdata->c400_ctl_status) & CSR_53C80_REG)) {
468 udelay(4); /* DTC436 chip hangs without this */
469 /* FIXME - no timeout */
472 if (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_END_DMA_TRANSFER)) {
473 printk(KERN_ERR "53C400w: no end dma signal\n");
476 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
481 static int generic_NCR5380_dma_xfer_len(struct NCR5380_hostdata *hostdata,
482 struct scsi_cmnd *cmd)
484 int transfersize = cmd->transfersize;
486 if (hostdata->flags & FLAG_NO_PSEUDO_DMA)
489 /* Limit transfers to 32K, for xx400 & xx406
490 * pseudoDMA that transfers in 128 bytes blocks.
492 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
493 !(cmd->SCp.this_residual % transfersize))
494 transfersize = 32 * 1024;
496 /* 53C400 datasheet: non-modulo-128-byte transfers should use PIO */
497 if (transfersize % 128)
504 * Include the NCR5380 core code that we build our driver around
509 static struct scsi_host_template driver_template = {
510 .module = THIS_MODULE,
511 .proc_name = DRV_MODULE_NAME,
512 .name = "Generic NCR5380/NCR53C400 SCSI",
513 .info = generic_NCR5380_info,
514 .queuecommand = generic_NCR5380_queue_command,
515 .eh_abort_handler = generic_NCR5380_abort,
516 .eh_bus_reset_handler = generic_NCR5380_bus_reset,
519 .sg_tablesize = SG_ALL,
521 .use_clustering = DISABLE_CLUSTERING,
522 .cmd_size = NCR5380_CMD_SIZE,
527 static int generic_NCR5380_isa_match(struct device *pdev, unsigned int ndev)
529 int ret = generic_NCR5380_init_one(&driver_template, pdev, base[ndev],
530 irq[ndev], card[ndev]);
533 printk(KERN_WARNING "Card not found at address 0x%03x\n",
541 static int generic_NCR5380_isa_remove(struct device *pdev,
544 generic_NCR5380_release_resources(dev_get_drvdata(pdev));
545 dev_set_drvdata(pdev, NULL);
549 static struct isa_driver generic_NCR5380_isa_driver = {
550 .match = generic_NCR5380_isa_match,
551 .remove = generic_NCR5380_isa_remove,
553 .name = DRV_MODULE_NAME
558 static struct pnp_device_id generic_NCR5380_pnp_ids[] = {
559 { .id = "DTC436e", .driver_data = BOARD_DTC3181E },
562 MODULE_DEVICE_TABLE(pnp, generic_NCR5380_pnp_ids);
564 static int generic_NCR5380_pnp_probe(struct pnp_dev *pdev,
565 const struct pnp_device_id *id)
569 if (pnp_activate_dev(pdev) < 0)
572 base = pnp_port_start(pdev, 0);
573 irq = pnp_irq(pdev, 0);
575 return generic_NCR5380_init_one(&driver_template, &pdev->dev, base, irq,
579 static void generic_NCR5380_pnp_remove(struct pnp_dev *pdev)
581 generic_NCR5380_release_resources(pnp_get_drvdata(pdev));
582 pnp_set_drvdata(pdev, NULL);
585 static struct pnp_driver generic_NCR5380_pnp_driver = {
586 .name = DRV_MODULE_NAME,
587 .id_table = generic_NCR5380_pnp_ids,
588 .probe = generic_NCR5380_pnp_probe,
589 .remove = generic_NCR5380_pnp_remove,
591 #endif /* defined(CONFIG_PNP) */
593 static int pnp_registered, isa_registered;
595 static int __init generic_NCR5380_init(void)
599 /* compatibility with old-style parameters */
600 if (irq[0] == 0 && base[0] == 0 && card[0] == -1) {
604 card[0] = BOARD_NCR5380;
606 card[0] = BOARD_NCR53C400;
608 card[0] = BOARD_NCR53C400A;
610 card[0] = BOARD_DTC3181E;
612 card[0] = BOARD_HP_C2502;
616 if (!pnp_register_driver(&generic_NCR5380_pnp_driver))
619 ret = isa_register_driver(&generic_NCR5380_isa_driver, MAX_CARDS);
623 return (pnp_registered || isa_registered) ? 0 : ret;
626 static void __exit generic_NCR5380_exit(void)
630 pnp_unregister_driver(&generic_NCR5380_pnp_driver);
633 isa_unregister_driver(&generic_NCR5380_isa_driver);
636 module_init(generic_NCR5380_init);
637 module_exit(generic_NCR5380_exit);