1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/acorn/scsi/cumana_2.c
5 * Copyright (C) 1997-2005 Russell King
8 * 30-08-1997 RMK 0.0.0 Created, READONLY version.
9 * 22-01-1998 RMK 0.0.1 Updated to 2.1.80.
10 * 15-04-1998 RMK 0.0.1 Only do PIO if FAS216 will allow it.
11 * 02-05-1998 RMK 0.0.2 Updated & added DMA support.
12 * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
13 * 18-08-1998 RMK 0.0.3 Fixed synchronous transfer depth.
14 * 02-04-2000 RMK 0.0.4 Updated for new error handling code.
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/ioport.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/pgtable.h>
29 #include <asm/ecard.h>
33 #include <scsi/scsi_host.h>
37 #include <scsi/scsicam.h>
39 #define CUMANASCSI2_STATUS (0x0000)
40 #define STATUS_INT (1 << 0)
41 #define STATUS_DRQ (1 << 1)
42 #define STATUS_LATCHED (1 << 3)
44 #define CUMANASCSI2_ALATCH (0x0014)
45 #define ALATCH_ENA_INT (3)
46 #define ALATCH_DIS_INT (2)
47 #define ALATCH_ENA_TERM (5)
48 #define ALATCH_DIS_TERM (4)
49 #define ALATCH_ENA_BIT32 (11)
50 #define ALATCH_DIS_BIT32 (10)
51 #define ALATCH_ENA_DMA (13)
52 #define ALATCH_DIS_DMA (12)
53 #define ALATCH_DMA_OUT (15)
54 #define ALATCH_DMA_IN (14)
56 #define CUMANASCSI2_PSEUDODMA (0x0200)
58 #define CUMANASCSI2_FAS216_OFFSET (0x0300)
59 #define CUMANASCSI2_FAS216_SHIFT 2
64 #define VERSION "1.00 (13/11/2002 2.5.47)"
67 * Use term=0,1,0,0,0 to turn terminators on/off
69 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
73 struct cumanascsi2_info {
75 struct expansion_card *ec;
77 unsigned int terms; /* Terminator state */
78 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
81 #define CSTATUS_IRQ (1 << 0)
82 #define CSTATUS_DRQ (1 << 1)
84 /* Prototype: void cumanascsi_2_irqenable(ec, irqnr)
85 * Purpose : Enable interrupts on Cumana SCSI 2 card
86 * Params : ec - expansion card structure
87 * : irqnr - interrupt number
90 cumanascsi_2_irqenable(struct expansion_card *ec, int irqnr)
92 struct cumanascsi2_info *info = ec->irq_data;
93 writeb(ALATCH_ENA_INT, info->base + CUMANASCSI2_ALATCH);
96 /* Prototype: void cumanascsi_2_irqdisable(ec, irqnr)
97 * Purpose : Disable interrupts on Cumana SCSI 2 card
98 * Params : ec - expansion card structure
99 * : irqnr - interrupt number
102 cumanascsi_2_irqdisable(struct expansion_card *ec, int irqnr)
104 struct cumanascsi2_info *info = ec->irq_data;
105 writeb(ALATCH_DIS_INT, info->base + CUMANASCSI2_ALATCH);
108 static const expansioncard_ops_t cumanascsi_2_ops = {
109 .irqenable = cumanascsi_2_irqenable,
110 .irqdisable = cumanascsi_2_irqdisable,
113 /* Prototype: void cumanascsi_2_terminator_ctl(host, on_off)
114 * Purpose : Turn the Cumana SCSI 2 terminators on or off
115 * Params : host - card to turn on/off
116 * : on_off - !0 to turn on, 0 to turn off
119 cumanascsi_2_terminator_ctl(struct Scsi_Host *host, int on_off)
121 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
125 writeb(ALATCH_ENA_TERM, info->base + CUMANASCSI2_ALATCH);
128 writeb(ALATCH_DIS_TERM, info->base + CUMANASCSI2_ALATCH);
132 /* Prototype: void cumanascsi_2_intr(irq, *dev_id, *regs)
133 * Purpose : handle interrupts from Cumana SCSI 2 card
134 * Params : irq - interrupt number
135 * dev_id - user-defined (Scsi_Host structure)
138 cumanascsi_2_intr(int irq, void *dev_id)
140 struct cumanascsi2_info *info = dev_id;
142 return fas216_intr(&info->info);
145 /* Prototype: fasdmatype_t cumanascsi_2_dma_setup(host, SCpnt, direction, min_type)
146 * Purpose : initialises DMA/PIO
147 * Params : host - host
149 * direction - DMA on to/off of card
150 * min_type - minimum DMA support that we must have for this transfer
151 * Returns : type of transfer to be performed
154 cumanascsi_2_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
155 fasdmadir_t direction, fasdmatype_t min_type)
157 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
158 struct device *dev = scsi_get_device(host);
159 int dmach = info->info.scsi.dma;
161 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
163 if (dmach != NO_DMA &&
164 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
165 int bufs, map_dir, dma_dir, alatch_dir;
167 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
169 if (direction == DMA_OUT) {
170 map_dir = DMA_TO_DEVICE;
171 dma_dir = DMA_MODE_WRITE;
172 alatch_dir = ALATCH_DMA_OUT;
174 map_dir = DMA_FROM_DEVICE;
175 dma_dir = DMA_MODE_READ;
176 alatch_dir = ALATCH_DMA_IN;
179 dma_map_sg(dev, info->sg, bufs, map_dir);
182 set_dma_sg(dmach, info->sg, bufs);
183 writeb(alatch_dir, info->base + CUMANASCSI2_ALATCH);
184 set_dma_mode(dmach, dma_dir);
186 writeb(ALATCH_ENA_DMA, info->base + CUMANASCSI2_ALATCH);
187 writeb(ALATCH_DIS_BIT32, info->base + CUMANASCSI2_ALATCH);
188 return fasdma_real_all;
192 * If we're not doing DMA,
193 * we'll do pseudo DMA
199 * Prototype: void cumanascsi_2_dma_pseudo(host, SCpnt, direction, transfer)
200 * Purpose : handles pseudo DMA
201 * Params : host - host
203 * direction - DMA on to/off of card
204 * transfer - minimum number of bytes we expect to transfer
207 cumanascsi_2_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
208 fasdmadir_t direction, int transfer)
210 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
214 length = SCp->this_residual;
217 if (direction == DMA_OUT)
221 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
223 if (status & STATUS_INT)
226 if (!(status & STATUS_DRQ))
229 word = *addr | *(addr + 1) << 8;
230 writew(word, info->base + CUMANASCSI2_PSEUDODMA);
235 printk ("PSEUDO_OUT???\n");
238 if (transfer && (transfer & 255)) {
239 while (length >= 256) {
240 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
242 if (status & STATUS_INT)
245 if (!(status & STATUS_DRQ))
248 readsw(info->base + CUMANASCSI2_PSEUDODMA,
257 unsigned int status = readb(info->base + CUMANASCSI2_STATUS);
259 if (status & STATUS_INT)
262 if (!(status & STATUS_DRQ))
265 word = readw(info->base + CUMANASCSI2_PSEUDODMA);
275 /* Prototype: int cumanascsi_2_dma_stop(host, SCpnt)
276 * Purpose : stops DMA/PIO
277 * Params : host - host
281 cumanascsi_2_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
283 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
284 if (info->info.scsi.dma != NO_DMA) {
285 writeb(ALATCH_DIS_DMA, info->base + CUMANASCSI2_ALATCH);
286 disable_dma(info->info.scsi.dma);
290 /* Prototype: const char *cumanascsi_2_info(struct Scsi_Host * host)
291 * Purpose : returns a descriptive string about this interface,
292 * Params : host - driver host structure to return info for.
293 * Returns : pointer to a static buffer containing null terminated string.
295 const char *cumanascsi_2_info(struct Scsi_Host *host)
297 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
298 static char string[150];
300 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
301 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
302 VERSION, info->terms ? "n" : "ff");
307 /* Prototype: int cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
308 * Purpose : Set a driver specific function
309 * Params : host - host to setup
310 * : buffer - buffer containing string describing operation
311 * : length - length of string
312 * Returns : -EINVAL, or 0
315 cumanascsi_2_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
319 if (length >= 11 && strncmp(buffer, "CUMANASCSI2", 11) == 0) {
323 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
324 if (buffer[5] == '1')
325 cumanascsi_2_terminator_ctl(host, 1);
326 else if (buffer[5] == '0')
327 cumanascsi_2_terminator_ctl(host, 0);
340 static int cumanascsi_2_show_info(struct seq_file *m, struct Scsi_Host *host)
342 struct cumanascsi2_info *info;
343 info = (struct cumanascsi2_info *)host->hostdata;
345 seq_printf(m, "Cumana SCSI II driver v%s\n", VERSION);
346 fas216_print_host(&info->info, m);
347 seq_printf(m, "Term : o%s\n",
348 info->terms ? "n" : "ff");
350 fas216_print_stats(&info->info, m);
351 fas216_print_devices(&info->info, m);
355 static struct scsi_host_template cumanascsi2_template = {
356 .module = THIS_MODULE,
357 .show_info = cumanascsi_2_show_info,
358 .write_info = cumanascsi_2_set_proc_info,
359 .name = "Cumana SCSI II",
360 .info = cumanascsi_2_info,
361 .queuecommand = fas216_queue_command,
362 .eh_host_reset_handler = fas216_eh_host_reset,
363 .eh_bus_reset_handler = fas216_eh_bus_reset,
364 .eh_device_reset_handler = fas216_eh_device_reset,
365 .eh_abort_handler = fas216_eh_abort,
368 .sg_tablesize = SG_MAX_SEGMENTS,
369 .dma_boundary = IOMD_DMA_BOUNDARY,
370 .proc_name = "cumanascsi2",
373 static int cumanascsi2_probe(struct expansion_card *ec,
374 const struct ecard_id *id)
376 struct Scsi_Host *host;
377 struct cumanascsi2_info *info;
381 ret = ecard_request_resources(ec);
385 base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
391 host = scsi_host_alloc(&cumanascsi2_template,
392 sizeof(struct cumanascsi2_info));
398 ecard_set_drvdata(ec, host);
400 info = (struct cumanascsi2_info *)host->hostdata;
404 cumanascsi_2_terminator_ctl(host, term[ec->slot_no]);
406 info->info.scsi.io_base = base + CUMANASCSI2_FAS216_OFFSET;
407 info->info.scsi.io_shift = CUMANASCSI2_FAS216_SHIFT;
408 info->info.scsi.irq = ec->irq;
409 info->info.scsi.dma = ec->dma;
410 info->info.ifcfg.clockrate = 40; /* MHz */
411 info->info.ifcfg.select_timeout = 255;
412 info->info.ifcfg.asyncperiod = 200; /* ns */
413 info->info.ifcfg.sync_max_depth = 7;
414 info->info.ifcfg.cntl3 = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
415 info->info.ifcfg.disconnect_ok = 1;
416 info->info.ifcfg.wide_max_size = 0;
417 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
418 info->info.dma.setup = cumanascsi_2_dma_setup;
419 info->info.dma.pseudo = cumanascsi_2_dma_pseudo;
420 info->info.dma.stop = cumanascsi_2_dma_stop;
422 ec->irqaddr = info->base + CUMANASCSI2_STATUS;
423 ec->irqmask = STATUS_INT;
425 ecard_setirq(ec, &cumanascsi_2_ops, info);
427 ret = fas216_init(host);
431 ret = request_irq(ec->irq, cumanascsi_2_intr,
432 0, "cumanascsi2", info);
434 printk("scsi%d: IRQ%d not free: %d\n",
435 host->host_no, ec->irq, ret);
439 if (info->info.scsi.dma != NO_DMA) {
440 if (request_dma(info->info.scsi.dma, "cumanascsi2")) {
441 printk("scsi%d: DMA%d not free, using PIO\n",
442 host->host_no, info->info.scsi.dma);
443 info->info.scsi.dma = NO_DMA;
445 set_dma_speed(info->info.scsi.dma, 180);
446 info->info.ifcfg.capabilities |= FASCAP_DMA;
450 ret = fas216_add(host, &ec->dev);
454 if (info->info.scsi.dma != NO_DMA)
455 free_dma(info->info.scsi.dma);
456 free_irq(ec->irq, info);
459 fas216_release(host);
465 ecard_release_resources(ec);
471 static void cumanascsi2_remove(struct expansion_card *ec)
473 struct Scsi_Host *host = ecard_get_drvdata(ec);
474 struct cumanascsi2_info *info = (struct cumanascsi2_info *)host->hostdata;
476 ecard_set_drvdata(ec, NULL);
479 if (info->info.scsi.dma != NO_DMA)
480 free_dma(info->info.scsi.dma);
481 free_irq(ec->irq, info);
483 fas216_release(host);
485 ecard_release_resources(ec);
488 static const struct ecard_id cumanascsi2_cids[] = {
489 { MANU_CUMANA, PROD_CUMANA_SCSI_2 },
493 static struct ecard_driver cumanascsi2_driver = {
494 .probe = cumanascsi2_probe,
495 .remove = cumanascsi2_remove,
496 .id_table = cumanascsi2_cids,
498 .name = "cumanascsi2",
502 static int __init cumanascsi2_init(void)
504 return ecard_register_driver(&cumanascsi2_driver);
507 static void __exit cumanascsi2_exit(void)
509 ecard_remove_driver(&cumanascsi2_driver);
512 module_init(cumanascsi2_init);
513 module_exit(cumanascsi2_exit);
515 MODULE_AUTHOR("Russell King");
516 MODULE_DESCRIPTION("Cumana SCSI-2 driver for Acorn machines");
517 module_param_array(term, int, NULL, 0);
518 MODULE_PARM_DESC(term, "SCSI bus termination");
519 MODULE_LICENSE("GPL");