2 * linux/drivers/acorn/scsi/cumana_2.c
4 * Copyright (C) 1997-2005 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.
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/blkdev.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/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/dma-mapping.h>
32 #include <asm/ecard.h>
35 #include <asm/pgtable.h>
38 #include <scsi/scsi_host.h>
42 #include <scsi/scsicam.h>
44 #define CUMANASCSI2_STATUS (0x0000)
45 #define STATUS_INT (1 << 0)
46 #define STATUS_DRQ (1 << 1)
47 #define STATUS_LATCHED (1 << 3)
49 #define CUMANASCSI2_ALATCH (0x0014)
50 #define ALATCH_ENA_INT (3)
51 #define ALATCH_DIS_INT (2)
52 #define ALATCH_ENA_TERM (5)
53 #define ALATCH_DIS_TERM (4)
54 #define ALATCH_ENA_BIT32 (11)
55 #define ALATCH_DIS_BIT32 (10)
56 #define ALATCH_ENA_DMA (13)
57 #define ALATCH_DIS_DMA (12)
58 #define ALATCH_DMA_OUT (15)
59 #define ALATCH_DMA_IN (14)
61 #define CUMANASCSI2_PSEUDODMA (0x0200)
63 #define CUMANASCSI2_FAS216_OFFSET (0x0300)
64 #define CUMANASCSI2_FAS216_SHIFT 2
69 #define VERSION "1.00 (13/11/2002 2.5.47)"
72 * Use term=0,1,0,0,0 to turn terminators on/off
74 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
78 struct cumanascsi2_info {
80 struct expansion_card *ec;
82 unsigned int terms; /* Terminator state */
83 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
86 #define CSTATUS_IRQ (1 << 0)
87 #define CSTATUS_DRQ (1 << 1)
89 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
90 * Purpose : Enable interrupts on Cumana SCSI 2 card
91 * Params : ec - expansion card structure
92 * : irqnr - interrupt number
95 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
97 struct cumanascsi2_info *info = ec->irq_data;
98 writeb(ALATCH_ENA_INT, info->base + CUMANASCSI2_ALATCH);
101 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
102 * Purpose : Disable interrupts on Cumana SCSI 2 card
103 * Params : ec - expansion card structure
104 * : irqnr - interrupt number
107 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
109 struct cumanascsi2_info *info = ec->irq_data;
110 writeb(ALATCH_DIS_INT, info->base + CUMANASCSI2_ALATCH);
113 static const expansioncard_ops_t cumanascsi_2_ops = {
114 .irqenable = cumanascsi_2_irqenable,
115 .irqdisable = cumanascsi_2_irqdisable,
118 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
119 * Purpose : Turn the Cumana SCSI 2 terminators on or off
120 * Params : host - card to turn on/off
121 * : on_off - !0 to turn on, 0 to turn off
124 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
126 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
130 writeb(ALATCH_ENA_TERM, info->base + CUMANASCSI2_ALATCH);
133 writeb(ALATCH_DIS_TERM, info->base + CUMANASCSI2_ALATCH);
137 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
138 * Purpose : handle interrupts from Cumana SCSI 2 card
139 * Params : irq - interrupt number
140 * dev_id - user-defined (Scsi_Host structure)
141 * regs - processor registers at interrupt
144 cumanascsi_2_intr(int irq, void *dev_id, struct pt_regs *regs)
146 struct cumanascsi2_info *info = dev_id;
148 return fas216_intr(&info->info);
151 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
152 * Purpose : initialises DMA/PIO
153 * Params : host - host
155 * direction - DMA on to/off of card
156 * min_type - minimum DMA support that we must have for this transfer
157 * Returns : type of transfer to be performed
160 cumanascsi_2_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
161 fasdmadir_t direction, fasdmatype_t min_type)
163 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
164 struct device *dev = scsi_get_device(host);
165 int dmach = info->info.scsi.dma;
167 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
169 if (dmach != NO_DMA &&
170 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
171 int bufs, map_dir, dma_dir, alatch_dir;
173 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
175 if (direction == DMA_OUT)
176 map_dir = DMA_TO_DEVICE,
177 dma_dir = DMA_MODE_WRITE,
178 alatch_dir = ALATCH_DMA_OUT;
180 map_dir = DMA_FROM_DEVICE,
181 dma_dir = DMA_MODE_READ,
182 alatch_dir = ALATCH_DMA_IN;
184 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
187 set_dma_sg(dmach, info->sg, bufs + 1);
188 writeb(alatch_dir, info->base + CUMANASCSI2_ALATCH);
189 set_dma_mode(dmach, dma_dir);
191 writeb(ALATCH_ENA_DMA, info->base + CUMANASCSI2_ALATCH);
192 writeb(ALATCH_DIS_BIT32, info->base + CUMANASCSI2_ALATCH);
193 return fasdma_real_all;
197 * If we're not doing DMA,
198 * we'll do pseudo DMA
204 * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
205 * Purpose : handles pseudo DMA
206 * Params : host - host
208 * direction - DMA on to/off of card
209 * transfer - minimum number of bytes we expect to transfer
212 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
213 fasdmadir_t direction, int transfer)
215 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
219 length = SCp->this_residual;
222 if (direction == DMA_OUT)
226 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
228 if (status & STATUS_INT)
231 if (!(status & STATUS_DRQ))
234 word = *addr | *(addr + 1) << 8;
235 writew(word, info->base + CUMANASCSI2_PSEUDODMA);
240 printk ("PSEUDO_OUT???\n");
243 if (transfer && (transfer & 255)) {
244 while (length >= 256) {
245 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
247 if (status & STATUS_INT)
250 if (!(status & STATUS_DRQ))
253 readsw(info->base + CUMANASCSI2_PSEUDODMA,
262 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
264 if (status & STATUS_INT)
267 if (!(status & STATUS_DRQ))
270 word = readw(info->base + CUMANASCSI2_PSEUDODMA);
280 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
281 * Purpose : stops DMA/PIO
282 * Params : host - host
286 cumanascsi_2_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
288 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
289 if (info->info.scsi.dma != NO_DMA) {
290 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
291 disable_dma(info->info.scsi.dma);
295 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
296 * Purpose : returns a descriptive string about this interface,
297 * Params : host - driver host structure to return info for.
298 * Returns : pointer to a static buffer containing null terminated string.
300 const char *cumanascsi_2_info(struct Scsi_Host *host)
302 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
303 static char string[150];
305 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
306 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
307 VERSION, info->terms ? "n" : "ff");
312 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
313 * Purpose : Set a driver specific function
314 * Params : host - host to setup
315 * : buffer - buffer containing string describing operation
316 * : length - length of string
317 * Returns : -EINVAL, or 0
320 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
324 if (length >= 11 && strcmp(buffer, "CUMANASCSI2") == 0) {
328 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
329 if (buffer[5] == '1')
330 cumanascsi_2_terminator_ctl(host, 1);
331 else if (buffer[5] == '0')
332 cumanascsi_2_terminator_ctl(host, 0);
343 /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
344 * int length, int host_no, int inout)
345 * Purpose : Return information about the driver to a user process accessing
346 * the /proc filesystem.
347 * Params : buffer - a buffer to write information to
348 * start - a pointer into this buffer set by this routine to the start
349 * of the required information.
350 * offset - offset into information that we have read upto.
351 * length - length of buffer
352 * host_no - host number to return information for
353 * inout - 0 for reading, 1 for writing.
354 * Returns : length of data written to buffer.
356 int cumanascsi_2_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset,
357 int length, int inout)
359 struct cumanascsi2_info *info;
364 return cumanascsi_2_set_proc_info(host, buffer, length);
366 info = (struct cumanascsi2_info *)host->hostdata;
368 p += sprintf(p, "Cumana SCSI II driver v%s\n", VERSION);
369 p += fas216_print_host(&info->info, p);
370 p += sprintf(p, "Term : o%s\n",
371 info->terms ? "n" : "ff");
373 p += fas216_print_stats(&info->info, p);
374 p += fas216_print_devices(&info->info, p);
376 *start = buffer + offset;
377 pos = p - buffer - offset;
384 static struct scsi_host_template cumanascsi2_template = {
385 .module = THIS_MODULE,
386 .proc_info = cumanascsi_2_proc_info,
387 .name = "Cumana SCSI II",
388 .info = cumanascsi_2_info,
389 .queuecommand = fas216_queue_command,
390 .eh_host_reset_handler = fas216_eh_host_reset,
391 .eh_bus_reset_handler = fas216_eh_bus_reset,
392 .eh_device_reset_handler = fas216_eh_device_reset,
393 .eh_abort_handler = fas216_eh_abort,
396 .sg_tablesize = SG_ALL,
398 .use_clustering = DISABLE_CLUSTERING,
399 .proc_name = "cumanascsi2",
403 cumanascsi2_probe(struct expansion_card *ec, const struct ecard_id *id)
405 struct Scsi_Host *host;
406 struct cumanascsi2_info *info;
407 unsigned long resbase, reslen;
411 ret = ecard_request_resources(ec);
415 resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
416 reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
417 base = ioremap(resbase, reslen);
423 host = scsi_host_alloc(&cumanascsi2_template,
424 sizeof(struct cumanascsi2_info));
430 ecard_set_drvdata(ec, host);
432 info = (struct cumanascsi2_info *)host->hostdata;
436 cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
438 info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
439 info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
440 info->info.scsi.irq = ec->irq;
441 info->info.scsi.dma = ec->dma;
442 info->info.ifcfg.clockrate = 40; /* MHz */
443 info->info.ifcfg.select_timeout = 255;
444 info->info.ifcfg.asyncperiod = 200; /* ns */
445 info->info.ifcfg.sync_max_depth = 7;
446 info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
447 info->info.ifcfg.disconnect_ok = 1;
448 info->info.ifcfg.wide_max_size = 0;
449 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
450 info->info.dma.setup = cumanascsi_2_dma_setup;
451 info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
452 info->info.dma.stop = cumanascsi_2_dma_stop;
454 ec->irqaddr = info->base + CUMANASCSI2_STATUS;
455 ec->irqmask = STATUS_INT;
457 ec->ops = &cumanascsi_2_ops;
459 ret = fas216_init(host);
463 ret = request_irq(ec->irq, cumanascsi_2_intr,
464 SA_INTERRUPT, "cumanascsi2", info);
466 printk("scsi%d: IRQ%d not free: %d\n",
467 host->host_no, ec->irq, ret);
471 if (info->info.scsi.dma != NO_DMA) {
472 if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
473 printk("scsi%d: DMA%d not free, using PIO\n",
474 host->host_no, info->info.scsi.dma);
475 info->info.scsi.dma = NO_DMA;
477 set_dma_speed(info->info.scsi.dma, 180);
478 info->info.ifcfg.capabilities |= FASCAP_DMA;
482 ret = fas216_add(host, &ec->dev);
486 if (info->info.scsi.dma != NO_DMA)
487 free_dma(info->info.scsi.dma);
488 free_irq(ec->irq, host);
491 fas216_release(host);
500 ecard_release_resources(ec);
506 static void __devexit cumanascsi2_remove(struct expansion_card *ec)
508 struct Scsi_Host *host = ecard_get_drvdata(ec);
509 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
511 ecard_set_drvdata(ec, NULL);
514 if (info->info.scsi.dma != NO_DMA)
515 free_dma(info->info.scsi.dma);
516 free_irq(ec->irq, info);
520 fas216_release(host);
522 ecard_release_resources(ec);
525 static const struct ecard_id cumanascsi2_cids[] = {
526 { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
530 static struct ecard_driver cumanascsi2_driver = {
531 .probe = cumanascsi2_probe,
532 .remove = __devexit_p(cumanascsi2_remove),
533 .id_table = cumanascsi2_cids,
535 .name = "cumanascsi2",
539 static int __init cumanascsi2_init(void)
541 return ecard_register_driver(&cumanascsi2_driver);
544 static void __exit cumanascsi2_exit(void)
546 ecard_remove_driver(&cumanascsi2_driver);
549 module_init(cumanascsi2_init);
550 module_exit(cumanascsi2_exit);
552 MODULE_AUTHOR("Russell King");
553 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
554 MODULE_PARM(term, "1-8i");
555 MODULE_PARM_DESC(term, "SCSI bus termination");
556 MODULE_LICENSE("GPL");