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>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi_tcq.h>
47 #include <scsi/scsicam.h>
49 #define EESOX_FAS216_OFFSET 0x3000
50 #define EESOX_FAS216_SHIFT 5
52 #define EESOX_DMASTAT 0x2800
53 #define EESOX_STAT_INTR 0x01
54 #define EESOX_STAT_DMA 0x02
56 #define EESOX_CONTROL 0x2800
57 #define EESOX_INTR_ENABLE 0x04
58 #define EESOX_TERM_ENABLE 0x02
59 #define EESOX_RESET 0x01
61 #define EESOX_DMADATA 0x3800
63 #define VERSION "1.10 (17/01/2003 2.5.59)"
66 * Use term=0,1,0,0,0 to turn terminators on/off
68 static int term
[MAX_ECARDS
] = { 1, 1, 1, 1, 1, 1, 1, 1 };
72 struct eesoxscsi_info
{
74 struct expansion_card
*ec
;
76 void __iomem
*ctl_port
;
78 struct scatterlist sg
[NR_SG
]; /* Scatter DMA list */
81 /* Prototype: void eesoxscsi_irqenable(ec, irqnr)
82 * Purpose : Enable interrupts on EESOX SCSI card
83 * Params : ec - expansion card structure
84 * : irqnr - interrupt number
87 eesoxscsi_irqenable(struct expansion_card
*ec
, int irqnr
)
89 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)ec
->irq_data
;
91 info
->control
|= EESOX_INTR_ENABLE
;
93 writeb(info
->control
, info
->ctl_port
);
96 /* Prototype: void eesoxscsi_irqdisable(ec, irqnr)
97 * Purpose : Disable interrupts on EESOX SCSI card
98 * Params : ec - expansion card structure
99 * : irqnr - interrupt number
102 eesoxscsi_irqdisable(struct expansion_card
*ec
, int irqnr
)
104 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)ec
->irq_data
;
106 info
->control
&= ~EESOX_INTR_ENABLE
;
108 writeb(info
->control
, info
->ctl_port
);
111 static const expansioncard_ops_t eesoxscsi_ops
= {
112 .irqenable
= eesoxscsi_irqenable
,
113 .irqdisable
= eesoxscsi_irqdisable
,
116 /* Prototype: void eesoxscsi_terminator_ctl(*host, on_off)
117 * Purpose : Turn the EESOX SCSI terminators on or off
118 * Params : host - card to turn on/off
119 * : on_off - !0 to turn on, 0 to turn off
122 eesoxscsi_terminator_ctl(struct Scsi_Host
*host
, int on_off
)
124 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
127 spin_lock_irqsave(host
->host_lock
, flags
);
129 info
->control
|= EESOX_TERM_ENABLE
;
131 info
->control
&= ~EESOX_TERM_ENABLE
;
133 writeb(info
->control
, info
->ctl_port
);
134 spin_unlock_irqrestore(host
->host_lock
, flags
);
137 /* Prototype: void eesoxscsi_intr(irq, *dev_id, *regs)
138 * Purpose : handle interrupts from EESOX SCSI card
139 * Params : irq - interrupt number
140 * dev_id - user-defined (Scsi_Host structure)
143 eesoxscsi_intr(int irq
, void *dev_id
)
145 struct eesoxscsi_info
*info
= dev_id
;
147 return fas216_intr(&info
->info
);
150 /* Prototype: fasdmatype_t eesoxscsi_dma_setup(host, SCpnt, direction, min_type)
151 * Purpose : initialises DMA/PIO
152 * Params : host - host
154 * direction - DMA on to/off of card
155 * min_type - minimum DMA support that we must have for this transfer
156 * Returns : type of transfer to be performed
159 eesoxscsi_dma_setup(struct Scsi_Host
*host
, struct scsi_pointer
*SCp
,
160 fasdmadir_t direction
, fasdmatype_t min_type
)
162 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
163 struct device
*dev
= scsi_get_device(host
);
164 int dmach
= info
->info
.scsi
.dma
;
166 if (dmach
!= NO_DMA
&&
167 (min_type
== fasdma_real_all
|| SCp
->this_residual
>= 512)) {
168 int bufs
, map_dir
, dma_dir
;
170 bufs
= copy_SCp_to_sg(&info
->sg
[0], SCp
, NR_SG
);
172 if (direction
== DMA_OUT
) {
173 map_dir
= DMA_TO_DEVICE
;
174 dma_dir
= DMA_MODE_WRITE
;
176 map_dir
= DMA_FROM_DEVICE
;
177 dma_dir
= DMA_MODE_READ
;
180 dma_map_sg(dev
, info
->sg
, bufs
, map_dir
);
183 set_dma_sg(dmach
, info
->sg
, bufs
);
184 set_dma_mode(dmach
, dma_dir
);
186 return fasdma_real_all
;
189 * We don't do DMA, we only do slow PIO
191 * Some day, we will do Pseudo DMA
193 return fasdma_pseudo
;
196 static void eesoxscsi_buffer_in(void *buf
, int length
, void __iomem
*base
)
198 const void __iomem
*reg_fas
= base
+ EESOX_FAS216_OFFSET
;
199 const void __iomem
*reg_dmastat
= base
+ EESOX_DMASTAT
;
200 const void __iomem
*reg_dmadata
= base
+ EESOX_DMADATA
;
201 register const unsigned long mask
= 0xffff;
209 status
= readb(reg_fas
+ (REG_STAT
<< EESOX_FAS216_SHIFT
));
210 if (status
& STAT_INT
)
214 * DMA request active?
216 status
= readb(reg_dmastat
);
217 if (!(status
& EESOX_STAT_DMA
))
221 * Get number of bytes in FIFO
223 status
= readb(reg_fas
+ (REG_CFIS
<< EESOX_FAS216_SHIFT
)) & CFIS_CF
;
232 if (((u32
)buf
) & 2 && status
>= 2) {
233 *(u16
*)buf
= readl(reg_dmadata
);
240 unsigned long l1
, l2
;
242 l1
= readl(reg_dmadata
) & mask
;
243 l1
|= readl(reg_dmadata
) << 16;
244 l2
= readl(reg_dmadata
) & mask
;
245 l2
|= readl(reg_dmadata
) << 16;
257 l1
= readl(reg_dmadata
) & mask
;
258 l1
|= readl(reg_dmadata
) << 16;
267 *(u16
*)buf
= readl(reg_dmadata
);
274 static void eesoxscsi_buffer_out(void *buf
, int length
, void __iomem
*base
)
276 const void __iomem
*reg_fas
= base
+ EESOX_FAS216_OFFSET
;
277 const void __iomem
*reg_dmastat
= base
+ EESOX_DMASTAT
;
278 void __iomem
*reg_dmadata
= base
+ EESOX_DMADATA
;
286 status
= readb(reg_fas
+ (REG_STAT
<< EESOX_FAS216_SHIFT
));
287 if (status
& STAT_INT
)
291 * DMA request active?
293 status
= readb(reg_dmastat
);
294 if (!(status
& EESOX_STAT_DMA
))
298 * Get number of bytes in FIFO
300 status
= readb(reg_fas
+ (REG_CFIS
<< EESOX_FAS216_SHIFT
)) & CFIS_CF
;
303 status
= 16 - status
;
311 if (((u32
)buf
) & 2 && status
>= 2) {
312 writel(*(u16
*)buf
<< 16, reg_dmadata
);
319 unsigned long l1
, l2
;
326 writel(l1
<< 16, reg_dmadata
);
327 writel(l1
, reg_dmadata
);
328 writel(l2
<< 16, reg_dmadata
);
329 writel(l2
, reg_dmadata
);
340 writel(l1
<< 16, reg_dmadata
);
341 writel(l1
, reg_dmadata
);
347 writel(*(u16
*)buf
<< 16, reg_dmadata
);
355 eesoxscsi_dma_pseudo(struct Scsi_Host
*host
, struct scsi_pointer
*SCp
,
356 fasdmadir_t dir
, int transfer_size
)
358 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
360 eesoxscsi_buffer_in(SCp
->ptr
, SCp
->this_residual
, info
->base
);
362 eesoxscsi_buffer_out(SCp
->ptr
, SCp
->this_residual
, info
->base
);
366 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
367 * Purpose : stops DMA/PIO
368 * Params : host - host
372 eesoxscsi_dma_stop(struct Scsi_Host
*host
, struct scsi_pointer
*SCp
)
374 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
375 if (info
->info
.scsi
.dma
!= NO_DMA
)
376 disable_dma(info
->info
.scsi
.dma
);
379 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
380 * Purpose : returns a descriptive string about this interface,
381 * Params : host - driver host structure to return info for.
382 * Returns : pointer to a static buffer containing null terminated string.
384 static const char *eesoxscsi_info(struct Scsi_Host
*host
)
386 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
387 static char string
[150];
389 sprintf(string
, "%s (%s) in slot %d v%s terminators o%s",
390 host
->hostt
->name
, info
->info
.scsi
.type
, info
->ec
->slot_no
,
391 VERSION
, info
->control
& EESOX_TERM_ENABLE
? "n" : "ff");
396 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
397 * Purpose : Set a driver specific function
398 * Params : host - host to setup
399 * : buffer - buffer containing string describing operation
400 * : length - length of string
401 * Returns : -EINVAL, or 0
404 eesoxscsi_set_proc_info(struct Scsi_Host
*host
, char *buffer
, int length
)
408 if (length
>= 9 && strncmp(buffer
, "EESOXSCSI", 9) == 0) {
412 if (length
>= 5 && strncmp(buffer
, "term=", 5) == 0) {
413 if (buffer
[5] == '1')
414 eesoxscsi_terminator_ctl(host
, 1);
415 else if (buffer
[5] == '0')
416 eesoxscsi_terminator_ctl(host
, 0);
427 static int eesoxscsi_show_info(struct seq_file
*m
, struct Scsi_Host
*host
)
429 struct eesoxscsi_info
*info
;
431 info
= (struct eesoxscsi_info
*)host
->hostdata
;
433 seq_printf(m
, "EESOX SCSI driver v%s\n", VERSION
);
434 fas216_print_host(&info
->info
, m
);
435 seq_printf(m
, "Term : o%s\n",
436 info
->control
& EESOX_TERM_ENABLE
? "n" : "ff");
438 fas216_print_stats(&info
->info
, m
);
439 fas216_print_devices(&info
->info
, m
);
443 static ssize_t
eesoxscsi_show_term(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
445 struct expansion_card
*ec
= ECARD_DEV(dev
);
446 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
447 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
449 return sprintf(buf
, "%d\n", info
->control
& EESOX_TERM_ENABLE
? 1 : 0);
452 static ssize_t
eesoxscsi_store_term(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t len
)
454 struct expansion_card
*ec
= ECARD_DEV(dev
);
455 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
456 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
460 spin_lock_irqsave(host
->host_lock
, flags
);
462 info
->control
|= EESOX_TERM_ENABLE
;
464 info
->control
&= ~EESOX_TERM_ENABLE
;
466 writeb(info
->control
, info
->ctl_port
);
467 spin_unlock_irqrestore(host
->host_lock
, flags
);
473 static DEVICE_ATTR(bus_term
, S_IRUGO
| S_IWUSR
,
474 eesoxscsi_show_term
, eesoxscsi_store_term
);
476 static const struct scsi_host_template eesox_template
= {
477 .module
= THIS_MODULE
,
478 .show_info
= eesoxscsi_show_info
,
479 .write_info
= eesoxscsi_set_proc_info
,
480 .name
= "EESOX SCSI",
481 .info
= eesoxscsi_info
,
482 .queuecommand
= fas216_queue_command
,
483 .eh_host_reset_handler
= fas216_eh_host_reset
,
484 .eh_bus_reset_handler
= fas216_eh_bus_reset
,
485 .eh_device_reset_handler
= fas216_eh_device_reset
,
486 .eh_abort_handler
= fas216_eh_abort
,
487 .cmd_size
= sizeof(struct fas216_cmd_priv
),
490 .sg_tablesize
= SG_MAX_SEGMENTS
,
491 .dma_boundary
= IOMD_DMA_BOUNDARY
,
492 .proc_name
= "eesox",
495 static int eesoxscsi_probe(struct expansion_card
*ec
, const struct ecard_id
*id
)
497 struct Scsi_Host
*host
;
498 struct eesoxscsi_info
*info
;
502 ret
= ecard_request_resources(ec
);
506 base
= ecardm_iomap(ec
, ECARD_RES_IOCFAST
, 0, 0);
512 host
= scsi_host_alloc(&eesox_template
,
513 sizeof(struct eesoxscsi_info
));
519 ecard_set_drvdata(ec
, host
);
521 info
= (struct eesoxscsi_info
*)host
->hostdata
;
524 info
->ctl_port
= base
+ EESOX_CONTROL
;
525 info
->control
= term
[ec
->slot_no
] ? EESOX_TERM_ENABLE
: 0;
526 writeb(info
->control
, info
->ctl_port
);
528 info
->info
.scsi
.io_base
= base
+ EESOX_FAS216_OFFSET
;
529 info
->info
.scsi
.io_shift
= EESOX_FAS216_SHIFT
;
530 info
->info
.scsi
.irq
= ec
->irq
;
531 info
->info
.scsi
.dma
= ec
->dma
;
532 info
->info
.ifcfg
.clockrate
= 40; /* MHz */
533 info
->info
.ifcfg
.select_timeout
= 255;
534 info
->info
.ifcfg
.asyncperiod
= 200; /* ns */
535 info
->info
.ifcfg
.sync_max_depth
= 7;
536 info
->info
.ifcfg
.cntl3
= CNTL3_FASTSCSI
| CNTL3_FASTCLK
;
537 info
->info
.ifcfg
.disconnect_ok
= 1;
538 info
->info
.ifcfg
.wide_max_size
= 0;
539 info
->info
.ifcfg
.capabilities
= FASCAP_PSEUDODMA
;
540 info
->info
.dma
.setup
= eesoxscsi_dma_setup
;
541 info
->info
.dma
.pseudo
= eesoxscsi_dma_pseudo
;
542 info
->info
.dma
.stop
= eesoxscsi_dma_stop
;
544 ec
->irqaddr
= base
+ EESOX_DMASTAT
;
545 ec
->irqmask
= EESOX_STAT_INTR
;
547 ecard_setirq(ec
, &eesoxscsi_ops
, info
);
549 device_create_file(&ec
->dev
, &dev_attr_bus_term
);
551 ret
= fas216_init(host
);
555 ret
= request_irq(ec
->irq
, eesoxscsi_intr
, 0, "eesoxscsi", info
);
557 printk("scsi%d: IRQ%d not free: %d\n",
558 host
->host_no
, ec
->irq
, ret
);
562 if (info
->info
.scsi
.dma
!= NO_DMA
) {
563 if (request_dma(info
->info
.scsi
.dma
, "eesox")) {
564 printk("scsi%d: DMA%d not free, DMA disabled\n",
565 host
->host_no
, info
->info
.scsi
.dma
);
566 info
->info
.scsi
.dma
= NO_DMA
;
568 set_dma_speed(info
->info
.scsi
.dma
, 180);
569 info
->info
.ifcfg
.capabilities
|= FASCAP_DMA
;
570 info
->info
.ifcfg
.cntl3
|= CNTL3_BS8
;
574 ret
= fas216_add(host
, &ec
->dev
);
578 if (info
->info
.scsi
.dma
!= NO_DMA
)
579 free_dma(info
->info
.scsi
.dma
);
580 free_irq(ec
->irq
, info
);
586 device_remove_file(&ec
->dev
, &dev_attr_bus_term
);
590 ecard_release_resources(ec
);
596 static void eesoxscsi_remove(struct expansion_card
*ec
)
598 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
599 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
601 ecard_set_drvdata(ec
, NULL
);
604 if (info
->info
.scsi
.dma
!= NO_DMA
)
605 free_dma(info
->info
.scsi
.dma
);
606 free_irq(ec
->irq
, info
);
608 device_remove_file(&ec
->dev
, &dev_attr_bus_term
);
610 fas216_release(host
);
612 ecard_release_resources(ec
);
615 static const struct ecard_id eesoxscsi_cids
[] = {
616 { MANU_EESOX
, PROD_EESOX_SCSI2
},
620 static struct ecard_driver eesoxscsi_driver
= {
621 .probe
= eesoxscsi_probe
,
622 .remove
= eesoxscsi_remove
,
623 .id_table
= eesoxscsi_cids
,
629 static int __init
eesox_init(void)
631 return ecard_register_driver(&eesoxscsi_driver
);
634 static void __exit
eesox_exit(void)
636 ecard_remove_driver(&eesoxscsi_driver
);
639 module_init(eesox_init
);
640 module_exit(eesox_exit
);
642 MODULE_AUTHOR("Russell King");
643 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
644 module_param_array(term
, int, NULL
, 0);
645 MODULE_PARM_DESC(term
, "SCSI bus termination");
646 MODULE_LICENSE("GPL");