blob: 3af4f69c199c1a71a7fefd774522fe85ce2b3797 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#define PSEUDO_DMA
2#define DONT_USE_INTR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#define DMA_WORKS_RIGHT
4
5
6/*
7 * DTC 3180/3280 driver, by
8 * Ray Van Tassle rayvt@comm.mot.com
9 *
10 * taken from ...
11 * Trantor T128/T128F/T228 driver by...
12 *
13 * Drew Eckhardt
14 * Visionary Computing
15 * (Unix and Linux consulting and custom programming)
16 * drew@colorado.edu
17 * +1 (303) 440-4894
Finn Thain3f9e9862014-11-12 16:11:55 +110018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * The card is detected and initialized in one of several ways :
22 * 1. Autoprobe (default) - since the board is memory mapped,
23 * a BIOS signature is scanned for to locate the registers.
24 * An interrupt is triggered to autoprobe for the interrupt
25 * line.
26 *
27 * 2. With command line overrides - dtc=address,irq may be
28 * used on the LILO command line to override the defaults.
29 *
30*/
31
32/*----------------------------------------------------------------*/
33/* the following will set the monitor border color (useful to find
34 where something crashed or gets stuck at */
35/* 1 = blue
36 2 = green
37 3 = cyan
38 4 = red
39 5 = magenta
40 6 = yellow
41 7 = white
42*/
43#if 0
44#define rtrc(i) {inb(0x3da); outb(0x31, 0x3c0); outb((i), 0x3c0);}
45#else
46#define rtrc(i) {}
47#endif
48
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/module.h>
51#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/blkdev.h>
53#include <linux/delay.h>
54#include <linux/stat.h>
55#include <linux/string.h>
56#include <linux/init.h>
57#include <linux/interrupt.h>
Matthew Wilcox53d5ed62006-10-11 01:22:01 -070058#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <scsi/scsi_host.h>
60#include "dtc.h"
61#define AUTOPROBE_IRQ
62#include "NCR5380.h"
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * The DTC3180 & 3280 boards are memory mapped.
66 *
67 */
68
69/*
70 */
71/* Offset from DTC_5380_OFFSET */
72#define DTC_CONTROL_REG 0x100 /* rw */
73#define D_CR_ACCESS 0x80 /* ro set=can access 3280 registers */
74#define CSR_DIR_READ 0x40 /* rw direction, 1 = read 0 = write */
75
76#define CSR_RESET 0x80 /* wo Resets 53c400 */
77#define CSR_5380_REG 0x80 /* ro 5380 registers can be accessed */
78#define CSR_TRANS_DIR 0x40 /* rw Data transfer direction */
79#define CSR_SCSI_BUFF_INTR 0x20 /* rw Enable int on transfer ready */
80#define CSR_5380_INTR 0x10 /* rw Enable 5380 interrupts */
81#define CSR_SHARED_INTR 0x08 /* rw Interrupt sharing */
82#define CSR_HOST_BUF_NOT_RDY 0x04 /* ro Host buffer not ready */
83#define CSR_SCSI_BUF_RDY 0x02 /* ro SCSI buffer ready */
84#define CSR_GATED_5380_IRQ 0x01 /* ro Last block xferred */
85#define CSR_INT_BASE (CSR_SCSI_BUFF_INTR | CSR_5380_INTR)
86
87
88#define DTC_BLK_CNT 0x101 /* rw
89 * # of 128-byte blocks to transfer */
90
91
92#define D_CR_ACCESS 0x80 /* ro set=can access 3280 registers */
93
94#define DTC_SWITCH_REG 0x3982 /* ro - DIP switches */
95#define DTC_RESUME_XFER 0x3982 /* wo - resume data xfer
96 * after disconnect/reconnect*/
97
98#define DTC_5380_OFFSET 0x3880 /* 8 registers here, see NCR5380.h */
99
100/*!!!! for dtc, it's a 128 byte buffer at 3900 !!! */
101#define DTC_DATA_BUF 0x3900 /* rw 128 bytes long */
102
103static struct override {
104 unsigned int address;
105 int irq;
106} overrides
107#ifdef OVERRIDE
108[] __initdata = OVERRIDE;
109#else
Alan Cox1fbe8522007-08-10 14:50:41 -0700110[4] __initdata = {
111 { 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }
112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114
Tobias Klauser6391a112006-06-08 22:23:48 -0700115#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117static struct base {
118 unsigned long address;
119 int noauto;
Tobias Klauser6391a112006-06-08 22:23:48 -0700120} bases[] __initdata = {
121 { 0xcc000, 0 },
122 { 0xc8000, 0 },
123 { 0xdc000, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 { 0xd8000, 0 }
125};
126
Tobias Klauser6391a112006-06-08 22:23:48 -0700127#define NO_BASES ARRAY_SIZE(bases)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static const struct signature {
130 const char *string;
131 int offset;
Tobias Klauser6391a112006-06-08 22:23:48 -0700132} signatures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 {"DATA TECHNOLOGY CORPORATION BIOS", 0x25},
134};
135
Tobias Klauser6391a112006-06-08 22:23:48 -0700136#define NO_SIGNATURES ARRAY_SIZE(signatures)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138#ifndef MODULE
139/*
140 * Function : dtc_setup(char *str, int *ints)
141 *
142 * Purpose : LILO command line initialization of the overrides array,
Tobias Klauser6391a112006-06-08 22:23:48 -0700143 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * Inputs : str - unused, ints - array of integer parameters with ints[0]
145 * equal to the number of ints.
146 *
Alan Cox1fbe8522007-08-10 14:50:41 -0700147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Finn Thain925e4612014-11-12 16:11:49 +1100149static int __init dtc_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Finn Thaind5f7e652016-01-03 16:05:03 +1100151 static int commandline_current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int i;
Finn Thain925e4612014-11-12 16:11:49 +1100153 int ints[10];
154
155 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (ints[0] != 2)
157 printk("dtc_setup: usage dtc=address,irq\n");
158 else if (commandline_current < NO_OVERRIDES) {
159 overrides[commandline_current].address = ints[1];
160 overrides[commandline_current].irq = ints[2];
161 for (i = 0; i < NO_BASES; ++i)
162 if (bases[i].address == ints[1]) {
163 bases[i].noauto = 1;
164 break;
165 }
166 ++commandline_current;
167 }
Finn Thain925e4612014-11-12 16:11:49 +1100168 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Finn Thain925e4612014-11-12 16:11:49 +1100170
171__setup("dtc=", dtc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
174/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100175 * Function : int dtc_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Purpose : detects and initializes DTC 3180/3280 controllers
178 * that were autoprobed, overridden on the LILO command line,
179 * or specified at compile time.
180 *
181 * Inputs : tpnt - template for this SCSI adapter.
182 *
183 * Returns : 1 if a host adapter was found, 0 if not.
184 *
185*/
186
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100187static int __init dtc_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Finn Thaind5f7e652016-01-03 16:05:03 +1100189 static int current_override, current_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct Scsi_Host *instance;
191 unsigned int addr;
192 void __iomem *base;
193 int sig, count;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
196 addr = 0;
197 base = NULL;
198
199 if (overrides[current_override].address) {
200 addr = overrides[current_override].address;
201 base = ioremap(addr, 0x2000);
202 if (!base)
203 addr = 0;
204 } else
205 for (; !addr && (current_base < NO_BASES); ++current_base) {
Finn Thain2f7dba92016-01-03 16:05:04 +1100206 dprintk(NDEBUG_INIT, "dtc: probing address 0x%08x\n",
207 (unsigned int)bases[current_base].address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (bases[current_base].noauto)
209 continue;
210 base = ioremap(bases[current_base].address, 0x2000);
211 if (!base)
212 continue;
213 for (sig = 0; sig < NO_SIGNATURES; ++sig) {
214 if (check_signature(base + signatures[sig].offset, signatures[sig].string, strlen(signatures[sig].string))) {
215 addr = bases[current_base].address;
Finn Thain2f7dba92016-01-03 16:05:04 +1100216 dprintk(NDEBUG_INIT, "dtc: detected board\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto found;
218 }
219 }
220 iounmap(base);
221 }
222
Finn Thain2f7dba92016-01-03 16:05:04 +1100223 dprintk(NDEBUG_INIT, "dtc: addr = 0x%08x\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!addr)
226 break;
227
228found:
229 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
230 if (instance == NULL)
Finn Thain0ad0eff2016-01-03 16:05:21 +1100231 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 instance->base = addr;
234 ((struct NCR5380_hostdata *)(instance)->hostdata)->base = base;
235
Finn Thain0ad0eff2016-01-03 16:05:21 +1100236 if (NCR5380_init(instance, 0))
237 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Finn Thainb6488f92016-01-03 16:05:08 +1100239 NCR5380_maybe_reset_bus(instance);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR); /* Enable int's */
242 if (overrides[current_override].irq != IRQ_AUTO)
243 instance->irq = overrides[current_override].irq;
244 else
245 instance->irq = NCR5380_probe_irq(instance, DTC_IRQS);
246
Finn Thain22f5f102014-11-12 16:11:56 +1100247 /* Compatibility with documented NCR5380 kernel parameters */
248 if (instance->irq == 255)
249 instance->irq = NO_IRQ;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#ifndef DONT_USE_INTR
252 /* With interrupts enabled, it will sometimes hang when doing heavy
253 * reads. So better not enable them until I finger it out. */
Finn Thain22f5f102014-11-12 16:11:56 +1100254 if (instance->irq != NO_IRQ)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100255 if (request_irq(instance->irq, dtc_intr, 0,
Jeff Garzik1e641662007-11-11 19:52:05 -0500256 "dtc", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 printk(KERN_ERR "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100258 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Finn Thain22f5f102014-11-12 16:11:56 +1100261 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 printk(KERN_WARNING "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
263 printk(KERN_WARNING "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
264 }
265#else
Finn Thain22f5f102014-11-12 16:11:56 +1100266 if (instance->irq != NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_WARNING "scsi%d : interrupts not used. Might as well not jumper it.\n", instance->host_no);
Finn Thain22f5f102014-11-12 16:11:56 +1100268 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#endif
Finn Thain2f7dba92016-01-03 16:05:04 +1100270 dprintk(NDEBUG_INIT, "scsi%d : irq = %d\n",
271 instance->host_no, instance->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 ++current_override;
274 ++count;
275 }
276 return count;
Finn Thain0ad0eff2016-01-03 16:05:21 +1100277
278out_unregister:
279 scsi_unregister(instance);
280out_unmap:
281 iounmap(base);
282 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286 * Function : int dtc_biosparam(Disk * disk, struct block_device *dev, int *ip)
287 *
288 * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for
289 * the specified device / size.
290 *
291 * Inputs : size = size of device in sectors (512 bytes), dev = block device
292 * major / minor, ip[] = {heads, sectors, cylinders}
293 *
294 * Returns : always 0 (success), initializes ip
295 *
296*/
297
298/*
299 * XXX Most SCSI boards use this mapping, I could be incorrect. Some one
300 * using hard disks on a trantor should verify that this mapping corresponds
301 * to that used by the BIOS / ASPI driver by running the linux fdisk program
302 * and matching the H_C_S coordinates to what DOS uses.
303*/
304
305static int dtc_biosparam(struct scsi_device *sdev, struct block_device *dev,
306 sector_t capacity, int *ip)
307{
308 int size = capacity;
309
310 ip[0] = 64;
311 ip[1] = 32;
312 ip[2] = size >> 11;
313 return 0;
314}
315
316
317/****************************************************************
318 * Function : int NCR5380_pread (struct Scsi_Host *instance,
319 * unsigned char *dst, int len)
320 *
321 * Purpose : Fast 5380 pseudo-dma read function, reads len bytes to
322 * dst
323 *
324 * Inputs : dst = destination, len = length in bytes
325 *
326 * Returns : 0 on success, non zero on a failure such as a watchdog
327 * timeout.
328*/
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
331{
332 unsigned char *d = dst;
333 int i; /* For counting time spent in the poll-loop */
Finn Thaina9c2dc42014-11-12 16:11:59 +1100334 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 i = 0;
Finn Thain22f5f102014-11-12 16:11:56 +1100337 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ);
339 else
340 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ | CSR_INT_BASE);
341 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
342 rtrc(1);
343 while (len > 0) {
344 rtrc(2);
345 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
346 ++i;
347 rtrc(3);
Finn Thain54d8fe42016-01-03 16:05:06 +1100348 memcpy_fromio(d, hostdata->base + DTC_DATA_BUF, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 d += 128;
350 len -= 128;
351 rtrc(7);
352 /*** with int's on, it sometimes hangs after here.
353 * Looks like something makes HBNR go away. */
354 }
355 rtrc(4);
356 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
357 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 rtrc(0);
Finn Thaina9c2dc42014-11-12 16:11:59 +1100359 if (i > hostdata->spin_max_r)
360 hostdata->spin_max_r = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return (0);
362}
363
364/****************************************************************
365 * Function : int NCR5380_pwrite (struct Scsi_Host *instance,
366 * unsigned char *src, int len)
367 *
368 * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
369 * src
370 *
371 * Inputs : src = source, len = length in bytes
372 *
373 * Returns : 0 on success, non zero on a failure such as a watchdog
374 * timeout.
375*/
376
377static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
378{
379 int i;
Finn Thaina9c2dc42014-11-12 16:11:59 +1100380 struct NCR5380_hostdata *hostdata = shost_priv(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Finn Thain22f5f102014-11-12 16:11:56 +1100382 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 NCR5380_write(DTC_CONTROL_REG, 0);
384 else
385 NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR);
386 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
387 for (i = 0; len > 0; ++i) {
388 rtrc(5);
389 /* Poll until the host buffer can accept data. */
390 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
391 ++i;
392 rtrc(3);
Finn Thain54d8fe42016-01-03 16:05:06 +1100393 memcpy_toio(hostdata->base + DTC_DATA_BUF, src, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 src += 128;
395 len -= 128;
396 }
397 rtrc(4);
398 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
399 ++i;
400 rtrc(6);
401 /* Wait until the last byte has been sent to the disk */
402 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
403 ++i;
404 rtrc(7);
405 /* Check for parity error here. fixme. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 rtrc(0);
Finn Thaina9c2dc42014-11-12 16:11:59 +1100407 if (i > hostdata->spin_max_w)
408 hostdata->spin_max_w = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return (0);
410}
411
Finn Thainff3d4572016-01-03 16:05:25 +1100412static int dtc_dma_xfer_len(struct scsi_cmnd *cmd)
413{
414 int transfersize = cmd->transfersize;
415
416 /* Limit transfers to 32K, for xx400 & xx406
417 * pseudoDMA that transfers in 128 bytes blocks.
418 */
419 if (transfersize > 32 * 1024 && cmd->SCp.this_residual &&
420 !(cmd->SCp.this_residual % transfersize))
421 transfersize = 32 * 1024;
422
423 return transfersize;
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426MODULE_LICENSE("GPL");
427
428#include "NCR5380.c"
429
430static int dtc_release(struct Scsi_Host *shost)
431{
Finn Thain54d8fe42016-01-03 16:05:06 +1100432 struct NCR5380_hostdata *hostdata = shost_priv(shost);
433
Finn Thain22f5f102014-11-12 16:11:56 +1100434 if (shost->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500435 free_irq(shost->irq, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 NCR5380_exit(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 scsi_unregister(shost);
Finn Thain54d8fe42016-01-03 16:05:06 +1100438 iounmap(hostdata->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100442static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .name = "DTC 3180/3280 ",
444 .detect = dtc_detect,
445 .release = dtc_release,
Finn Thain4d3d2a52014-11-12 16:11:52 +1100446 .proc_name = "dtc3x80",
447 .show_info = dtc_show_info,
448 .write_info = dtc_write_info,
Finn Thain8c325132014-11-12 16:11:58 +1100449 .info = dtc_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .queuecommand = dtc_queue_command,
451 .eh_abort_handler = dtc_abort,
452 .eh_bus_reset_handler = dtc_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 .bios_param = dtc_biosparam,
454 .can_queue = CAN_QUEUE,
455 .this_id = 7,
456 .sg_tablesize = SG_ALL,
457 .cmd_per_lun = CMD_PER_LUN,
458 .use_clustering = DISABLE_CLUSTERING,
459};
460#include "scsi_module.c"