Merge tag 's390-5.3-4' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-block.git] / drivers / scsi / a2091.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2#include <linux/types.h>
1da177e4
LT
3#include <linux/init.h>
4#include <linux/interrupt.h>
c737e22c
GU
5#include <linux/mm.h>
6#include <linux/slab.h>
7#include <linux/spinlock.h>
8#include <linux/zorro.h>
acf3368f 9#include <linux/module.h>
1da177e4 10
1da177e4
LT
11#include <asm/page.h>
12#include <asm/pgtable.h>
13#include <asm/amigaints.h>
14#include <asm/amigahw.h>
1da177e4
LT
15
16#include "scsi.h"
1da177e4
LT
17#include "wd33c93.h"
18#include "a2091.h"
19
5880f486 20
65c2784a
GU
21struct a2091_hostdata {
22 struct WD33C93_hostdata wh;
23 struct a2091_scsiregs *regs;
24};
25
b1de6ab7 26static irqreturn_t a2091_intr(int irq, void *data)
1da177e4 27{
b1de6ab7 28 struct Scsi_Host *instance = data;
65c2784a
GU
29 struct a2091_hostdata *hdata = shost_priv(instance);
30 unsigned int status = hdata->regs->ISTR;
09bc85b0 31 unsigned long flags;
09bc85b0 32
09bc85b0
GU
33 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
34 return IRQ_NONE;
35
36 spin_lock_irqsave(instance->host_lock, flags);
37 wd33c93_intr(instance);
38 spin_unlock_irqrestore(instance->host_lock, flags);
39 return IRQ_HANDLED;
1da177e4
LT
40}
41
65396410 42static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
1da177e4 43{
6d1d5d43 44 struct Scsi_Host *instance = cmd->device->host;
65c2784a
GU
45 struct a2091_hostdata *hdata = shost_priv(instance);
46 struct WD33C93_hostdata *wh = &hdata->wh;
47 struct a2091_scsiregs *regs = hdata->regs;
09bc85b0
GU
48 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
49 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
1da177e4 50
09bc85b0 51 /* don't allow DMA if the physical address is bad */
1da177e4 52 if (addr & A2091_XFER_MASK) {
65c2784a
GU
53 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
54 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
55 GFP_KERNEL);
09bc85b0
GU
56
57 /* can't allocate memory; use PIO */
65c2784a
GU
58 if (!wh->dma_bounce_buffer) {
59 wh->dma_bounce_len = 0;
09bc85b0
GU
60 return 1;
61 }
62
63 /* get the physical address of the bounce buffer */
65c2784a 64 addr = virt_to_bus(wh->dma_bounce_buffer);
09bc85b0
GU
65
66 /* the bounce buffer may not be in the first 16M of physmem */
67 if (addr & A2091_XFER_MASK) {
68 /* we could use chipmem... maybe later */
65c2784a
GU
69 kfree(wh->dma_bounce_buffer);
70 wh->dma_bounce_buffer = NULL;
71 wh->dma_bounce_len = 0;
09bc85b0
GU
72 return 1;
73 }
74
75 if (!dir_in) {
76 /* copy to bounce buffer for a write */
65c2784a 77 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
6d1d5d43 78 cmd->SCp.this_residual);
09bc85b0 79 }
1da177e4 80 }
1da177e4 81
09bc85b0
GU
82 /* setup dma direction */
83 if (!dir_in)
84 cntr |= CNTR_DDIR;
1da177e4 85
09bc85b0 86 /* remember direction */
65c2784a 87 wh->dma_dir = dir_in;
1da177e4 88
b1de6ab7 89 regs->CNTR = cntr;
1da177e4 90
09bc85b0 91 /* setup DMA *physical* address */
b1de6ab7 92 regs->ACR = addr;
1da177e4 93
09bc85b0
GU
94 if (dir_in) {
95 /* invalidate any cache */
96 cache_clear(addr, cmd->SCp.this_residual);
97 } else {
98 /* push any dirty cache */
99 cache_push(addr, cmd->SCp.this_residual);
100 }
101 /* start DMA */
b1de6ab7 102 regs->ST_DMA = 1;
1da177e4 103
09bc85b0
GU
104 /* return success */
105 return 0;
1da177e4
LT
106}
107
65396410 108static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
09bc85b0 109 int status)
1da177e4 110{
65c2784a
GU
111 struct a2091_hostdata *hdata = shost_priv(instance);
112 struct WD33C93_hostdata *wh = &hdata->wh;
113 struct a2091_scsiregs *regs = hdata->regs;
6d1d5d43 114
09bc85b0
GU
115 /* disable SCSI interrupts */
116 unsigned short cntr = CNTR_PDMD;
117
65c2784a 118 if (!wh->dma_dir)
09bc85b0
GU
119 cntr |= CNTR_DDIR;
120
121 /* disable SCSI interrupts */
b1de6ab7 122 regs->CNTR = cntr;
09bc85b0
GU
123
124 /* flush if we were reading */
65c2784a 125 if (wh->dma_dir) {
b1de6ab7
GU
126 regs->FLUSH = 1;
127 while (!(regs->ISTR & ISTR_FE_FLG))
09bc85b0
GU
128 ;
129 }
130
131 /* clear a possible interrupt */
b1de6ab7 132 regs->CINT = 1;
09bc85b0
GU
133
134 /* stop DMA */
b1de6ab7 135 regs->SP_DMA = 1;
09bc85b0
GU
136
137 /* restore the CONTROL bits (minus the direction flag) */
b1de6ab7 138 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
09bc85b0
GU
139
140 /* copy from a bounce buffer, if necessary */
65c2784a
GU
141 if (status && wh->dma_bounce_buffer) {
142 if (wh->dma_dir)
143 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
09bc85b0 144 SCpnt->SCp.this_residual);
65c2784a
GU
145 kfree(wh->dma_bounce_buffer);
146 wh->dma_bounce_buffer = NULL;
147 wh->dma_bounce_len = 0;
09bc85b0 148 }
1da177e4
LT
149}
150
c737e22c
GU
151static struct scsi_host_template a2091_scsi_template = {
152 .module = THIS_MODULE,
1da177e4 153 .name = "Commodore A2091/A590 SCSI",
408bb25b
AV
154 .show_info = wd33c93_show_info,
155 .write_info = wd33c93_write_info,
c737e22c 156 .proc_name = "A2901",
1da177e4
LT
157 .queuecommand = wd33c93_queuecommand,
158 .eh_abort_handler = wd33c93_abort,
1da177e4
LT
159 .eh_host_reset_handler = wd33c93_host_reset,
160 .can_queue = CAN_QUEUE,
161 .this_id = 7,
162 .sg_tablesize = SG_ALL,
163 .cmd_per_lun = CMD_PER_LUN,
4af14d11 164 .dma_boundary = PAGE_SIZE - 1,
1da177e4
LT
165};
166
6f039790 167static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
c737e22c
GU
168{
169 struct Scsi_Host *instance;
170 int error;
171 struct a2091_scsiregs *regs;
172 wd33c93_regs wdregs;
65c2784a 173 struct a2091_hostdata *hdata;
c737e22c
GU
174
175 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
176 return -EBUSY;
177
178 instance = scsi_host_alloc(&a2091_scsi_template,
65c2784a 179 sizeof(struct a2091_hostdata));
c737e22c
GU
180 if (!instance) {
181 error = -ENOMEM;
182 goto fail_alloc;
183 }
184
c737e22c
GU
185 instance->irq = IRQ_AMIGA_PORTS;
186 instance->unique_id = z->slotaddr;
187
6112ea08 188 regs = ZTWO_VADDR(z->resource.start);
c737e22c
GU
189 regs->DAWR = DAWR_A2091;
190
191 wdregs.SASR = &regs->SASR;
192 wdregs.SCMD = &regs->SCMD;
1da177e4 193
c737e22c 194 hdata = shost_priv(instance);
65c2784a
GU
195 hdata->wh.no_sync = 0xff;
196 hdata->wh.fast = 0;
197 hdata->wh.dma_mode = CTRL_DMA;
198 hdata->regs = regs;
c737e22c
GU
199
200 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
201 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
202 "A2091 SCSI", instance);
203 if (error)
204 goto fail_irq;
205
206 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
207
208 error = scsi_add_host(instance, NULL);
209 if (error)
210 goto fail_host;
211
212 zorro_set_drvdata(z, instance);
213
214 scsi_scan_host(instance);
215 return 0;
1da177e4 216
c737e22c
GU
217fail_host:
218 free_irq(IRQ_AMIGA_PORTS, instance);
219fail_irq:
220 scsi_host_put(instance);
221fail_alloc:
222 release_mem_region(z->resource.start, 256);
223 return error;
224}
225
6f039790 226static void a2091_remove(struct zorro_dev *z)
1da177e4 227{
c737e22c 228 struct Scsi_Host *instance = zorro_get_drvdata(z);
65c2784a 229 struct a2091_hostdata *hdata = shost_priv(instance);
b1de6ab7 230
65c2784a 231 hdata->regs->CNTR = 0;
c737e22c 232 scsi_remove_host(instance);
1da177e4 233 free_irq(IRQ_AMIGA_PORTS, instance);
c737e22c
GU
234 scsi_host_put(instance);
235 release_mem_region(z->resource.start, 256);
236}
237
6f039790 238static struct zorro_device_id a2091_zorro_tbl[] = {
c737e22c
GU
239 { ZORRO_PROD_CBM_A590_A2091_1 },
240 { ZORRO_PROD_CBM_A590_A2091_2 },
241 { 0 }
242};
243MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
244
245static struct zorro_driver a2091_driver = {
246 .name = "a2091",
247 .id_table = a2091_zorro_tbl,
248 .probe = a2091_probe,
6f039790 249 .remove = a2091_remove,
c737e22c
GU
250};
251
252static int __init a2091_init(void)
253{
254 return zorro_register_driver(&a2091_driver);
255}
256module_init(a2091_init);
257
258static void __exit a2091_exit(void)
259{
260 zorro_unregister_driver(&a2091_driver);
1da177e4 261}
c737e22c 262module_exit(a2091_exit);
1da177e4 263
c737e22c 264MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
1da177e4 265MODULE_LICENSE("GPL");