1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/acorn/scsi/eesox.c
5 * Copyright (C) 1997-2005 Russell King
7 * This driver is based on experimentation. Hence, it may have made
8 * assumptions about the particular card that I have available, and
12 * 01-10-1997 RMK Created, READONLY version
13 * 15-02-1998 RMK READ/WRITE version
14 * added DMA support and hardware definitions
15 * 14-03-1998 RMK Updated DMA support
16 * Added terminator control
17 * 15-04-1998 RMK Only do PIO if FAS216 will allow it.
18 * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
19 * 02-04-2000 RMK 0.0.3 Fixed NO_IRQ/NO_DMA problem, updated for new
20 * error handling code.
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/ioport.h>
27 #include <linux/proc_fs.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/init.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/pgtable.h>
36 #include <asm/ecard.h>
39 #include <scsi/scsi_host.h>
43 #include <scsi/scsicam.h>
45 #define EESOX_FAS216_OFFSET 0x3000
46 #define EESOX_FAS216_SHIFT 5
48 #define EESOX_DMASTAT 0x2800
49 #define EESOX_STAT_INTR 0x01
50 #define EESOX_STAT_DMA 0x02
52 #define EESOX_CONTROL 0x2800
53 #define EESOX_INTR_ENABLE 0x04
54 #define EESOX_TERM_ENABLE 0x02
55 #define EESOX_RESET 0x01
57 #define EESOX_DMADATA 0x3800
59 #define VERSION "1.10 (17/01/2003 2.5.59)"
62 * Use term=0,1,0,0,0 to turn terminators on/off
64 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
68 struct eesoxscsi_info {
70 struct expansion_card *ec;
72 void __iomem *ctl_port;
74 struct scatterlist sg[NR_SG]; /* Scatter DMA list */
77 /* Prototype: void eesoxscsi_irqenable(ec, irqnr)
78 * Purpose : Enable interrupts on EESOX SCSI card
79 * Params : ec - expansion card structure
80 * : irqnr - interrupt number
83 eesoxscsi_irqenable(struct expansion_card *ec, int irqnr)
85 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
87 info->control |= EESOX_INTR_ENABLE;
89 writeb(info->control, info->ctl_port);
92 /* Prototype: void eesoxscsi_irqdisable(ec, irqnr)
93 * Purpose : Disable interrupts on EESOX SCSI card
94 * Params : ec - expansion card structure
95 * : irqnr - interrupt number
98 eesoxscsi_irqdisable(struct expansion_card *ec, int irqnr)
100 struct eesoxscsi_info *info = (struct eesoxscsi_info *)ec->irq_data;
102 info->control &= ~EESOX_INTR_ENABLE;
104 writeb(info->control, info->ctl_port);
107 static const expansioncard_ops_t eesoxscsi_ops = {
108 .irqenable = eesoxscsi_irqenable,
109 .irqdisable = eesoxscsi_irqdisable,
112 /* Prototype: void eesoxscsi_terminator_ctl(*host, on_off)
113 * Purpose : Turn the EESOX SCSI terminators on or off
114 * Params : host - card to turn on/off
115 * : on_off - !0 to turn on, 0 to turn off
118 eesoxscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
120 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
123 spin_lock_irqsave(host->host_lock, flags);
125 info->control |= EESOX_TERM_ENABLE;
127 info->control &= ~EESOX_TERM_ENABLE;
129 writeb(info->control, info->ctl_port);
130 spin_unlock_irqrestore(host->host_lock, flags);
133 /* Prototype: void eesoxscsi_intr(irq, *dev_id, *regs)
134 * Purpose : handle interrupts from EESOX SCSI card
135 * Params : irq - interrupt number
136 * dev_id - user-defined (Scsi_Host structure)
139 eesoxscsi_intr(int irq, void *dev_id)
141 struct eesoxscsi_info *info = dev_id;
143 return fas216_intr(&info->info);
146 /* Prototype: fasdmatype_t eesoxscsi_dma_setup(host, SCpnt, direction, min_type)
147 * Purpose : initialises DMA/PIO
148 * Params : host - host
150 * direction - DMA on to/off of card
151 * min_type - minimum DMA support that we must have for this transfer
152 * Returns : type of transfer to be performed
155 eesoxscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
156 fasdmadir_t direction, fasdmatype_t min_type)
158 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
159 struct device *dev = scsi_get_device(host);
160 int dmach = info->info.scsi.dma;
162 if (dmach != NO_DMA &&
163 (min_type == fasdma_real_all || SCp->this_residual >= 512)) {
164 int bufs, map_dir, dma_dir;
166 bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
168 if (direction == DMA_OUT) {
169 map_dir = DMA_TO_DEVICE;
170 dma_dir = DMA_MODE_WRITE;
172 map_dir = DMA_FROM_DEVICE;
173 dma_dir = DMA_MODE_READ;
176 dma_map_sg(dev, info->sg, bufs, map_dir);
179 set_dma_sg(dmach, info->sg, bufs);
180 set_dma_mode(dmach, dma_dir);
182 return fasdma_real_all;
185 * We don't do DMA, we only do slow PIO
187 * Some day, we will do Pseudo DMA
189 return fasdma_pseudo;
192 static void eesoxscsi_buffer_in(void *buf, int length, void __iomem *base)
194 const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
195 const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
196 const void __iomem *reg_dmadata = base + EESOX_DMADATA;
197 register const unsigned long mask = 0xffff;
205 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
206 if (status & STAT_INT)
210 * DMA request active?
212 status = readb(reg_dmastat);
213 if (!(status & EESOX_STAT_DMA))
217 * Get number of bytes in FIFO
219 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
228 if (((u32)buf) & 2 && status >= 2) {
229 *(u16 *)buf = readl(reg_dmadata);
236 unsigned long l1, l2;
238 l1 = readl(reg_dmadata) & mask;
239 l1 |= readl(reg_dmadata) << 16;
240 l2 = readl(reg_dmadata) & mask;
241 l2 |= readl(reg_dmadata) << 16;
253 l1 = readl(reg_dmadata) & mask;
254 l1 |= readl(reg_dmadata) << 16;
263 *(u16 *)buf = readl(reg_dmadata);
270 static void eesoxscsi_buffer_out(void *buf, int length, void __iomem *base)
272 const void __iomem *reg_fas = base + EESOX_FAS216_OFFSET;
273 const void __iomem *reg_dmastat = base + EESOX_DMASTAT;
274 void __iomem *reg_dmadata = base + EESOX_DMADATA;
282 status = readb(reg_fas + (REG_STAT << EESOX_FAS216_SHIFT));
283 if (status & STAT_INT)
287 * DMA request active?
289 status = readb(reg_dmastat);
290 if (!(status & EESOX_STAT_DMA))
294 * Get number of bytes in FIFO
296 status = readb(reg_fas + (REG_CFIS << EESOX_FAS216_SHIFT)) & CFIS_CF;
299 status = 16 - status;
307 if (((u32)buf) & 2 && status >= 2) {
308 writel(*(u16 *)buf << 16, reg_dmadata);
315 unsigned long l1, l2;
322 writel(l1 << 16, reg_dmadata);
323 writel(l1, reg_dmadata);
324 writel(l2 << 16, reg_dmadata);
325 writel(l2, reg_dmadata);
336 writel(l1 << 16, reg_dmadata);
337 writel(l1, reg_dmadata);
343 writel(*(u16 *)buf << 16, reg_dmadata);
351 eesoxscsi_dma_pseudo(struct Scsi_Host *host, struct scsi_pointer *SCp,
352 fasdmadir_t dir, int transfer_size)
354 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
356 eesoxscsi_buffer_in(SCp->ptr, SCp->this_residual, info->base);
358 eesoxscsi_buffer_out(SCp->ptr, SCp->this_residual, info->base);
362 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
363 * Purpose : stops DMA/PIO
364 * Params : host - host
368 eesoxscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
370 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
371 if (info->info.scsi.dma != NO_DMA)
372 disable_dma(info->info.scsi.dma);
375 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
376 * Purpose : returns a descriptive string about this interface,
377 * Params : host - driver host structure to return info for.
378 * Returns : pointer to a static buffer containing null terminated string.
380 const char *eesoxscsi_info(struct Scsi_Host *host)
382 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
383 static char string[150];
385 sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
386 host->hostt->name, info->info.scsi.type, info->ec->slot_no,
387 VERSION, info->control & EESOX_TERM_ENABLE ? "n" : "ff");
392 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
393 * Purpose : Set a driver specific function
394 * Params : host - host to setup
395 * : buffer - buffer containing string describing operation
396 * : length - length of string
397 * Returns : -EINVAL, or 0
400 eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
404 if (length >= 9 && strncmp(buffer, "EESOXSCSI", 9) == 0) {
408 if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
409 if (buffer[5] == '1')
410 eesoxscsi_terminator_ctl(host, 1);
411 else if (buffer[5] == '0')
412 eesoxscsi_terminator_ctl(host, 0);
423 static int eesoxscsi_show_info(struct seq_file *m, struct Scsi_Host *host)
425 struct eesoxscsi_info *info;
427 info = (struct eesoxscsi_info *)host->hostdata;
429 seq_printf(m, "EESOX SCSI driver v%s\n", VERSION);
430 fas216_print_host(&info->info, m);
431 seq_printf(m, "Term : o%s\n",
432 info->control & EESOX_TERM_ENABLE ? "n" : "ff");
434 fas216_print_stats(&info->info, m);
435 fas216_print_devices(&info->info, m);
439 static ssize_t eesoxscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
441 struct expansion_card *ec = ECARD_DEV(dev);
442 struct Scsi_Host *host = ecard_get_drvdata(ec);
443 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
445 return sprintf(buf, "%d\n", info->control & EESOX_TERM_ENABLE ? 1 : 0);
448 static ssize_t eesoxscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
450 struct expansion_card *ec = ECARD_DEV(dev);
451 struct Scsi_Host *host = ecard_get_drvdata(ec);
452 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
456 spin_lock_irqsave(host->host_lock, flags);
458 info->control |= EESOX_TERM_ENABLE;
460 info->control &= ~EESOX_TERM_ENABLE;
462 writeb(info->control, info->ctl_port);
463 spin_unlock_irqrestore(host->host_lock, flags);
469 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
470 eesoxscsi_show_term, eesoxscsi_store_term);
472 static struct scsi_host_template eesox_template = {
473 .module = THIS_MODULE,
474 .show_info = eesoxscsi_show_info,
475 .write_info = eesoxscsi_set_proc_info,
476 .name = "EESOX SCSI",
477 .info = eesoxscsi_info,
478 .queuecommand = fas216_queue_command,
479 .eh_host_reset_handler = fas216_eh_host_reset,
480 .eh_bus_reset_handler = fas216_eh_bus_reset,
481 .eh_device_reset_handler = fas216_eh_device_reset,
482 .eh_abort_handler = fas216_eh_abort,
483 .cmd_size = sizeof(struct fas216_cmd_priv),
486 .sg_tablesize = SG_MAX_SEGMENTS,
487 .dma_boundary = IOMD_DMA_BOUNDARY,
488 .proc_name = "eesox",
491 static int eesoxscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
493 struct Scsi_Host *host;
494 struct eesoxscsi_info *info;
498 ret = ecard_request_resources(ec);
502 base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
508 host = scsi_host_alloc(&eesox_template,
509 sizeof(struct eesoxscsi_info));
515 ecard_set_drvdata(ec, host);
517 info = (struct eesoxscsi_info *)host->hostdata;
520 info->ctl_port = base + EESOX_CONTROL;
521 info->control = term[ec->slot_no] ? EESOX_TERM_ENABLE : 0;
522 writeb(info->control, info->ctl_port);
524 info->info.scsi.io_base = base + EESOX_FAS216_OFFSET;
525 info->info.scsi.io_shift = EESOX_FAS216_SHIFT;
526 info->info.scsi.irq = ec->irq;
527 info->info.scsi.dma = ec->dma;
528 info->info.ifcfg.clockrate = 40; /* MHz */
529 info->info.ifcfg.select_timeout = 255;
530 info->info.ifcfg.asyncperiod = 200; /* ns */
531 info->info.ifcfg.sync_max_depth = 7;
532 info->info.ifcfg.cntl3 = CNTL3_FASTSCSI | CNTL3_FASTCLK;
533 info->info.ifcfg.disconnect_ok = 1;
534 info->info.ifcfg.wide_max_size = 0;
535 info->info.ifcfg.capabilities = FASCAP_PSEUDODMA;
536 info->info.dma.setup = eesoxscsi_dma_setup;
537 info->info.dma.pseudo = eesoxscsi_dma_pseudo;
538 info->info.dma.stop = eesoxscsi_dma_stop;
540 ec->irqaddr = base + EESOX_DMASTAT;
541 ec->irqmask = EESOX_STAT_INTR;
543 ecard_setirq(ec, &eesoxscsi_ops, info);
545 device_create_file(&ec->dev, &dev_attr_bus_term);
547 ret = fas216_init(host);
551 ret = request_irq(ec->irq, eesoxscsi_intr, 0, "eesoxscsi", info);
553 printk("scsi%d: IRQ%d not free: %d\n",
554 host->host_no, ec->irq, ret);
558 if (info->info.scsi.dma != NO_DMA) {
559 if (request_dma(info->info.scsi.dma, "eesox")) {
560 printk("scsi%d: DMA%d not free, DMA disabled\n",
561 host->host_no, info->info.scsi.dma);
562 info->info.scsi.dma = NO_DMA;
564 set_dma_speed(info->info.scsi.dma, 180);
565 info->info.ifcfg.capabilities |= FASCAP_DMA;
566 info->info.ifcfg.cntl3 |= CNTL3_BS8;
570 ret = fas216_add(host, &ec->dev);
574 if (info->info.scsi.dma != NO_DMA)
575 free_dma(info->info.scsi.dma);
576 free_irq(ec->irq, info);
582 device_remove_file(&ec->dev, &dev_attr_bus_term);
586 ecard_release_resources(ec);
592 static void eesoxscsi_remove(struct expansion_card *ec)
594 struct Scsi_Host *host = ecard_get_drvdata(ec);
595 struct eesoxscsi_info *info = (struct eesoxscsi_info *)host->hostdata;
597 ecard_set_drvdata(ec, NULL);
600 if (info->info.scsi.dma != NO_DMA)
601 free_dma(info->info.scsi.dma);
602 free_irq(ec->irq, info);
604 device_remove_file(&ec->dev, &dev_attr_bus_term);
606 fas216_release(host);
608 ecard_release_resources(ec);
611 static const struct ecard_id eesoxscsi_cids[] = {
612 { MANU_EESOX, PROD_EESOX_SCSI2 },
616 static struct ecard_driver eesoxscsi_driver = {
617 .probe = eesoxscsi_probe,
618 .remove = eesoxscsi_remove,
619 .id_table = eesoxscsi_cids,
625 static int __init eesox_init(void)
627 return ecard_register_driver(&eesoxscsi_driver);
630 static void __exit eesox_exit(void)
632 ecard_remove_driver(&eesoxscsi_driver);
635 module_init(eesox_init);
636 module_exit(eesox_exit);
638 MODULE_AUTHOR("Russell King");
639 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
640 module_param_array(term, int, NULL, 0);
641 MODULE_PARM_DESC(term, "SCSI bus termination");
642 MODULE_LICENSE("GPL");