blob: 2dacf2833b64d12f61786bd78b40b4ff5185d098 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ++current_override;
285 ++count;
286 }
287 return count;
288}
289
290/*
291 * Function : int dtc_biosparam(Disk * disk, struct block_device *dev, int *ip)
292 *
293 * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for
294 * the specified device / size.
295 *
296 * Inputs : size = size of device in sectors (512 bytes), dev = block device
297 * major / minor, ip[] = {heads, sectors, cylinders}
298 *
299 * Returns : always 0 (success), initializes ip
300 *
301*/
302
303/*
304 * XXX Most SCSI boards use this mapping, I could be incorrect. Some one
305 * using hard disks on a trantor should verify that this mapping corresponds
306 * to that used by the BIOS / ASPI driver by running the linux fdisk program
307 * and matching the H_C_S coordinates to what DOS uses.
308*/
309
310static int dtc_biosparam(struct scsi_device *sdev, struct block_device *dev,
311 sector_t capacity, int *ip)
312{
313 int size = capacity;
314
315 ip[0] = 64;
316 ip[1] = 32;
317 ip[2] = size >> 11;
318 return 0;
319}
320
321
322/****************************************************************
323 * Function : int NCR5380_pread (struct Scsi_Host *instance,
324 * unsigned char *dst, int len)
325 *
326 * Purpose : Fast 5380 pseudo-dma read function, reads len bytes to
327 * dst
328 *
329 * Inputs : dst = destination, len = length in bytes
330 *
331 * Returns : 0 on success, non zero on a failure such as a watchdog
332 * timeout.
333*/
334
335static int dtc_maxi = 0;
336static int dtc_wmaxi = 0;
337
338static inline int NCR5380_pread(struct Scsi_Host *instance, unsigned char *dst, int len)
339{
340 unsigned char *d = dst;
341 int i; /* For counting time spent in the poll-loop */
342 NCR5380_local_declare();
343 NCR5380_setup(instance);
344
345 i = 0;
346 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
347 NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
Finn Thain22f5f102014-11-12 16:11:56 +1100348 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ);
350 else
351 NCR5380_write(DTC_CONTROL_REG, CSR_DIR_READ | CSR_INT_BASE);
352 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
353 rtrc(1);
354 while (len > 0) {
355 rtrc(2);
356 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
357 ++i;
358 rtrc(3);
359 memcpy_fromio(d, base + DTC_DATA_BUF, 128);
360 d += 128;
361 len -= 128;
362 rtrc(7);
363 /*** with int's on, it sometimes hangs after here.
364 * Looks like something makes HBNR go away. */
365 }
366 rtrc(4);
367 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
368 ++i;
369 NCR5380_write(MODE_REG, 0); /* Clear the operating mode */
370 rtrc(0);
371 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
372 if (i > dtc_maxi)
373 dtc_maxi = i;
374 return (0);
375}
376
377/****************************************************************
378 * Function : int NCR5380_pwrite (struct Scsi_Host *instance,
379 * unsigned char *src, int len)
380 *
381 * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
382 * src
383 *
384 * Inputs : src = source, len = length in bytes
385 *
386 * Returns : 0 on success, non zero on a failure such as a watchdog
387 * timeout.
388*/
389
390static inline int NCR5380_pwrite(struct Scsi_Host *instance, unsigned char *src, int len)
391{
392 int i;
393 NCR5380_local_declare();
394 NCR5380_setup(instance);
395
396 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
397 NCR5380_write(MODE_REG, MR_ENABLE_EOP_INTR | MR_DMA_MODE);
398 /* set direction (write) */
Finn Thain22f5f102014-11-12 16:11:56 +1100399 if (instance->irq == NO_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 NCR5380_write(DTC_CONTROL_REG, 0);
401 else
402 NCR5380_write(DTC_CONTROL_REG, CSR_5380_INTR);
403 NCR5380_write(DTC_BLK_CNT, len >> 7); /* Block count */
404 for (i = 0; len > 0; ++i) {
405 rtrc(5);
406 /* Poll until the host buffer can accept data. */
407 while (NCR5380_read(DTC_CONTROL_REG) & CSR_HOST_BUF_NOT_RDY)
408 ++i;
409 rtrc(3);
410 memcpy_toio(base + DTC_DATA_BUF, src, 128);
411 src += 128;
412 len -= 128;
413 }
414 rtrc(4);
415 while (!(NCR5380_read(DTC_CONTROL_REG) & D_CR_ACCESS))
416 ++i;
417 rtrc(6);
418 /* Wait until the last byte has been sent to the disk */
419 while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT))
420 ++i;
421 rtrc(7);
422 /* Check for parity error here. fixme. */
423 NCR5380_write(MODE_REG, 0); /* Clear the operating mode */
424 rtrc(0);
425 if (i > dtc_wmaxi)
426 dtc_wmaxi = i;
427 return (0);
428}
429
430MODULE_LICENSE("GPL");
431
432#include "NCR5380.c"
433
434static int dtc_release(struct Scsi_Host *shost)
435{
436 NCR5380_local_declare();
437 NCR5380_setup(shost);
Finn Thain22f5f102014-11-12 16:11:56 +1100438 if (shost->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500439 free_irq(shost->irq, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 NCR5380_exit(shost);
441 if (shost->io_port && shost->n_io_port)
442 release_region(shost->io_port, shost->n_io_port);
443 scsi_unregister(shost);
444 iounmap(base);
445 return 0;
446}
447
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100448static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .name = "DTC 3180/3280 ",
450 .detect = dtc_detect,
451 .release = dtc_release,
Finn Thain4d3d2a52014-11-12 16:11:52 +1100452 .proc_name = "dtc3x80",
453 .show_info = dtc_show_info,
454 .write_info = dtc_write_info,
Finn Thain8c325132014-11-12 16:11:58 +1100455 .info = dtc_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .queuecommand = dtc_queue_command,
457 .eh_abort_handler = dtc_abort,
458 .eh_bus_reset_handler = dtc_bus_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 .bios_param = dtc_biosparam,
460 .can_queue = CAN_QUEUE,
461 .this_id = 7,
462 .sg_tablesize = SG_ALL,
463 .cmd_per_lun = CMD_PER_LUN,
464 .use_clustering = DISABLE_CLUSTERING,
465};
466#include "scsi_module.c"