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>
35 #include <asm/ecard.h>
36 #include <asm/pgtable.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
;
175 dma_map_sg(dev
, info
->sg
, bufs
, map_dir
);
178 set_dma_sg(dmach
, info
->sg
, bufs
);
179 set_dma_mode(dmach
, dma_dir
);
181 return fasdma_real_all
;
184 * We don't do DMA, we only do slow PIO
186 * Some day, we will do Pseudo DMA
188 return fasdma_pseudo
;
191 static void eesoxscsi_buffer_in(void *buf
, int length
, void __iomem
*base
)
193 const void __iomem
*reg_fas
= base
+ EESOX_FAS216_OFFSET
;
194 const void __iomem
*reg_dmastat
= base
+ EESOX_DMASTAT
;
195 const void __iomem
*reg_dmadata
= base
+ EESOX_DMADATA
;
196 register const unsigned long mask
= 0xffff;
204 status
= readb(reg_fas
+ (REG_STAT
<< EESOX_FAS216_SHIFT
));
205 if (status
& STAT_INT
)
209 * DMA request active?
211 status
= readb(reg_dmastat
);
212 if (!(status
& EESOX_STAT_DMA
))
216 * Get number of bytes in FIFO
218 status
= readb(reg_fas
+ (REG_CFIS
<< EESOX_FAS216_SHIFT
)) & CFIS_CF
;
227 if (((u32
)buf
) & 2 && status
>= 2) {
228 *(u16
*)buf
= readl(reg_dmadata
);
235 unsigned long l1
, l2
;
237 l1
= readl(reg_dmadata
) & mask
;
238 l1
|= readl(reg_dmadata
) << 16;
239 l2
= readl(reg_dmadata
) & mask
;
240 l2
|= readl(reg_dmadata
) << 16;
252 l1
= readl(reg_dmadata
) & mask
;
253 l1
|= readl(reg_dmadata
) << 16;
262 *(u16
*)buf
= readl(reg_dmadata
);
269 static void eesoxscsi_buffer_out(void *buf
, int length
, void __iomem
*base
)
271 const void __iomem
*reg_fas
= base
+ EESOX_FAS216_OFFSET
;
272 const void __iomem
*reg_dmastat
= base
+ EESOX_DMASTAT
;
273 void __iomem
*reg_dmadata
= base
+ EESOX_DMADATA
;
281 status
= readb(reg_fas
+ (REG_STAT
<< EESOX_FAS216_SHIFT
));
282 if (status
& STAT_INT
)
286 * DMA request active?
288 status
= readb(reg_dmastat
);
289 if (!(status
& EESOX_STAT_DMA
))
293 * Get number of bytes in FIFO
295 status
= readb(reg_fas
+ (REG_CFIS
<< EESOX_FAS216_SHIFT
)) & CFIS_CF
;
298 status
= 16 - status
;
306 if (((u32
)buf
) & 2 && status
>= 2) {
307 writel(*(u16
*)buf
<< 16, reg_dmadata
);
314 unsigned long l1
, l2
;
321 writel(l1
<< 16, reg_dmadata
);
322 writel(l1
, reg_dmadata
);
323 writel(l2
<< 16, reg_dmadata
);
324 writel(l2
, reg_dmadata
);
335 writel(l1
<< 16, reg_dmadata
);
336 writel(l1
, reg_dmadata
);
342 writel(*(u16
*)buf
<< 16, reg_dmadata
);
350 eesoxscsi_dma_pseudo(struct Scsi_Host
*host
, struct scsi_pointer
*SCp
,
351 fasdmadir_t dir
, int transfer_size
)
353 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
355 eesoxscsi_buffer_in(SCp
->ptr
, SCp
->this_residual
, info
->base
);
357 eesoxscsi_buffer_out(SCp
->ptr
, SCp
->this_residual
, info
->base
);
361 /* Prototype: int eesoxscsi_dma_stop(host, SCpnt)
362 * Purpose : stops DMA/PIO
363 * Params : host - host
367 eesoxscsi_dma_stop(struct Scsi_Host
*host
, struct scsi_pointer
*SCp
)
369 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
370 if (info
->info
.scsi
.dma
!= NO_DMA
)
371 disable_dma(info
->info
.scsi
.dma
);
374 /* Prototype: const char *eesoxscsi_info(struct Scsi_Host * host)
375 * Purpose : returns a descriptive string about this interface,
376 * Params : host - driver host structure to return info for.
377 * Returns : pointer to a static buffer containing null terminated string.
379 const char *eesoxscsi_info(struct Scsi_Host
*host
)
381 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
382 static char string
[150];
384 sprintf(string
, "%s (%s) in slot %d v%s terminators o%s",
385 host
->hostt
->name
, info
->info
.scsi
.type
, info
->ec
->slot_no
,
386 VERSION
, info
->control
& EESOX_TERM_ENABLE
? "n" : "ff");
391 /* Prototype: int eesoxscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
392 * Purpose : Set a driver specific function
393 * Params : host - host to setup
394 * : buffer - buffer containing string describing operation
395 * : length - length of string
396 * Returns : -EINVAL, or 0
399 eesoxscsi_set_proc_info(struct Scsi_Host
*host
, char *buffer
, int length
)
403 if (length
>= 9 && strncmp(buffer
, "EESOXSCSI", 9) == 0) {
407 if (length
>= 5 && strncmp(buffer
, "term=", 5) == 0) {
408 if (buffer
[5] == '1')
409 eesoxscsi_terminator_ctl(host
, 1);
410 else if (buffer
[5] == '0')
411 eesoxscsi_terminator_ctl(host
, 0);
422 static int eesoxscsi_show_info(struct seq_file
*m
, struct Scsi_Host
*host
)
424 struct eesoxscsi_info
*info
;
426 info
= (struct eesoxscsi_info
*)host
->hostdata
;
428 seq_printf(m
, "EESOX SCSI driver v%s\n", VERSION
);
429 fas216_print_host(&info
->info
, m
);
430 seq_printf(m
, "Term : o%s\n",
431 info
->control
& EESOX_TERM_ENABLE
? "n" : "ff");
433 fas216_print_stats(&info
->info
, m
);
434 fas216_print_devices(&info
->info
, m
);
438 static ssize_t
eesoxscsi_show_term(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
440 struct expansion_card
*ec
= ECARD_DEV(dev
);
441 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
442 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
444 return sprintf(buf
, "%d\n", info
->control
& EESOX_TERM_ENABLE
? 1 : 0);
447 static ssize_t
eesoxscsi_store_term(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t len
)
449 struct expansion_card
*ec
= ECARD_DEV(dev
);
450 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
451 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
455 spin_lock_irqsave(host
->host_lock
, flags
);
457 info
->control
|= EESOX_TERM_ENABLE
;
459 info
->control
&= ~EESOX_TERM_ENABLE
;
461 writeb(info
->control
, info
->ctl_port
);
462 spin_unlock_irqrestore(host
->host_lock
, flags
);
468 static DEVICE_ATTR(bus_term
, S_IRUGO
| S_IWUSR
,
469 eesoxscsi_show_term
, eesoxscsi_store_term
);
471 static struct scsi_host_template eesox_template
= {
472 .module
= THIS_MODULE
,
473 .show_info
= eesoxscsi_show_info
,
474 .write_info
= eesoxscsi_set_proc_info
,
475 .name
= "EESOX SCSI",
476 .info
= eesoxscsi_info
,
477 .queuecommand
= fas216_queue_command
,
478 .eh_host_reset_handler
= fas216_eh_host_reset
,
479 .eh_bus_reset_handler
= fas216_eh_bus_reset
,
480 .eh_device_reset_handler
= fas216_eh_device_reset
,
481 .eh_abort_handler
= fas216_eh_abort
,
484 .sg_tablesize
= SG_MAX_SEGMENTS
,
485 .dma_boundary
= IOMD_DMA_BOUNDARY
,
486 .proc_name
= "eesox",
489 static int eesoxscsi_probe(struct expansion_card
*ec
, const struct ecard_id
*id
)
491 struct Scsi_Host
*host
;
492 struct eesoxscsi_info
*info
;
496 ret
= ecard_request_resources(ec
);
500 base
= ecardm_iomap(ec
, ECARD_RES_IOCFAST
, 0, 0);
506 host
= scsi_host_alloc(&eesox_template
,
507 sizeof(struct eesoxscsi_info
));
513 ecard_set_drvdata(ec
, host
);
515 info
= (struct eesoxscsi_info
*)host
->hostdata
;
518 info
->ctl_port
= base
+ EESOX_CONTROL
;
519 info
->control
= term
[ec
->slot_no
] ? EESOX_TERM_ENABLE
: 0;
520 writeb(info
->control
, info
->ctl_port
);
522 info
->info
.scsi
.io_base
= base
+ EESOX_FAS216_OFFSET
;
523 info
->info
.scsi
.io_shift
= EESOX_FAS216_SHIFT
;
524 info
->info
.scsi
.irq
= ec
->irq
;
525 info
->info
.scsi
.dma
= ec
->dma
;
526 info
->info
.ifcfg
.clockrate
= 40; /* MHz */
527 info
->info
.ifcfg
.select_timeout
= 255;
528 info
->info
.ifcfg
.asyncperiod
= 200; /* ns */
529 info
->info
.ifcfg
.sync_max_depth
= 7;
530 info
->info
.ifcfg
.cntl3
= CNTL3_FASTSCSI
| CNTL3_FASTCLK
;
531 info
->info
.ifcfg
.disconnect_ok
= 1;
532 info
->info
.ifcfg
.wide_max_size
= 0;
533 info
->info
.ifcfg
.capabilities
= FASCAP_PSEUDODMA
;
534 info
->info
.dma
.setup
= eesoxscsi_dma_setup
;
535 info
->info
.dma
.pseudo
= eesoxscsi_dma_pseudo
;
536 info
->info
.dma
.stop
= eesoxscsi_dma_stop
;
538 ec
->irqaddr
= base
+ EESOX_DMASTAT
;
539 ec
->irqmask
= EESOX_STAT_INTR
;
541 ecard_setirq(ec
, &eesoxscsi_ops
, info
);
543 device_create_file(&ec
->dev
, &dev_attr_bus_term
);
545 ret
= fas216_init(host
);
549 ret
= request_irq(ec
->irq
, eesoxscsi_intr
, 0, "eesoxscsi", info
);
551 printk("scsi%d: IRQ%d not free: %d\n",
552 host
->host_no
, ec
->irq
, ret
);
556 if (info
->info
.scsi
.dma
!= NO_DMA
) {
557 if (request_dma(info
->info
.scsi
.dma
, "eesox")) {
558 printk("scsi%d: DMA%d not free, DMA disabled\n",
559 host
->host_no
, info
->info
.scsi
.dma
);
560 info
->info
.scsi
.dma
= NO_DMA
;
562 set_dma_speed(info
->info
.scsi
.dma
, 180);
563 info
->info
.ifcfg
.capabilities
|= FASCAP_DMA
;
564 info
->info
.ifcfg
.cntl3
|= CNTL3_BS8
;
568 ret
= fas216_add(host
, &ec
->dev
);
572 if (info
->info
.scsi
.dma
!= NO_DMA
)
573 free_dma(info
->info
.scsi
.dma
);
574 free_irq(ec
->irq
, host
);
580 device_remove_file(&ec
->dev
, &dev_attr_bus_term
);
584 ecard_release_resources(ec
);
590 static void eesoxscsi_remove(struct expansion_card
*ec
)
592 struct Scsi_Host
*host
= ecard_get_drvdata(ec
);
593 struct eesoxscsi_info
*info
= (struct eesoxscsi_info
*)host
->hostdata
;
595 ecard_set_drvdata(ec
, NULL
);
598 if (info
->info
.scsi
.dma
!= NO_DMA
)
599 free_dma(info
->info
.scsi
.dma
);
600 free_irq(ec
->irq
, info
);
602 device_remove_file(&ec
->dev
, &dev_attr_bus_term
);
604 fas216_release(host
);
606 ecard_release_resources(ec
);
609 static const struct ecard_id eesoxscsi_cids
[] = {
610 { MANU_EESOX
, PROD_EESOX_SCSI2
},
614 static struct ecard_driver eesoxscsi_driver
= {
615 .probe
= eesoxscsi_probe
,
616 .remove
= eesoxscsi_remove
,
617 .id_table
= eesoxscsi_cids
,
623 static int __init
eesox_init(void)
625 return ecard_register_driver(&eesoxscsi_driver
);
628 static void __exit
eesox_exit(void)
630 ecard_remove_driver(&eesoxscsi_driver
);
633 module_init(eesox_init
);
634 module_exit(eesox_exit
);
636 MODULE_AUTHOR("Russell King");
637 MODULE_DESCRIPTION("EESOX 'Fast' SCSI driver for Acorn machines");
638 module_param_array(term
, int, NULL
, 0);
639 MODULE_PARM_DESC(term
, "SCSI bus termination");
640 MODULE_LICENSE("GPL");