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.
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 void *status; /* card status register */
83 void *alatch; /* Control register */
84 unsigned int terms; /* Terminator state */
85 void *dmaarea; /* Pseudo DMA area */
86 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
89 #define CSTATUS_IRQ (1 << 0)
90 #define CSTATUS_DRQ (1 << 1)
92 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
93 * Purpose : Enable interrupts on Cumana SCSI 2 card
94 * Params : ec - expansion card structure
95 * : irqnr - interrupt number
98 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
100 writeb(ALATCH_ENA_INT, ec->irq_data);
103 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
104 * Purpose : Disable interrupts on Cumana SCSI 2 card
105 * Params : ec - expansion card structure
106 * : irqnr - interrupt number
109 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
111 writeb(ALATCH_DIS_INT, ec->irq_data);
114 static const expansioncard_ops_t cumanascsi_2_ops = {
115 .irqenable = cumanascsi_2_irqenable,
116 .irqdisable = cumanascsi_2_irqdisable,
119 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
120 * Purpose : Turn the Cumana SCSI 2 terminators on or off
121 * Params : host - card to turn on/off
122 * : on_off - !0 to turn on, 0 to turn off
125 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
127 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
131 writeb(ALATCH_ENA_TERM, info->alatch);
134 writeb(ALATCH_DIS_TERM, info->alatch);
138 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
139 * Purpose : handle interrupts from Cumana SCSI 2 card
140 * Params : irq - interrupt number
141 * dev_id - user-defined (Scsi_Host structure)
142 * regs - processor registers at interrupt
145 cumanascsi_2_intr(int irq, void *dev_id, struct pt_regs *regs)
147 struct cumanascsi2_info *info = dev_id;
149 return fas216_intr(&info->info);
152 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
153 * Purpose : initialises DMA/PIO
154 * Params : host - host
156 * direction - DMA on to/off of card
157 * min_type - minimum DMA support that we must have for this transfer
158 * Returns : type of transfer to be performed
161 cumanascsi_2_dma_setup(struct Scsi_Host *host, Scsi_Pointer *SCp,
162 fasdmadir_t direction, fasdmatype_t min_type)
164 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
165 struct device *dev = scsi_get_device(host);
166 int dmach = host->dma_channel;
168 writeb(ALATCH_DIS_DMA, info->alatch);
170 if (dmach != NO_DMA &&
171 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
172 int bufs, map_dir, dma_dir, alatch_dir;
174 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
176 if (direction == DMA_OUT)
177 map_dir = DMA_TO_DEVICE,
178 dma_dir = DMA_MODE_WRITE,
179 alatch_dir = ALATCH_DMA_OUT;
181 map_dir = DMA_FROM_DEVICE,
182 dma_dir = DMA_MODE_READ,
183 alatch_dir = ALATCH_DMA_IN;
185 dma_map_sg(dev, info->sg, bufs + 1, map_dir);
188 set_dma_sg(dmach, info->sg, bufs + 1);
189 writeb(alatch_dir, info->alatch);
190 set_dma_mode(dmach, dma_dir);
192 writeb(ALATCH_ENA_DMA, info->alatch);
193 writeb(ALATCH_DIS_BIT32, info->alatch);
194 return fasdma_real_all;
198 * If we're not doing DMA,
199 * we'll do pseudo DMA
205 * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
206 * Purpose : handles pseudo DMA
207 * Params : host - host
209 * direction - DMA on to/off of card
210 * transfer - minimum number of bytes we expect to transfer
213 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, Scsi_Pointer *SCp,
214 fasdmadir_t direction, int transfer)
216 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
220 length = SCp->this_residual;
223 if (direction == DMA_OUT)
227 unsigned int status = readb(info->status);
229 if (status & STATUS_INT)
232 if (!(status & STATUS_DRQ))
235 word = *addr | *(addr + 1) << 8;
236 writew(word, info->dmaarea);
241 printk ("PSEUDO_OUT???\n");
244 if (transfer && (transfer & 255)) {
245 while (length >= 256) {
246 unsigned int status = readb(info->status);
248 if (status & STATUS_INT)
251 if (!(status & STATUS_DRQ))
254 readsw(info->dmaarea, addr, 256 >> 1);
262 unsigned int status = readb(info->status);
264 if (status & STATUS_INT)
267 if (!(status & STATUS_DRQ))
270 word = readw(info->dmaarea);
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, Scsi_Pointer *SCp)
288 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
289 if (host->dma_channel != NO_DMA) {
290 writeb(ALATCH_DIS_DMA, info->alatch);
291 disable_dma(host->dma_channel);
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 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 resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
412 reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
414 if (!request_mem_region(resbase, reslen, "cumanascsi2")) {
419 base = ioremap(resbase, reslen);
425 host = scsi_host_alloc(&cumanascsi2_template,
426 sizeof(struct cumanascsi2_info));
432 host->base = (unsigned long)base;
434 host->dma_channel = ec->dma;
436 ecard_set_drvdata(ec, host);
438 info = (struct cumanascsi2_info *)host->hostdata;
440 info->dmaarea = base + CUMANASCSI2_PSEUDODMA;
441 info->status = base + CUMANASCSI2_STATUS;
442 info->alatch = base + CUMANASCSI2_ALATCH;
444 ec->irqaddr = info->status;
445 ec->irqmask = STATUS_INT;
446 ec->irq_data = base + CUMANASCSI2_ALATCH;
447 ec->ops = &cumanascsi_2_ops;
449 cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
451 info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
452 info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
453 info->info.scsi.irq = host->irq;
454 info->info.ifcfg.clockrate = 40; /* MHz */
455 info->info.ifcfg.select_timeout = 255;
456 info->info.ifcfg.asyncperiod = 200; /* ns */
457 info->info.ifcfg.sync_max_depth = 7;
458 info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
459 info->info.ifcfg.disconnect_ok = 1;
460 info->info.ifcfg.wide_max_size = 0;
461 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
462 info->info.dma.setup = cumanascsi_2_dma_setup;
463 info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
464 info->info.dma.stop = cumanascsi_2_dma_stop;
466 ret = fas216_init(host);
470 ret = request_irq(host->irq, cumanascsi_2_intr,
471 SA_INTERRUPT, "cumanascsi2", info);
473 printk("scsi%d: IRQ%d not free: %d\n",
474 host->host_no, host->irq, ret);
478 if (host->dma_channel != NO_DMA) {
479 if (request_dma(host->dma_channel, "cumanascsi2")) {
480 printk("scsi%d: DMA%d not free, using PIO\n",
481 host->host_no, host->dma_channel);
482 host->dma_channel = NO_DMA;
484 set_dma_speed(host->dma_channel, 180);
485 info->info.ifcfg.capabilities |= FASCAP_DMA;
489 ret = fas216_add(host, &ec->dev);
493 if (host->dma_channel != NO_DMA)
494 free_dma(host->dma_channel);
495 free_irq(host->irq, host);
498 fas216_release(host);
507 release_mem_region(resbase, reslen);
513 static void __devexit cumanascsi2_remove(struct expansion_card *ec)
515 struct Scsi_Host *host = ecard_get_drvdata(ec);
516 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
517 unsigned long resbase, reslen;
519 ecard_set_drvdata(ec, NULL);
522 if (host->dma_channel != NO_DMA)
523 free_dma(host->dma_channel);
524 free_irq(host->irq, info);
526 iounmap((void *)host->base);
528 resbase = ecard_resource_start(ec, ECARD_RES_MEMC);
529 reslen = ecard_resource_len(ec, ECARD_RES_MEMC);
531 release_mem_region(resbase, reslen);
533 fas216_release(host);
537 static const struct ecard_id cumanascsi2_cids[] = {
538 { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
542 static struct ecard_driver cumanascsi2_driver = {
543 .probe = cumanascsi2_probe,
544 .remove = __devexit_p(cumanascsi2_remove),
545 .id_table = cumanascsi2_cids,
547 .name = "cumanascsi2",
551 static int __init cumanascsi2_init(void)
553 return ecard_register_driver(&cumanascsi2_driver);
556 static void __exit cumanascsi2_exit(void)
558 ecard_remove_driver(&cumanascsi2_driver);
561 module_init(cumanascsi2_init);
562 module_exit(cumanascsi2_exit);
564 MODULE_AUTHOR("Russell King");
565 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
566 MODULE_PARM(term, "1-8i");
567 MODULE_PARM_DESC(term, "SCSI bus termination");
568 MODULE_LICENSE("GPL");