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>
34 #include <asm/pgtable.h>
37 #include <scsi/scsi_host.h>
41 #include <scsi/scsicam.h>
43 #define CUMANASCSI2_STATUS (0x0000)
44 #define STATUS_INT (1 << 0)
45 #define STATUS_DRQ (1 << 1)
46 #define STATUS_LATCHED (1 << 3)
48 #define CUMANASCSI2_ALATCH (0x0014)
49 #define ALATCH_ENA_INT (3)
50 #define ALATCH_DIS_INT (2)
51 #define ALATCH_ENA_TERM (5)
52 #define ALATCH_DIS_TERM (4)
53 #define ALATCH_ENA_BIT32 (11)
54 #define ALATCH_DIS_BIT32 (10)
55 #define ALATCH_ENA_DMA (13)
56 #define ALATCH_DIS_DMA (12)
57 #define ALATCH_DMA_OUT (15)
58 #define ALATCH_DMA_IN (14)
60 #define CUMANASCSI2_PSEUDODMA (0x0200)
62 #define CUMANASCSI2_FAS216_OFFSET (0x0300)
63 #define CUMANASCSI2_FAS216_SHIFT 2
68 #define VERSION "1.00 (13/11/2002 2.5.47)"
71 * Use term=0,1,0,0,0 to turn terminators on/off
73 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
77 struct cumanascsi2_info {
79 struct expansion_card *ec;
81 unsigned int terms; /* Terminator state */
82 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
85 #define CSTATUS_IRQ (1 << 0)
86 #define CSTATUS_DRQ (1 << 1)
88 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
89 * Purpose : Enable interrupts on Cumana SCSI 2 card
90 * Params : ec - expansion card structure
91 * : irqnr - interrupt number
94 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
96 struct cumanascsi2_info *info = ec->irq_data;
97 writeb(ALATCH_ENA_INT, info->base + CUMANASCSI2_ALATCH);
100 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
101 * Purpose : Disable interrupts on Cumana SCSI 2 card
102 * Params : ec - expansion card structure
103 * : irqnr - interrupt number
106 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
108 struct cumanascsi2_info *info = ec->irq_data;
109 writeb(ALATCH_DIS_INT, info->base + CUMANASCSI2_ALATCH);
112 static const expansioncard_ops_t cumanascsi_2_ops = {
113 .irqenable = cumanascsi_2_irqenable,
114 .irqdisable = cumanascsi_2_irqdisable,
117 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
118 * Purpose : Turn the Cumana SCSI 2 terminators on or off
119 * Params : host - card to turn on/off
120 * : on_off - !0 to turn on, 0 to turn off
123 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
125 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
129 writeb(ALATCH_ENA_TERM, info->base + CUMANASCSI2_ALATCH);
132 writeb(ALATCH_DIS_TERM, info->base + CUMANASCSI2_ALATCH);
136 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
137 * Purpose : handle interrupts from Cumana SCSI 2 card
138 * Params : irq - interrupt number
139 * dev_id - user-defined (Scsi_Host structure)
142 cumanascsi_2_intr(int irq, void *dev_id)
144 struct cumanascsi2_info *info = dev_id;
146 return fas216_intr(&info->info);
149 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
150 * Purpose : initialises DMA/PIO
151 * Params : host - host
153 * direction - DMA on to/off of card
154 * min_type - minimum DMA support that we must have for this transfer
155 * Returns : type of transfer to be performed
158 cumanascsi_2_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
159 fasdmadir_t direction, fasdmatype_t min_type)
161 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
162 struct device *dev = scsi_get_device(host);
163 int dmach = info->info.scsi.dma;
165 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
167 if (dmach != NO_DMA &&
168 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
169 int bufs, map_dir, dma_dir, alatch_dir;
171 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
173 if (direction == DMA_OUT)
174 map_dir = DMA_TO_DEVICE,
175 dma_dir = DMA_MODE_WRITE,
176 alatch_dir = ALATCH_DMA_OUT;
178 map_dir = DMA_FROM_DEVICE,
179 dma_dir = DMA_MODE_READ,
180 alatch_dir = ALATCH_DMA_IN;
182 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
185 set_dma_sg(dmach, info->sg, bufs + 1);
186 writeb(alatch_dir, info->base + CUMANASCSI2_ALATCH);
187 set_dma_mode(dmach, dma_dir);
189 writeb(ALATCH_ENA_DMA, info->base + CUMANASCSI2_ALATCH);
190 writeb(ALATCH_DIS_BIT32, info->base + CUMANASCSI2_ALATCH);
191 return fasdma_real_all;
195 * If we're not doing DMA,
196 * we'll do pseudo DMA
202 * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
203 * Purpose : handles pseudo DMA
204 * Params : host - host
206 * direction - DMA on to/off of card
207 * transfer - minimum number of bytes we expect to transfer
210 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
211 fasdmadir_t direction, int transfer)
213 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
217 length = SCp->this_residual;
220 if (direction == DMA_OUT)
224 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
226 if (status & STATUS_INT)
229 if (!(status & STATUS_DRQ))
232 word = *addr | *(addr + 1) << 8;
233 writew(word, info->base + CUMANASCSI2_PSEUDODMA);
238 printk ("PSEUDO_OUT???\n");
241 if (transfer && (transfer & 255)) {
242 while (length >= 256) {
243 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
245 if (status & STATUS_INT)
248 if (!(status & STATUS_DRQ))
251 readsw(info->base + CUMANASCSI2_PSEUDODMA,
260 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
262 if (status & STATUS_INT)
265 if (!(status & STATUS_DRQ))
268 word = readw(info->base + CUMANASCSI2_PSEUDODMA);
278 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
279 * Purpose : stops DMA/PIO
280 * Params : host - host
284 cumanascsi_2_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
286 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
287 if (info->info.scsi.dma != NO_DMA) {
288 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
289 disable_dma(info->info.scsi.dma);
293 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
294 * Purpose : returns a descriptive string about this interface,
295 * Params : host - driver host structure to return info for.
296 * Returns : pointer to a static buffer containing null terminated string.
298 const char *cumanascsi_2_info(struct Scsi_Host *host)
300 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
301 static char string[150];
303 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
304 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
305 VERSION, info->terms ? "n" : "ff");
310 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
311 * Purpose : Set a driver specific function
312 * Params : host - host to setup
313 * : buffer - buffer containing string describing operation
314 * : length - length of string
315 * Returns : -EINVAL, or 0
318 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
322 if (length >= 11 && strcmp(buffer, "CUMANASCSI2") == 0) {
326 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
327 if (buffer[5] == '1')
328 cumanascsi_2_terminator_ctl(host, 1);
329 else if (buffer[5] == '0')
330 cumanascsi_2_terminator_ctl(host, 0);
341 /* Prototype: int cumanascsi_2_proc_info(char *buffer, char **start, off_t offset,
342 * int length, int host_no, int inout)
343 * Purpose : Return information about the driver to a user process accessing
344 * the /proc filesystem.
345 * Params : buffer - a buffer to write information to
346 * start - a pointer into this buffer set by this routine to the start
347 * of the required information.
348 * offset - offset into information that we have read upto.
349 * length - length of buffer
350 * host_no - host number to return information for
351 * inout - 0 for reading, 1 for writing.
352 * Returns : length of data written to buffer.
354 int cumanascsi_2_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset,
355 int length, int inout)
357 struct cumanascsi2_info *info;
362 return cumanascsi_2_set_proc_info(host, buffer, length);
364 info = (struct cumanascsi2_info *)host->hostdata;
366 p += sprintf(p, "Cumana SCSI II driver v%s\n", VERSION);
367 p += fas216_print_host(&info->info, p);
368 p += sprintf(p, "Term : o%s\n",
369 info->terms ? "n" : "ff");
371 p += fas216_print_stats(&info->info, p);
372 p += fas216_print_devices(&info->info, p);
374 *start = buffer + offset;
375 pos = p - buffer - offset;
382 static struct scsi_host_template cumanascsi2_template = {
383 .module = THIS_MODULE,
384 .proc_info = cumanascsi_2_proc_info,
385 .name = "Cumana SCSI II",
386 .info = cumanascsi_2_info,
387 .queuecommand = fas216_queue_command,
388 .eh_host_reset_handler = fas216_eh_host_reset,
389 .eh_bus_reset_handler = fas216_eh_bus_reset,
390 .eh_device_reset_handler = fas216_eh_device_reset,
391 .eh_abort_handler = fas216_eh_abort,
394 .sg_tablesize = SG_ALL,
396 .use_clustering = DISABLE_CLUSTERING,
397 .proc_name = "cumanascsi2",
401 cumanascsi2_probe(struct expansion_card *ec, const struct ecard_id *id)
403 struct Scsi_Host *host;
404 struct cumanascsi2_info *info;
405 unsigned long resbase, reslen;
409 ret = ecard_request_resources(ec);
413 resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
414 reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
415 base = ioremap(resbase, reslen);
421 host = scsi_host_alloc(&cumanascsi2_template,
422 sizeof(struct cumanascsi2_info));
428 ecard_set_drvdata(ec, host);
430 info = (struct cumanascsi2_info *)host->hostdata;
434 cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
436 info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
437 info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
438 info->info.scsi.irq = ec->irq;
439 info->info.scsi.dma = ec->dma;
440 info->info.ifcfg.clockrate = 40; /* MHz */
441 info->info.ifcfg.select_timeout = 255;
442 info->info.ifcfg.asyncperiod = 200; /* ns */
443 info->info.ifcfg.sync_max_depth = 7;
444 info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
445 info->info.ifcfg.disconnect_ok = 1;
446 info->info.ifcfg.wide_max_size = 0;
447 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
448 info->info.dma.setup = cumanascsi_2_dma_setup;
449 info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
450 info->info.dma.stop = cumanascsi_2_dma_stop;
452 ec->irqaddr = info->base + CUMANASCSI2_STATUS;
453 ec->irqmask = STATUS_INT;
455 ec->ops = &cumanascsi_2_ops;
457 ret = fas216_init(host);
461 ret = request_irq(ec->irq, cumanascsi_2_intr,
462 IRQF_DISABLED, "cumanascsi2", info);
464 printk("scsi%d: IRQ%d not free: %d\n",
465 host->host_no, ec->irq, ret);
469 if (info->info.scsi.dma != NO_DMA) {
470 if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
471 printk("scsi%d: DMA%d not free, using PIO\n",
472 host->host_no, info->info.scsi.dma);
473 info->info.scsi.dma = NO_DMA;
475 set_dma_speed(info->info.scsi.dma, 180);
476 info->info.ifcfg.capabilities |= FASCAP_DMA;
480 ret = fas216_add(host, &ec->dev);
484 if (info->info.scsi.dma != NO_DMA)
485 free_dma(info->info.scsi.dma);
486 free_irq(ec->irq, host);
489 fas216_release(host);
498 ecard_release_resources(ec);
504 static void __devexit cumanascsi2_remove(struct expansion_card *ec)
506 struct Scsi_Host *host = ecard_get_drvdata(ec);
507 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
509 ecard_set_drvdata(ec, NULL);
512 if (info->info.scsi.dma != NO_DMA)
513 free_dma(info->info.scsi.dma);
514 free_irq(ec->irq, info);
518 fas216_release(host);
520 ecard_release_resources(ec);
523 static const struct ecard_id cumanascsi2_cids[] = {
524 { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
528 static struct ecard_driver cumanascsi2_driver = {
529 .probe = cumanascsi2_probe,
530 .remove = __devexit_p(cumanascsi2_remove),
531 .id_table = cumanascsi2_cids,
533 .name = "cumanascsi2",
537 static int __init cumanascsi2_init(void)
539 return ecard_register_driver(&cumanascsi2_driver);
542 static void __exit cumanascsi2_exit(void)
544 ecard_remove_driver(&cumanascsi2_driver);
547 module_init(cumanascsi2_init);
548 module_exit(cumanascsi2_exit);
550 MODULE_AUTHOR("Russell King");
551 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
552 module_param_array(term, int, NULL, 0);
553 MODULE_PARM_DESC(term, "SCSI bus termination");
554 MODULE_LICENSE("GPL");