1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Future Domain TMC-16x0 and TMC-3260 SCSI host adapters
4 * Copyright 2019 Ondrej Zary
7 * Rickard E. Faith, faith@cs.unc.edu
9 * Future Domain BIOS versions supported for autodetect:
10 * 2.0, 3.0, 3.2, 3.4 (1.0), 3.5 (2.0), 3.6, 3.61
12 * TMC-1800, TMC-18C50, TMC-18C30, TMC-36C70
14 * Future Domain TMC-1650, TMC-1660, TMC-1670, TMC-1680, TMC-1610M/MER/MEX
15 * Future Domain TMC-3260 (PCI)
16 * Quantum ISA-200S, ISA-250MG
17 * Adaptec AHA-2920A (PCI) [BUT *NOT* AHA-2920C -- use aic7xxx instead]
22 * The Adaptec AHA-2920C has an Adaptec AIC-7850 chip on it.
23 * Use the aic7xxx driver for this board.
25 * The Adaptec AHA-2920A has a Future Domain chip on it, so this is the right
26 * driver for that card. Unfortunately, the boxes will probably just say
27 * "2920", so you'll have to look on the card for a Future Domain logo, or a
28 * letter after the 2920.
30 * If you have a TMC-8xx or TMC-9xx board, then this is not the driver for
35 * This is the Linux low-level SCSI driver for Future Domain TMC-1660/1680
36 * TMC-1650/1670, and TMC-3260 SCSI host adapters. The 1650 and 1670 have a
37 * 25-pin external connector, whereas the 1660 and 1680 have a SCSI-2 50-pin
38 * high-density external connector. The 1670 and 1680 have floppy disk
39 * controllers built in. The TMC-3260 is a PCI bus card.
41 * Future Domain's older boards are based on the TMC-1800 chip, and this
42 * driver was originally written for a TMC-1680 board with the TMC-1800 chip.
43 * More recently, boards are being produced with the TMC-18C50 and TMC-18C30
46 * Please note that the drive ordering that Future Domain implemented in BIOS
47 * versions 3.4 and 3.5 is the opposite of the order (currently) used by the
48 * rest of the SCSI industry.
53 * "TMC-1800 SCSI Chip Specification (FDC-1800T)", Future Domain Corporation,
56 * "Technical Reference Manual: 18C50 SCSI Host Adapter Chip", Future Domain
57 * Corporation, January 1992.
59 * "LXT SCSI Products: Specifications and OEM Technical Manual (Revision
60 * B/September 1991)", Maxtor Corporation, 1991.
62 * "7213S product Manual (Revision P3)", Maxtor Corporation, 1992.
64 * "Draft Proposed American National Standard: Small Computer System
65 * Interface - 2 (SCSI-2)", Global Engineering Documents. (X3T9.2/86-109,
66 * revision 10h, October 17, 1991)
68 * Private communications, Drew Eckhardt (drew@cs.colorado.edu) and Eric
69 * Youngdale (ericy@cais.com), 1992.
71 * Private communication, Tuong Le (Future Domain Engineering department),
72 * 1994. (Disk geometry computations for Future Domain BIOS version 3.4, and
73 * TMC-18C30 detection.)
75 * Hogan, Thom. The Programmer's PC Sourcebook. Microsoft Press, 1988. Page
76 * 60 (2.39: Disk Partition Table Layout).
78 * "18C30 Technical Reference Manual", Future Domain Corporation, 1993, page
82 #include <linux/module.h>
83 #include <linux/interrupt.h>
84 #include <linux/delay.h>
85 #include <linux/pci.h>
86 #include <linux/workqueue.h>
87 #include <scsi/scsicam.h>
88 #include <scsi/scsi_cmnd.h>
89 #include <scsi/scsi_device.h>
90 #include <scsi/scsi_host.h>
94 * FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the
95 * 18C30 chip have a 2k cache). When this many 512 byte blocks are filled by
96 * the SCSI device, an interrupt will be raised. Therefore, this could be as
97 * low as 0, or as high as 16. Note, however, that values which are too high
98 * or too low seem to prevent any interrupts from occurring, and thereby lock
101 #define FIFO_COUNT 2 /* Number of 512 byte blocks before INTR */
102 #define PARITY_MASK ACTL_PAREN /* Parity enabled, 0 = disabled */
113 struct scsi_cmnd
*cur_cmd
;
115 struct work_struct work
;
118 static struct scsi_pointer
*fdomain_scsi_pointer(struct scsi_cmnd
*cmd
)
120 return scsi_cmd_priv(cmd
);
123 static inline void fdomain_make_bus_idle(struct fdomain
*fd
)
125 outb(0, fd
->base
+ REG_BCTL
);
126 outb(0, fd
->base
+ REG_MCTL
);
127 if (fd
->chip
== tmc18c50
|| fd
->chip
== tmc18c30
)
128 /* Clear forced intr. */
129 outb(ACTL_RESET
| ACTL_CLRFIRQ
| PARITY_MASK
,
130 fd
->base
+ REG_ACTL
);
132 outb(ACTL_RESET
| PARITY_MASK
, fd
->base
+ REG_ACTL
);
135 static enum chip_type
fdomain_identify(int port
)
137 u16 id
= inb(port
+ REG_ID_LSB
) | inb(port
+ REG_ID_MSB
) << 8;
142 case 0x60e9: /* 18c50 or 18c30 */
148 /* Try to toggle 32-bit mode. This only works on an 18c30 chip. */
149 outb(CFG2_32BIT
, port
+ REG_CFG2
);
150 if ((inb(port
+ REG_CFG2
) & CFG2_32BIT
)) {
151 outb(0, port
+ REG_CFG2
);
152 if ((inb(port
+ REG_CFG2
) & CFG2_32BIT
) == 0)
155 /* If that failed, we are an 18c50. */
159 static int fdomain_test_loopback(int base
)
163 for (i
= 0; i
< 255; i
++) {
164 outb(i
, base
+ REG_LOOPBACK
);
165 if (inb(base
+ REG_LOOPBACK
) != i
)
172 static void fdomain_reset(int base
)
174 outb(BCTL_RST
, base
+ REG_BCTL
);
176 outb(0, base
+ REG_BCTL
);
178 outb(0, base
+ REG_MCTL
);
179 outb(PARITY_MASK
, base
+ REG_ACTL
);
182 static int fdomain_select(struct Scsi_Host
*sh
, int target
)
185 unsigned long timeout
;
186 struct fdomain
*fd
= shost_priv(sh
);
188 outb(BCTL_BUSEN
| BCTL_SEL
, fd
->base
+ REG_BCTL
);
189 outb(BIT(sh
->this_id
) | BIT(target
), fd
->base
+ REG_SCSI_DATA_NOACK
);
191 /* Stop arbitration and enable parity */
192 outb(PARITY_MASK
, fd
->base
+ REG_ACTL
);
194 timeout
= 350; /* 350 msec */
197 status
= inb(fd
->base
+ REG_BSTAT
);
198 if (status
& BSTAT_BSY
) {
199 /* Enable SCSI Bus */
200 /* (on error, should make bus idle with 0) */
201 outb(BCTL_BUSEN
, fd
->base
+ REG_BCTL
);
206 fdomain_make_bus_idle(fd
);
210 static void fdomain_finish_cmd(struct fdomain
*fd
)
212 outb(0, fd
->base
+ REG_ICTL
);
213 fdomain_make_bus_idle(fd
);
214 scsi_done(fd
->cur_cmd
);
218 static void fdomain_read_data(struct scsi_cmnd
*cmd
)
220 struct fdomain
*fd
= shost_priv(cmd
->device
->host
);
221 unsigned char *virt
, *ptr
;
224 while ((len
= inw(fd
->base
+ REG_FIFO_COUNT
)) > 0) {
225 offset
= scsi_bufflen(cmd
) - scsi_get_resid(cmd
);
226 virt
= scsi_kmap_atomic_sg(scsi_sglist(cmd
), scsi_sg_count(cmd
),
230 *ptr
++ = inb(fd
->base
+ REG_FIFO
);
232 insw(fd
->base
+ REG_FIFO
, ptr
, len
>> 1);
233 scsi_set_resid(cmd
, scsi_get_resid(cmd
) - len
);
234 scsi_kunmap_atomic_sg(virt
);
238 static void fdomain_write_data(struct scsi_cmnd
*cmd
)
240 struct fdomain
*fd
= shost_priv(cmd
->device
->host
);
241 /* 8k FIFO for pre-tmc18c30 chips, 2k FIFO for tmc18c30 */
242 int FIFO_Size
= fd
->chip
== tmc18c30
? 0x800 : 0x2000;
243 unsigned char *virt
, *ptr
;
246 while ((len
= FIFO_Size
- inw(fd
->base
+ REG_FIFO_COUNT
)) > 512) {
247 offset
= scsi_bufflen(cmd
) - scsi_get_resid(cmd
);
248 if (len
+ offset
> scsi_bufflen(cmd
)) {
249 len
= scsi_bufflen(cmd
) - offset
;
253 virt
= scsi_kmap_atomic_sg(scsi_sglist(cmd
), scsi_sg_count(cmd
),
257 outb(*ptr
++, fd
->base
+ REG_FIFO
);
259 outsw(fd
->base
+ REG_FIFO
, ptr
, len
>> 1);
260 scsi_set_resid(cmd
, scsi_get_resid(cmd
) - len
);
261 scsi_kunmap_atomic_sg(virt
);
265 static void fdomain_work(struct work_struct
*work
)
267 struct fdomain
*fd
= container_of(work
, struct fdomain
, work
);
268 struct Scsi_Host
*sh
= container_of((void *)fd
, struct Scsi_Host
,
270 struct scsi_cmnd
*cmd
= fd
->cur_cmd
;
271 struct scsi_pointer
*scsi_pointer
= fdomain_scsi_pointer(cmd
);
276 spin_lock_irqsave(sh
->host_lock
, flags
);
278 if (scsi_pointer
->phase
& in_arbitration
) {
279 status
= inb(fd
->base
+ REG_ASTAT
);
280 if (!(status
& ASTAT_ARB
)) {
281 set_host_byte(cmd
, DID_BUS_BUSY
);
282 fdomain_finish_cmd(fd
);
285 scsi_pointer
->phase
= in_selection
;
287 outb(ICTL_SEL
| FIFO_COUNT
, fd
->base
+ REG_ICTL
);
288 outb(BCTL_BUSEN
| BCTL_SEL
, fd
->base
+ REG_BCTL
);
289 outb(BIT(cmd
->device
->host
->this_id
) | BIT(scmd_id(cmd
)),
290 fd
->base
+ REG_SCSI_DATA_NOACK
);
291 /* Stop arbitration and enable parity */
292 outb(ACTL_IRQEN
| PARITY_MASK
, fd
->base
+ REG_ACTL
);
294 } else if (scsi_pointer
->phase
& in_selection
) {
295 status
= inb(fd
->base
+ REG_BSTAT
);
296 if (!(status
& BSTAT_BSY
)) {
297 /* Try again, for slow devices */
298 if (fdomain_select(cmd
->device
->host
, scmd_id(cmd
))) {
299 set_host_byte(cmd
, DID_NO_CONNECT
);
300 fdomain_finish_cmd(fd
);
303 /* Stop arbitration and enable parity */
304 outb(ACTL_IRQEN
| PARITY_MASK
, fd
->base
+ REG_ACTL
);
306 scsi_pointer
->phase
= in_other
;
307 outb(ICTL_FIFO
| ICTL_REQ
| FIFO_COUNT
, fd
->base
+ REG_ICTL
);
308 outb(BCTL_BUSEN
, fd
->base
+ REG_BCTL
);
312 /* fdomain_scsi_pointer(cur_cmd)->phase == in_other: this is the body of the routine */
313 status
= inb(fd
->base
+ REG_BSTAT
);
315 if (status
& BSTAT_REQ
) {
316 switch (status
& (BSTAT_MSG
| BSTAT_CMD
| BSTAT_IO
)) {
317 case BSTAT_CMD
: /* COMMAND OUT */
318 outb(cmd
->cmnd
[scsi_pointer
->sent_command
++],
319 fd
->base
+ REG_SCSI_DATA
);
321 case 0: /* DATA OUT -- tmc18c50/tmc18c30 only */
322 if (fd
->chip
!= tmc1800
&& !scsi_pointer
->have_data_in
) {
323 scsi_pointer
->have_data_in
= -1;
324 outb(ACTL_IRQEN
| ACTL_FIFOWR
| ACTL_FIFOEN
|
325 PARITY_MASK
, fd
->base
+ REG_ACTL
);
328 case BSTAT_IO
: /* DATA IN -- tmc18c50/tmc18c30 only */
329 if (fd
->chip
!= tmc1800
&& !scsi_pointer
->have_data_in
) {
330 scsi_pointer
->have_data_in
= 1;
331 outb(ACTL_IRQEN
| ACTL_FIFOEN
| PARITY_MASK
,
332 fd
->base
+ REG_ACTL
);
335 case BSTAT_CMD
| BSTAT_IO
: /* STATUS IN */
336 scsi_pointer
->Status
= inb(fd
->base
+ REG_SCSI_DATA
);
338 case BSTAT_MSG
| BSTAT_CMD
: /* MESSAGE OUT */
339 outb(MESSAGE_REJECT
, fd
->base
+ REG_SCSI_DATA
);
341 case BSTAT_MSG
| BSTAT_CMD
| BSTAT_IO
: /* MESSAGE IN */
342 scsi_pointer
->Message
= inb(fd
->base
+ REG_SCSI_DATA
);
343 if (scsi_pointer
->Message
== COMMAND_COMPLETE
)
349 if (fd
->chip
== tmc1800
&& !scsi_pointer
->have_data_in
&&
350 scsi_pointer
->sent_command
>= cmd
->cmd_len
) {
351 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
352 scsi_pointer
->have_data_in
= -1;
353 outb(ACTL_IRQEN
| ACTL_FIFOWR
| ACTL_FIFOEN
|
354 PARITY_MASK
, fd
->base
+ REG_ACTL
);
356 scsi_pointer
->have_data_in
= 1;
357 outb(ACTL_IRQEN
| ACTL_FIFOEN
| PARITY_MASK
,
358 fd
->base
+ REG_ACTL
);
362 if (scsi_pointer
->have_data_in
== -1) /* DATA OUT */
363 fdomain_write_data(cmd
);
365 if (scsi_pointer
->have_data_in
== 1) /* DATA IN */
366 fdomain_read_data(cmd
);
369 set_status_byte(cmd
, scsi_pointer
->Status
);
370 set_host_byte(cmd
, DID_OK
);
371 scsi_msg_to_host_byte(cmd
, scsi_pointer
->Message
);
372 fdomain_finish_cmd(fd
);
374 if (scsi_pointer
->phase
& disconnect
) {
375 outb(ICTL_FIFO
| ICTL_SEL
| ICTL_REQ
| FIFO_COUNT
,
376 fd
->base
+ REG_ICTL
);
377 outb(0, fd
->base
+ REG_BCTL
);
379 outb(ICTL_FIFO
| ICTL_REQ
| FIFO_COUNT
,
380 fd
->base
+ REG_ICTL
);
383 spin_unlock_irqrestore(sh
->host_lock
, flags
);
386 static irqreturn_t
fdomain_irq(int irq
, void *dev_id
)
388 struct fdomain
*fd
= dev_id
;
391 if ((inb(fd
->base
+ REG_ASTAT
) & ASTAT_IRQ
) == 0)
394 outb(0, fd
->base
+ REG_ICTL
);
396 /* We usually have one spurious interrupt after each command. */
397 if (!fd
->cur_cmd
) /* Spurious interrupt */
400 schedule_work(&fd
->work
);
405 static int fdomain_queue(struct Scsi_Host
*sh
, struct scsi_cmnd
*cmd
)
407 struct scsi_pointer
*scsi_pointer
= fdomain_scsi_pointer(cmd
);
408 struct fdomain
*fd
= shost_priv(cmd
->device
->host
);
411 scsi_pointer
->Status
= 0;
412 scsi_pointer
->Message
= 0;
413 scsi_pointer
->have_data_in
= 0;
414 scsi_pointer
->sent_command
= 0;
415 scsi_pointer
->phase
= in_arbitration
;
416 scsi_set_resid(cmd
, scsi_bufflen(cmd
));
418 spin_lock_irqsave(sh
->host_lock
, flags
);
422 fdomain_make_bus_idle(fd
);
424 /* Start arbitration */
425 outb(0, fd
->base
+ REG_ICTL
);
426 outb(0, fd
->base
+ REG_BCTL
); /* Disable data drivers */
428 outb(BIT(cmd
->device
->host
->this_id
), fd
->base
+ REG_SCSI_DATA_NOACK
);
429 outb(ICTL_ARB
, fd
->base
+ REG_ICTL
);
430 /* Start arbitration */
431 outb(ACTL_ARB
| ACTL_IRQEN
| PARITY_MASK
, fd
->base
+ REG_ACTL
);
433 spin_unlock_irqrestore(sh
->host_lock
, flags
);
438 static int fdomain_abort(struct scsi_cmnd
*cmd
)
440 struct Scsi_Host
*sh
= cmd
->device
->host
;
441 struct fdomain
*fd
= shost_priv(sh
);
447 spin_lock_irqsave(sh
->host_lock
, flags
);
449 fdomain_make_bus_idle(fd
);
450 fdomain_scsi_pointer(fd
->cur_cmd
)->phase
|= aborted
;
452 /* Aborts are not done well. . . */
453 set_host_byte(fd
->cur_cmd
, DID_ABORT
);
454 fdomain_finish_cmd(fd
);
455 spin_unlock_irqrestore(sh
->host_lock
, flags
);
459 static int fdomain_host_reset(struct scsi_cmnd
*cmd
)
461 struct Scsi_Host
*sh
= cmd
->device
->host
;
462 struct fdomain
*fd
= shost_priv(sh
);
465 spin_lock_irqsave(sh
->host_lock
, flags
);
466 fdomain_reset(fd
->base
);
467 spin_unlock_irqrestore(sh
->host_lock
, flags
);
471 static int fdomain_biosparam(struct scsi_device
*sdev
,
472 struct block_device
*bdev
, sector_t capacity
,
475 unsigned char *p
= scsi_bios_ptable(bdev
);
477 if (p
&& p
[65] == 0xaa && p
[64] == 0x55 /* Partition table valid */
478 && p
[4]) { /* Partition type */
479 geom
[0] = p
[5] + 1; /* heads */
480 geom
[1] = p
[6] & 0x3f; /* sectors */
482 if (capacity
>= 0x7e0000) {
483 geom
[0] = 255; /* heads */
484 geom
[1] = 63; /* sectors */
485 } else if (capacity
>= 0x200000) {
486 geom
[0] = 128; /* heads */
487 geom
[1] = 63; /* sectors */
489 geom
[0] = 64; /* heads */
490 geom
[1] = 32; /* sectors */
493 geom
[2] = sector_div(capacity
, geom
[0] * geom
[1]);
499 static const struct scsi_host_template fdomain_template
= {
500 .module
= THIS_MODULE
,
501 .name
= "Future Domain TMC-16x0",
502 .proc_name
= "fdomain",
503 .queuecommand
= fdomain_queue
,
504 .eh_abort_handler
= fdomain_abort
,
505 .eh_host_reset_handler
= fdomain_host_reset
,
506 .bios_param
= fdomain_biosparam
,
510 .dma_boundary
= PAGE_SIZE
- 1,
511 .cmd_size
= sizeof(struct scsi_pointer
),
514 struct Scsi_Host
*fdomain_create(int base
, int irq
, int this_id
,
517 struct Scsi_Host
*sh
;
520 static const char * const chip_names
[] = {
521 "Unknown", "TMC-1800", "TMC-18C50", "TMC-18C30"
523 unsigned long irq_flags
= 0;
525 chip
= fdomain_identify(base
);
531 if (fdomain_test_loopback(base
))
535 dev_err(dev
, "card has no IRQ assigned");
539 sh
= scsi_host_alloc(&fdomain_template
, sizeof(struct fdomain
));
544 sh
->this_id
= this_id
& 0x07;
548 sh
->n_io_port
= FDOMAIN_REGION_SIZE
;
553 INIT_WORK(&fd
->work
, fdomain_work
);
555 if (dev_is_pci(dev
) || !strcmp(dev
->bus
->name
, "pcmcia"))
556 irq_flags
= IRQF_SHARED
;
558 if (request_irq(irq
, fdomain_irq
, irq_flags
, "fdomain", fd
))
561 shost_printk(KERN_INFO
, sh
, "%s chip at 0x%x irq %d SCSI ID %d\n",
562 dev_is_pci(dev
) ? "TMC-36C70 (PCI bus)" : chip_names
[chip
],
563 base
, irq
, sh
->this_id
);
565 if (scsi_add_host(sh
, dev
))
578 EXPORT_SYMBOL_GPL(fdomain_create
);
580 int fdomain_destroy(struct Scsi_Host
*sh
)
582 struct fdomain
*fd
= shost_priv(sh
);
584 cancel_work_sync(&fd
->work
);
585 scsi_remove_host(sh
);
587 free_irq(sh
->irq
, fd
);
591 EXPORT_SYMBOL_GPL(fdomain_destroy
);
593 #ifdef CONFIG_PM_SLEEP
594 static int fdomain_resume(struct device
*dev
)
596 struct fdomain
*fd
= shost_priv(dev_get_drvdata(dev
));
598 fdomain_reset(fd
->base
);
602 static SIMPLE_DEV_PM_OPS(fdomain_pm_ops
, NULL
, fdomain_resume
);
603 #endif /* CONFIG_PM_SLEEP */
605 MODULE_AUTHOR("Ondrej Zary, Rickard E. Faith");
606 MODULE_DESCRIPTION("Future Domain TMC-16x0/TMC-3260 SCSI driver");
607 MODULE_LICENSE("GPL");