blob: 2971c7f0e89885023210ddccb3e28cd9c039a792 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#define PSEUDO_DMA
3#define DONT_USE_INTR
4#define UNSAFE /* Leave interrupts enabled during pseudo-dma I/O */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#define DMA_WORKS_RIGHT
6
7
8/*
9 * DTC 3180/3280 driver, by
10 * Ray Van Tassle rayvt@comm.mot.com
11 *
12 * taken from ...
13 * Trantor T128/T128F/T228 driver by...
14 *
15 * Drew Eckhardt
16 * Visionary Computing
17 * (Unix and Linux consulting and custom programming)
18 * drew@colorado.edu
19 * +1 (303) 440-4894
20 *
21 * DISTRIBUTION RELEASE 1.
Finn Thain3f9e9862014-11-12 16:11:55 +110022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * The card is detected and initialized in one of several ways :
26 * 1. Autoprobe (default) - since the board is memory mapped,
27 * a BIOS signature is scanned for to locate the registers.
28 * An interrupt is triggered to autoprobe for the interrupt
29 * line.
30 *
31 * 2. With command line overrides - dtc=address,irq may be
32 * used on the LILO command line to override the defaults.
33 *
34*/
35
36/*----------------------------------------------------------------*/
37/* the following will set the monitor border color (useful to find
38 where something crashed or gets stuck at */
39/* 1 = blue
40 2 = green
41 3 = cyan
42 4 = red
43 5 = magenta
44 6 = yellow
45 7 = white
46*/
47#if 0
48#define rtrc(i) {inb(0x3da); outb(0x31, 0x3c0); outb((i), 0x3c0);}
49#else
50#define rtrc(i) {}
51#endif
52
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/module.h>
55#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/blkdev.h>
57#include <linux/delay.h>
58#include <linux/stat.h>
59#include <linux/string.h>
60#include <linux/init.h>
61#include <linux/interrupt.h>
Matthew Wilcox53d5ed62006-10-11 01:22:01 -070062#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "scsi.h"
64#include <scsi/scsi_host.h>
65#include "dtc.h"
66#define AUTOPROBE_IRQ
67#include "NCR5380.h"
68
69
70#define DTC_PUBLIC_RELEASE 2
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * The DTC3180 & 3280 boards are memory mapped.
74 *
75 */
76
77/*
78 */
79/* Offset from DTC_5380_OFFSET */
80#define DTC_CONTROL_REG 0x100 /* rw */
81#define D_CR_ACCESS 0x80 /* ro set=can access 3280 registers */
82#define CSR_DIR_READ 0x40 /* rw direction, 1 = read 0 = write */
83
84#define CSR_RESET 0x80 /* wo Resets 53c400 */
85#define CSR_5380_REG 0x80 /* ro 5380 registers can be accessed */
86#define CSR_TRANS_DIR 0x40 /* rw Data transfer direction */
87#define CSR_SCSI_BUFF_INTR 0x20 /* rw Enable int on transfer ready */
88#define CSR_5380_INTR 0x10 /* rw Enable 5380 interrupts */
89#define CSR_SHARED_INTR 0x08 /* rw Interrupt sharing */
90#define CSR_HOST_BUF_NOT_RDY 0x04 /* ro Host buffer not ready */
91#define CSR_SCSI_BUF_RDY 0x02 /* ro SCSI buffer ready */
92#define CSR_GATED_5380_IRQ 0x01 /* ro Last block xferred */
93#define CSR_INT_BASE (CSR_SCSI_BUFF_INTR | CSR_5380_INTR)
94
95
96#define DTC_BLK_CNT 0x101 /* rw
97 * # of 128-byte blocks to transfer */
98
99
100#define D_CR_ACCESS 0x80 /* ro set=can access 3280 registers */
101
102#define DTC_SWITCH_REG 0x3982 /* ro - DIP switches */
103#define DTC_RESUME_XFER 0x3982 /* wo - resume data xfer
104 * after disconnect/reconnect*/
105
106#define DTC_5380_OFFSET 0x3880 /* 8 registers here, see NCR5380.h */
107
108/*!!!! for dtc, it's a 128 byte buffer at 3900 !!! */
109#define DTC_DATA_BUF 0x3900 /* rw 128 bytes long */
110
111static struct override {
112 unsigned int address;
113 int irq;
114} overrides
115#ifdef OVERRIDE
116[] __initdata = OVERRIDE;
117#else
Alan Cox1fbe8522007-08-10 14:50:41 -0700118[4] __initdata = {
119 { 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }, { 0, IRQ_AUTO }
120};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
Tobias Klauser6391a112006-06-08 22:23:48 -0700123#define NO_OVERRIDES ARRAY_SIZE(overrides)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125static struct base {
126 unsigned long address;
127 int noauto;
Tobias Klauser6391a112006-06-08 22:23:48 -0700128} bases[] __initdata = {
129 { 0xcc000, 0 },
130 { 0xc8000, 0 },
131 { 0xdc000, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 { 0xd8000, 0 }
133};
134
Tobias Klauser6391a112006-06-08 22:23:48 -0700135#define NO_BASES ARRAY_SIZE(bases)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137static const struct signature {
138 const char *string;
139 int offset;
Tobias Klauser6391a112006-06-08 22:23:48 -0700140} signatures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 {"DATA TECHNOLOGY CORPORATION BIOS", 0x25},
142};
143
Tobias Klauser6391a112006-06-08 22:23:48 -0700144#define NO_SIGNATURES ARRAY_SIZE(signatures)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#ifndef MODULE
147/*
148 * Function : dtc_setup(char *str, int *ints)
149 *
150 * Purpose : LILO command line initialization of the overrides array,
Tobias Klauser6391a112006-06-08 22:23:48 -0700151 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * Inputs : str - unused, ints - array of integer parameters with ints[0]
153 * equal to the number of ints.
154 *
Alan Cox1fbe8522007-08-10 14:50:41 -0700155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Finn Thain925e4612014-11-12 16:11:49 +1100157static int __init dtc_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 static int commandline_current = 0;
160 int i;
Finn Thain925e4612014-11-12 16:11:49 +1100161 int ints[10];
162
163 get_options(str, ARRAY_SIZE(ints), ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ints[0] != 2)
165 printk("dtc_setup: usage dtc=address,irq\n");
166 else if (commandline_current < NO_OVERRIDES) {
167 overrides[commandline_current].address = ints[1];
168 overrides[commandline_current].irq = ints[2];
169 for (i = 0; i < NO_BASES; ++i)
170 if (bases[i].address == ints[1]) {
171 bases[i].noauto = 1;
172 break;
173 }
174 ++commandline_current;
175 }
Finn Thain925e4612014-11-12 16:11:49 +1100176 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
Finn Thain925e4612014-11-12 16:11:49 +1100178
179__setup("dtc=", dtc_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif
181
182/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100183 * Function : int dtc_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
185 * Purpose : detects and initializes DTC 3180/3280 controllers
186 * that were autoprobed, overridden on the LILO command line,
187 * or specified at compile time.
188 *
189 * Inputs : tpnt - template for this SCSI adapter.
190 *
191 * Returns : 1 if a host adapter was found, 0 if not.
192 *
193*/
194
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100195static int __init dtc_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 static int current_override = 0, current_base = 0;
198 struct Scsi_Host *instance;
199 unsigned int addr;
200 void __iomem *base;
201 int sig, count;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
204 addr = 0;
205 base = NULL;
206
207 if (overrides[current_override].address) {
208 addr = overrides[current_override].address;
209 base = ioremap(addr, 0x2000);
210 if (!base)
211 addr = 0;
212 } else
213 for (; !addr && (current_base < NO_BASES); ++current_base) {
214#if (DTCDEBUG & DTCDEBUG_INIT)
Alan Cox1fbe8522007-08-10 14:50:41 -0700215 printk(KERN_DEBUG "scsi-dtc : probing address %08x\n", bases[current_base].address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
217 if (bases[current_base].noauto)
218 continue;
219 base = ioremap(bases[current_base].address, 0x2000);
220 if (!base)
221 continue;
222 for (sig = 0; sig < NO_SIGNATURES; ++sig) {
223 if (check_signature(base + signatures[sig].offset, signatures[sig].string, strlen(signatures[sig].string))) {
224 addr = bases[current_base].address;
225#if (DTCDEBUG & DTCDEBUG_INIT)
Alan Cox5307b1e2007-09-20 15:12:11 +0100226 printk(KERN_DEBUG "scsi-dtc : detected board.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
228 goto found;
229 }
230 }
231 iounmap(base);
232 }
233
234#if defined(DTCDEBUG) && (DTCDEBUG & DTCDEBUG_INIT)
Alan Cox1fbe8522007-08-10 14:50:41 -0700235 printk(KERN_DEBUG "scsi-dtc : base = %08x\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif
237
238 if (!addr)
239 break;
240
241found:
242 instance = scsi_register(tpnt, sizeof(struct NCR5380_hostdata));
243 if (instance == NULL)
244 break;
245
246 instance->base = addr;
247 ((struct NCR5380_hostdata *)(instance)->hostdata)->base = base;
248
249 NCR5380_init(instance, 0);
250
251 NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR); /* Enable int's */
252 if (overrides[current_override].irq != IRQ_AUTO)
253 instance->irq = overrides[current_override].irq;
254 else
255 instance->irq = NCR5380_probe_irq(instance, DTC_IRQS);
256
Finn Thain22f5f102014-11-12 16:11:56 +1100257 /* Compatibility with documented NCR5380 kernel parameters */
258 if (instance->irq == 255)
259 instance->irq = NO_IRQ;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#ifndef DONT_USE_INTR
262 /* With interrupts enabled, it will sometimes hang when doing heavy
263 * reads. So better not enable them until I finger it out. */
Finn Thain22f5f102014-11-12 16:11:56 +1100264 if (instance->irq != NO_IRQ)
Michael Opdenacker4909cc22014-03-05 06:09:41 +0100265 if (request_irq(instance->irq, dtc_intr, 0,
Jeff Garzik1e641662007-11-11 19:52:05 -0500266 "dtc", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_ERR "scsi%d : IRQ%d not free, interrupts disabled\n", instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100268 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Finn Thain22f5f102014-11-12 16:11:56 +1100271 if (instance->irq == NO_IRQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 printk(KERN_WARNING "scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
273 printk(KERN_WARNING "scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
274 }
275#else
Finn Thain22f5f102014-11-12 16:11:56 +1100276 if (instance->irq != NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 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 +1100278 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279#endif
280#if defined(DTCDEBUG) && (DTCDEBUG & DTCDEBUG_INIT)
281 printk("scsi%d : irq = %d\n", instance->host_no, instance->irq);
282#endif
283
284 printk(KERN_INFO "scsi%d : at 0x%05X", instance->host_no, (int) instance->base);
Finn Thain22f5f102014-11-12 16:11:56 +1100285 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk(" interrupts disabled");
287 else
288 printk(" irq %d", instance->irq);
289 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d", CAN_QUEUE, CMD_PER_LUN, DTC_PUBLIC_RELEASE);
290 NCR5380_print_options(instance);
291 printk("\n");
292
293 ++current_override;
294 ++count;
295 }
296 return count;
297}
298
299/*
300 * Function : int dtc_biosparam(Disk * disk, struct block_device *dev, int *ip)
301 *
302 * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for
303 * the specified device / size.
304 *
305 * Inputs : size = size of device in sectors (512 bytes), dev = block device
306 * major / minor, ip[] = {heads, sectors, cylinders}
307 *
308 * Returns : always 0 (success), initializes ip
309 *
310*/
311
312/*
313 * XXX Most SCSI boards use this mapping, I could be incorrect. Some one
314 * using hard disks on a trantor should verify that this mapping corresponds
315 * to that used by the BIOS / ASPI driver by running the linux fdisk program
316 * and matching the H_C_S coordinates to what DOS uses.
317*/
318
319static int dtc_biosparam(struct scsi_device *sdev, struct block_device *dev,
320 sector_t capacity, int *ip)
321{
322 int size = capacity;
323
324 ip[0] = 64;
325 ip[1] = 32;
326 ip[2] = size >> 11;
327 return 0;
328}
329
330
331/****************************************************************
332 * Function : int NCR5380_pread (struct Scsi_Host *instance,
333 * unsigned char *dst, int len)
334 *
335 * Purpose : Fast 5380 pseudo-dma read function, reads len bytes to
336 * dst
337 *
338 * Inputs : dst = destination, len = length in bytes
339 *
340 * Returns : 0 on success, non zero on a failure such as a watchdog
341 * timeout.
342*/
343
344static int dtc_maxi = 0;
345static int dtc_wmaxi = 0;
346
347static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
348{
349 unsigned char *d = dst;
350 int i; /* For counting time spent in the poll-loop */
351 NCR5380_local_declare();
352 NCR5380_setup(instance);
353
354 i = 0;
355 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
356 NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
Finn Thain22f5f102014-11-12 16:11:56 +1100357 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ);
359 else
360 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ | CSR_INT_BASE);
361 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
362 rtrc(1);
363 while (len > 0) {
364 rtrc(2);
365 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
366 ++i;
367 rtrc(3);
368 memcpy_fromio(d, base + DTC_DATA_BUF, 128);
369 d += 128;
370 len -= 128;
371 rtrc(7);
372 /*** with int's on, it sometimes hangs after here.
373 * Looks like something makes HBNR go away. */
374 }
375 rtrc(4);
376 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
377 ++i;
378 NCR5380_write(MODE_REG, 0); /* Clear the operating mode */
379 rtrc(0);
380 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
381 if (i > dtc_maxi)
382 dtc_maxi = i;
383 return (0);
384}
385
386/****************************************************************
387 * Function : int NCR5380_pwrite (struct Scsi_Host *instance,
388 * unsigned char *src, int len)
389 *
390 * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
391 * src
392 *
393 * Inputs : src = source, len = length in bytes
394 *
395 * Returns : 0 on success, non zero on a failure such as a watchdog
396 * timeout.
397*/
398
399static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
400{
401 int i;
402 NCR5380_local_declare();
403 NCR5380_setup(instance);
404
405 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
406 NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
407 /* set direction (write) */
Finn Thain22f5f102014-11-12 16:11:56 +1100408 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 NCR5380_write(DTC_CONTROL_REG, 0);
410 else
411 NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR);
412 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
413 for (i = 0; len > 0; ++i) {
414 rtrc(5);
415 /* Poll until the host buffer can accept data. */
416 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
417 ++i;
418 rtrc(3);
419 memcpy_toio(base + DTC_DATA_BUF, src, 128);
420 src += 128;
421 len -= 128;
422 }
423 rtrc(4);
424 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
425 ++i;
426 rtrc(6);
427 /* Wait until the last byte has been sent to the disk */
428 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
429 ++i;
430 rtrc(7);
431 /* Check for parity error here. fixme. */
432 NCR5380_write(MODE_REG, 0); /* Clear the operating mode */
433 rtrc(0);
434 if (i > dtc_wmaxi)
435 dtc_wmaxi = i;
436 return (0);
437}
438
439MODULE_LICENSE("GPL");
440
441#include "NCR5380.c"
442
443static int dtc_release(struct Scsi_Host *shost)
444{
445 NCR5380_local_declare();
446 NCR5380_setup(shost);
Finn Thain22f5f102014-11-12 16:11:56 +1100447 if (shost->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500448 free_irq(shost->irq, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 NCR5380_exit(shost);
450 if (shost->io_port && shost->n_io_port)
451 release_region(shost->io_port, shost->n_io_port);
452 scsi_unregister(shost);
453 iounmap(base);
454 return 0;
455}
456
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100457static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .name = "DTC 3180/3280 ",
459 .detect = dtc_detect,
460 .release = dtc_release,
Finn Thain4d3d2a52014-11-12 16:11:52 +1100461 .proc_name = "dtc3x80",
462 .show_info = dtc_show_info,
463 .write_info = dtc_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .queuecommand = dtc_queue_command,
465 .eh_abort_handler = dtc_abort,
466 .eh_bus_reset_handler = dtc_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .bios_param = dtc_biosparam,
468 .can_queue = CAN_QUEUE,
469 .this_id = 7,
470 .sg_tablesize = SG_ALL,
471 .cmd_per_lun = CMD_PER_LUN,
472 .use_clustering = DISABLE_CLUSTERING,
473};
474#include "scsi_module.c"