Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / acorn / scsi / cumana_2.c
blob33aa30401b1aea0864d5f6df5748327e6e6d4009
1 /*
2 * linux/drivers/acorn/scsi/cumana_2.c
4 * Copyright (C) 1997-2002 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Changelog:
11 * 30-08-1997 RMK 0.0.0 Created, READONLY version.
12 * 22-01-1998 RMK 0.0.1 Updated to 2.1.80.
13 * 15-04-1998 RMK 0.0.1 Only do PIO if FAS216 will allow it.
14 * 02-05-1998 RMK 0.0.2 Updated & added DMA support.
15 * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
16 * 18-08-1998 RMK 0.0.3 Fixed synchronous transfer depth.
17 * 02-04-2000 RMK 0.0.4 Updated for new error handling code.
19 #include <linux/module.h>
20 #include <linux/blk.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/ioport.h>
24 #include <linux/sched.h>
25 #include <linux/proc_fs.h>
26 #include <linux/unistd.h>
27 #include <linux/stat.h>
28 #include <linux/delay.h>
29 #include <linux/pci.h>
30 #include <linux/init.h>
32 #include <asm/dma.h>
33 #include <asm/ecard.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/pgtable.h>
38 #include "../../scsi/scsi.h"
39 #include "../../scsi/hosts.h"
40 #include "fas216.h"
41 #include "scsi.h"
43 #include <scsi/scsicam.h>
45 #define CUMANASCSI2_STATUS (0)
46 #define STATUS_INT (1 << 0)
47 #define STATUS_DRQ (1 << 1)
48 #define STATUS_LATCHED (1 << 3)
50 #define CUMANASCSI2_ALATCH (5)
51 #define ALATCH_ENA_INT (3)
52 #define ALATCH_DIS_INT (2)
53 #define ALATCH_ENA_TERM (5)
54 #define ALATCH_DIS_TERM (4)
55 #define ALATCH_ENA_BIT32 (11)
56 #define ALATCH_DIS_BIT32 (10)
57 #define ALATCH_ENA_DMA (13)
58 #define ALATCH_DIS_DMA (12)
59 #define ALATCH_DMA_OUT (15)
60 #define ALATCH_DMA_IN (14)
62 #define CUMANASCSI2_PSEUDODMA (0x80)
64 #define CUMANASCSI2_FAS216_OFFSET (0xc0)
65 #define CUMANASCSI2_FAS216_SHIFT 0
66 #define CUMANASCSI2_FAS216_SIZE (16)
69 * Version
71 #define VERSION "1.00 (13/11/2002 2.5.47)"
74 * Use term=0,1,0,0,0 to turn terminators on/off
76 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
78 #define NR_SG 256
80 struct cumanascsi2_info {
81 FAS216_Info info;
83 unsigned int status; /* card status register */
84 unsigned int alatch; /* Control register */
85 unsigned int terms; /* Terminator state */
86 unsigned int dmaarea; /* Pseudo DMA area */
87 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
90 #define CSTATUS_IRQ (1 << 0)
91 #define CSTATUS_DRQ (1 << 1)
93 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
94 * Purpose : Enable interrupts on Cumana SCSI 2 card
95 * Params : ec - expansion card structure
96 * : irqnr - interrupt number
98 static void
99 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
101 unsigned int port = (unsigned int)ec->irq_data;
102 outb(ALATCH_ENA_INT, port);
105 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
106 * Purpose : Disable interrupts on Cumana SCSI 2 card
107 * Params : ec - expansion card structure
108 * : irqnr - interrupt number
110 static void
111 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
113 unsigned int port = (unsigned int)ec->irq_data;
114 outb(ALATCH_DIS_INT, port);
117 static const expansioncard_ops_t cumanascsi_2_ops = {
118 .irqenable = cumanascsi_2_irqenable,
119 .irqdisable = cumanascsi_2_irqdisable,
122 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
123 * Purpose : Turn the Cumana SCSI 2 terminators on or off
124 * Params : host - card to turn on/off
125 * : on_off - !0 to turn on, 0 to turn off
127 static void
128 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
130 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
132 if (on_off) {
133 info->terms = 1;
134 outb (ALATCH_ENA_TERM, info->alatch);
135 } else {
136 info->terms = 0;
137 outb (ALATCH_DIS_TERM, info->alatch);
141 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
142 * Purpose : handle interrupts from Cumana SCSI 2 card
143 * Params : irq - interrupt number
144 * dev_id - user-defined (Scsi_Host structure)
145 * regs - processor registers at interrupt
147 static void
148 cumanascsi_2_intr(int irq, void *dev_id, struct pt_regs *regs)
150 struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
152 fas216_intr(host);
155 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
156 * Purpose : initialises DMA/PIO
157 * Params : host - host
158 * SCpnt - command
159 * direction - DMA on to/off of card
160 * min_type - minimum DMA support that we must have for this transfer
161 * Returns : type of transfer to be performed
163 static fasdmatype_t
164 cumanascsi_2_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
165 fasdmadir_t direction, fasdmatype_t min_type)
167 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
168 int dmach = host->dma_channel;
170 outb(ALATCH_DIS_DMA, info->alatch);
172 if (dmach != NO_DMA &&
173 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
174 int bufs, pci_dir, dma_dir, alatch_dir;
176 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
178 if (direction == DMA_OUT)
179 pci_dir = PCI_DMA_TODEVICE,
180 dma_dir = DMA_MODE_WRITE,
181 alatch_dir = ALATCH_DMA_OUT;
182 else
183 pci_dir = PCI_DMA_FROMDEVICE,
184 dma_dir = DMA_MODE_READ,
185 alatch_dir = ALATCH_DMA_IN;
187 pci_map_sg(NULL, info->sg, bufs + 1, pci_dir);
189 disable_dma(dmach);
190 set_dma_sg(dmach, info->sg, bufs + 1);
191 outb(alatch_dir, info->alatch);
192 set_dma_mode(dmach, dma_dir);
193 enable_dma(dmach);
194 outb(ALATCH_ENA_DMA, info->alatch);
195 outb(ALATCH_DIS_BIT32, info->alatch);
196 return fasdma_real_all;
200 * If we're not doing DMA,
201 * we'll do pseudo DMA
203 return fasdma_pio;
207 * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
208 * Purpose : handles pseudo DMA
209 * Params : host - host
210 * SCpnt - command
211 * direction - DMA on to/off of card
212 * transfer - minimum number of bytes we expect to transfer
214 static void
215 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
216 fasdmadir_t direction, int transfer)
218 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
219 unsigned int length;
220 unsigned char *addr;
222 length = SCp->this_residual;
223 addr = SCp->ptr;
225 if (direction == DMA_OUT)
226 #if 0
227 while (length > 1) {
228 unsigned long word;
229 unsigned int status = inb(info->status);
231 if (status & STATUS_INT)
232 goto end;
234 if (!(status & STATUS_DRQ))
235 continue;
237 word = *addr | *(addr + 1) << 8;
238 outw (info->dmaarea);
239 addr += 2;
240 length -= 2;
242 #else
243 printk ("PSEUDO_OUT???\n");
244 #endif
245 else {
246 if (transfer && (transfer & 255)) {
247 while (length >= 256) {
248 unsigned int status = inb(info->status);
250 if (status & STATUS_INT)
251 goto end;
253 if (!(status & STATUS_DRQ))
254 continue;
256 insw(info->dmaarea, addr, 256 >> 1);
257 addr += 256;
258 length -= 256;
262 while (length > 0) {
263 unsigned long word;
264 unsigned int status = inb(info->status);
266 if (status & STATUS_INT)
267 goto end;
269 if (!(status & STATUS_DRQ))
270 continue;
272 word = inw (info->dmaarea);
273 *addr++ = word;
274 if (--length > 0) {
275 *addr++ = word >> 8;
276 length --;
281 end:
284 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
285 * Purpose : stops DMA/PIO
286 * Params : host - host
287 * SCpnt - command
289 static void
290 cumanascsi_2_dma_stop(struct Scsi_Host *host, Scsi_Pointer *SCp)
292 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
293 if (host->dma_channel != NO_DMA) {
294 outb(ALATCH_DIS_DMA, info->alatch);
295 disable_dma(host->dma_channel);
299 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
300 * Purpose : returns a descriptive string about this interface,
301 * Params : host - driver host structure to return info for.
302 * Returns : pointer to a static buffer containing null terminated string.
304 const char *cumanascsi_2_info(struct Scsi_Host *host)
306 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
307 static char string[100], *p;
309 p = string;
310 p += sprintf(p, "%s ", host->hostt->name);
311 p += fas216_info(&info->info, p);
312 p += sprintf(p, "v%s terminators o%s",
313 VERSION, info->terms ? "n" : "ff");
315 return string;
318 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
319 * Purpose : Set a driver specific function
320 * Params : host - host to setup
321 * : buffer - buffer containing string describing operation
322 * : length - length of string
323 * Returns : -EINVAL, or 0
325 static int
326 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
328 int ret = length;
330 if (length >= 11 && strcmp(buffer, "CUMANASCSI2") == 0) {
331 buffer += 11;
332 length -= 11;
334 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
335 if (buffer[5] == '1')
336 cumanascsi_2_terminator_ctl(host, 1);
337 else if (buffer[5] == '0')
338 cumanascsi_2_terminator_ctl(host, 0);
339 else
340 ret = -EINVAL;
341 } else
342 ret = -EINVAL;
343 } else
344 ret = -EINVAL;
346 return ret;
349 /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
350 * int length, int host_no, int inout)
351 * Purpose : Return information about the driver to a user process accessing
352 * the /proc filesystem.
353 * Params : buffer - a buffer to write information to
354 * start - a pointer into this buffer set by this routine to the start
355 * of the required information.
356 * offset - offset into information that we have read upto.
357 * length - length of buffer
358 * host_no - host number to return information for
359 * inout - 0 for reading, 1 for writing.
360 * Returns : length of data written to buffer.
362 int cumanascsi_2_proc_info (char *buffer, char **start, off_t offset,
363 int length, int host_no, int inout)
365 int pos, begin;
366 struct Scsi_Host *host;
367 struct cumanascsi2_info *info;
368 Scsi_Device *scd;
370 host = scsi_host_hn_get(host_no);
371 if (!host)
372 return 0;
374 if (inout == 1)
375 return cumanascsi_2_set_proc_info(host, buffer, length);
377 info = (struct cumanascsi2_info *)host->hostdata;
379 begin = 0;
380 pos = sprintf(buffer,
381 "Cumana SCSI II driver version v%s\n",
382 VERSION);
384 pos += fas216_print_host(&info->info, buffer + pos);
385 pos += sprintf(buffer + pos, "Term : o%s\n",
386 info->terms ? "n" : "ff");
388 pos += fas216_print_stats(&info->info, buffer + pos);
390 pos += sprintf(buffer+pos, "\nAttached devices:\n");
392 for (scd = host->host_queue; scd; scd = scd->next) {
393 int len;
395 proc_print_scsidevice(scd, buffer, &len, pos);
396 pos += len;
397 pos += sprintf(buffer+pos, "Extensions: ");
398 if (scd->tagged_supported)
399 pos += sprintf(buffer+pos, "TAG %sabled [%d] ",
400 scd->tagged_queue ? "en" : "dis",
401 scd->current_tag);
402 pos += sprintf(buffer+pos, "\n");
404 if (pos + begin < offset) {
405 begin += pos;
406 pos = 0;
408 if (pos + begin > offset + length)
409 break;
412 *start = buffer + (offset - begin);
413 pos -= offset - begin;
414 if (pos > length)
415 pos = length;
417 return pos;
420 static Scsi_Host_Template cumanascsi2_template = {
421 .module = THIS_MODULE,
422 .proc_info = cumanascsi_2_proc_info,
423 .name = "Cumana SCSI II",
424 .info = cumanascsi_2_info,
425 .command = fas216_command,
426 .queuecommand = fas216_queue_command,
427 .eh_host_reset_handler = fas216_eh_host_reset,
428 .eh_bus_reset_handler = fas216_eh_bus_reset,
429 .eh_device_reset_handler = fas216_eh_device_reset,
430 .eh_abort_handler = fas216_eh_abort,
431 .can_queue = 1,
432 .this_id = 7,
433 .sg_tablesize = SG_ALL,
434 .cmd_per_lun = 1,
435 .use_clustering = DISABLE_CLUSTERING,
436 .proc_name = "cumanascsi2",
439 static int __devinit
440 cumanascsi2_probe(struct expansion_card *ec, const struct ecard_id *id)
442 struct Scsi_Host *host;
443 struct cumanascsi2_info *info;
444 int ret = -ENOMEM;
446 host = scsi_register(&cumanascsi2_template, sizeof (struct cumanascsi2_info));
447 if (!host)
448 goto out;
450 host->io_port = ecard_address(ec, ECARD_MEMC, 0);
451 host->irq = ec->irq;
452 host->dma_channel = ec->dma;
454 if (!request_region(host->io_port + CUMANASCSI2_FAS216_OFFSET,
455 CUMANASCSI2_FAS216_SIZE, "cumanascsi2-fas")) {
456 ret = -EBUSY;
457 goto out_free;
460 ecard_set_drvdata(ec, host);
462 info = (struct cumanascsi2_info *)host->hostdata;
463 info->dmaarea = host->io_port + CUMANASCSI2_PSEUDODMA;
464 info->status = host->io_port + CUMANASCSI2_STATUS;
465 info->alatch = host->io_port + CUMANASCSI2_ALATCH;
467 ec->irqaddr = (unsigned char *)ioaddr(info->status);
468 ec->irqmask = STATUS_INT;
469 ec->irq_data = (void *)info->alatch;
470 ec->ops = (expansioncard_ops_t *)&cumanascsi_2_ops;
472 cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
474 info->info.scsi.io_port = host->io_port + CUMANASCSI2_FAS216_OFFSET;
475 info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
476 info->info.scsi.irq = host->irq;
477 info->info.ifcfg.clockrate = 40; /* MHz */
478 info->info.ifcfg.select_timeout = 255;
479 info->info.ifcfg.asyncperiod = 200; /* ns */
480 info->info.ifcfg.sync_max_depth = 7;
481 info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
482 info->info.ifcfg.disconnect_ok = 1;
483 info->info.ifcfg.wide_max_size = 0;
484 info->info.dma.setup = cumanascsi_2_dma_setup;
485 info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
486 info->info.dma.stop = cumanascsi_2_dma_stop;
488 ret = request_irq(host->irq, cumanascsi_2_intr,
489 SA_INTERRUPT, "cumanascsi2", host);
490 if (ret) {
491 printk("scsi%d: IRQ%d not free: %d\n",
492 host->host_no, host->irq, ret);
493 goto out_region;
496 if (host->dma_channel != NO_DMA) {
497 if (request_dma(host->dma_channel, "cumanascsi2")) {
498 printk("scsi%d: DMA%d not free, using PIO\n",
499 host->host_no, host->dma_channel);
500 host->dma_channel = NO_DMA;
501 } else {
502 set_dma_speed(host->dma_channel, 180);
506 fas216_init(host);
508 ret = scsi_add_host(host);
509 if (ret == 0)
510 goto out;
512 fas216_release(host);
514 if (host->dma_channel != NO_DMA)
515 free_dma(host->dma_channel);
516 free_irq(host->irq, host);
517 out_region:
518 release_region(host->io_port + CUMANASCSI2_FAS216_OFFSET,
519 CUMANASCSI2_FAS216_SIZE);
520 out_free:
521 scsi_unregister(host);
523 out:
524 return ret;
527 static void __devexit cumanascsi2_remove(struct expansion_card *ec)
529 struct Scsi_Host *host = ecard_get_drvdata(ec);
531 ecard_set_drvdata(ec, NULL);
532 scsi_remove_host(host);
533 fas216_release(host);
535 if (host->dma_channel != NO_DMA)
536 free_dma(host->dma_channel);
537 free_irq(host->irq, host);
538 release_region(host->io_port + CUMANASCSI2_FAS216_OFFSET,
539 CUMANASCSI2_FAS216_SIZE);
541 scsi_unregister(host);
544 static const struct ecard_id cumanascsi2_cids[] = {
545 { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
546 { 0xffff, 0xffff },
549 static struct ecard_driver cumanascsi2_driver = {
550 .probe = cumanascsi2_probe,
551 .remove = __devexit_p(cumanascsi2_remove),
552 .id_table = cumanascsi2_cids,
553 .drv = {
554 .name = "cumanascsi2",
558 static int __init cumanascsi2_init(void)
560 return ecard_register_driver(&cumanascsi2_driver);
563 static void __exit cumanascsi2_exit(void)
565 ecard_remove_driver(&cumanascsi2_driver);
568 module_init(cumanascsi2_init);
569 module_exit(cumanascsi2_exit);
571 MODULE_AUTHOR("Russell King");
572 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
573 MODULE_PARM(term, "1-8i");
574 MODULE_PARM_DESC(term, "SCSI bus termination");
575 MODULE_LICENSE("GPL");