gpio: zynq: restore zynq_gpio_irq_reqres/zynq_gpio_irq_relres callbacks
[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 11#include <asm/page.h>
1da177e4
LT
12#include <asm/amigaints.h>
13#include <asm/amigahw.h>
1da177e4 14
53555fb7
BVA
15#include <scsi/scsi.h>
16#include <scsi/scsi_cmnd.h>
17#include <scsi/scsi_device.h>
18#include <scsi/scsi_eh.h>
19#include <scsi/scsi_tcq.h>
1da177e4
LT
20#include "wd33c93.h"
21#include "a2091.h"
22
5880f486 23
65c2784a
GU
24struct a2091_hostdata {
25 struct WD33C93_hostdata wh;
26 struct a2091_scsiregs *regs;
479accbb 27 struct device *dev;
65c2784a
GU
28};
29
479accbb
MS
30#define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
31
b1de6ab7 32static irqreturn_t a2091_intr(int irq, void *data)
1da177e4 33{
b1de6ab7 34 struct Scsi_Host *instance = data;
65c2784a
GU
35 struct a2091_hostdata *hdata = shost_priv(instance);
36 unsigned int status = hdata->regs->ISTR;
09bc85b0 37 unsigned long flags;
09bc85b0 38
09bc85b0
GU
39 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
40 return IRQ_NONE;
41
42 spin_lock_irqsave(instance->host_lock, flags);
43 wd33c93_intr(instance);
44 spin_unlock_irqrestore(instance->host_lock, flags);
45 return IRQ_HANDLED;
1da177e4
LT
46}
47
65396410 48static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
1da177e4 49{
dbb2da55 50 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
479accbb 51 unsigned long len = scsi_pointer->this_residual;
6d1d5d43 52 struct Scsi_Host *instance = cmd->device->host;
65c2784a
GU
53 struct a2091_hostdata *hdata = shost_priv(instance);
54 struct WD33C93_hostdata *wh = &hdata->wh;
55 struct a2091_scsiregs *regs = hdata->regs;
09bc85b0 56 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
479accbb
MS
57 dma_addr_t addr;
58
59 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
60 len, DMA_DIR(dir_in));
61 if (dma_mapping_error(hdata->dev, addr)) {
62 dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
63 scsi_pointer->ptr);
64 return 1;
65 }
66 scsi_pointer->dma_handle = addr;
1da177e4 67
09bc85b0 68 /* don't allow DMA if the physical address is bad */
1da177e4 69 if (addr & A2091_XFER_MASK) {
479accbb
MS
70 /* drop useless mapping */
71 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
72 scsi_pointer->this_residual,
73 DMA_DIR(dir_in));
74 scsi_pointer->dma_handle = (dma_addr_t) NULL;
75
dbb2da55 76 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
65c2784a
GU
77 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
78 GFP_KERNEL);
09bc85b0
GU
79
80 /* can't allocate memory; use PIO */
65c2784a
GU
81 if (!wh->dma_bounce_buffer) {
82 wh->dma_bounce_len = 0;
09bc85b0
GU
83 return 1;
84 }
85
479accbb
MS
86 if (!dir_in) {
87 /* copy to bounce buffer for a write */
88 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
89 scsi_pointer->this_residual);
90 }
91
92 /* will flush/invalidate cache for us */
93 addr = dma_map_single(hdata->dev, wh->dma_bounce_buffer,
94 wh->dma_bounce_len, DMA_DIR(dir_in));
95 /* can't map buffer; use PIO */
96 if (dma_mapping_error(hdata->dev, addr)) {
97 dev_warn(hdata->dev, "cannot map bounce buffer %p\n",
98 wh->dma_bounce_buffer);
99 return 1;
100 }
09bc85b0
GU
101
102 /* the bounce buffer may not be in the first 16M of physmem */
103 if (addr & A2091_XFER_MASK) {
104 /* we could use chipmem... maybe later */
65c2784a
GU
105 kfree(wh->dma_bounce_buffer);
106 wh->dma_bounce_buffer = NULL;
107 wh->dma_bounce_len = 0;
09bc85b0
GU
108 return 1;
109 }
110
479accbb 111 scsi_pointer->dma_handle = addr;
1da177e4 112 }
1da177e4 113
09bc85b0
GU
114 /* setup dma direction */
115 if (!dir_in)
116 cntr |= CNTR_DDIR;
1da177e4 117
09bc85b0 118 /* remember direction */
65c2784a 119 wh->dma_dir = dir_in;
1da177e4 120
b1de6ab7 121 regs->CNTR = cntr;
1da177e4 122
09bc85b0 123 /* setup DMA *physical* address */
b1de6ab7 124 regs->ACR = addr;
1da177e4 125
479accbb
MS
126 /* no more cache flush here - dma_map_single() takes care */
127
09bc85b0 128 /* start DMA */
b1de6ab7 129 regs->ST_DMA = 1;
1da177e4 130
09bc85b0
GU
131 /* return success */
132 return 0;
1da177e4
LT
133}
134
65396410 135static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
09bc85b0 136 int status)
1da177e4 137{
dbb2da55 138 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
65c2784a
GU
139 struct a2091_hostdata *hdata = shost_priv(instance);
140 struct WD33C93_hostdata *wh = &hdata->wh;
141 struct a2091_scsiregs *regs = hdata->regs;
6d1d5d43 142
09bc85b0
GU
143 /* disable SCSI interrupts */
144 unsigned short cntr = CNTR_PDMD;
145
65c2784a 146 if (!wh->dma_dir)
09bc85b0
GU
147 cntr |= CNTR_DDIR;
148
149 /* disable SCSI interrupts */
b1de6ab7 150 regs->CNTR = cntr;
09bc85b0
GU
151
152 /* flush if we were reading */
65c2784a 153 if (wh->dma_dir) {
b1de6ab7
GU
154 regs->FLUSH = 1;
155 while (!(regs->ISTR & ISTR_FE_FLG))
09bc85b0
GU
156 ;
157 }
158
159 /* clear a possible interrupt */
b1de6ab7 160 regs->CINT = 1;
09bc85b0
GU
161
162 /* stop DMA */
b1de6ab7 163 regs->SP_DMA = 1;
09bc85b0
GU
164
165 /* restore the CONTROL bits (minus the direction flag) */
b1de6ab7 166 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
09bc85b0 167
479accbb
MS
168 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
169 scsi_pointer->this_residual,
170 DMA_DIR(wh->dma_dir));
171
09bc85b0 172 /* copy from a bounce buffer, if necessary */
65c2784a
GU
173 if (status && wh->dma_bounce_buffer) {
174 if (wh->dma_dir)
dbb2da55
BVA
175 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
176 scsi_pointer->this_residual);
65c2784a
GU
177 kfree(wh->dma_bounce_buffer);
178 wh->dma_bounce_buffer = NULL;
179 wh->dma_bounce_len = 0;
09bc85b0 180 }
1da177e4
LT
181}
182
4412df38 183static const struct scsi_host_template a2091_scsi_template = {
c737e22c 184 .module = THIS_MODULE,
1da177e4 185 .name = "Commodore A2091/A590 SCSI",
408bb25b
AV
186 .show_info = wd33c93_show_info,
187 .write_info = wd33c93_write_info,
c737e22c 188 .proc_name = "A2901",
1da177e4
LT
189 .queuecommand = wd33c93_queuecommand,
190 .eh_abort_handler = wd33c93_abort,
1da177e4
LT
191 .eh_host_reset_handler = wd33c93_host_reset,
192 .can_queue = CAN_QUEUE,
193 .this_id = 7,
194 .sg_tablesize = SG_ALL,
195 .cmd_per_lun = CMD_PER_LUN,
4af14d11 196 .dma_boundary = PAGE_SIZE - 1,
dbb2da55 197 .cmd_size = sizeof(struct scsi_pointer),
1da177e4
LT
198};
199
6f039790 200static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
c737e22c
GU
201{
202 struct Scsi_Host *instance;
203 int error;
204 struct a2091_scsiregs *regs;
205 wd33c93_regs wdregs;
65c2784a 206 struct a2091_hostdata *hdata;
c737e22c 207
479accbb
MS
208 if (dma_set_mask_and_coherent(&z->dev, DMA_BIT_MASK(24))) {
209 dev_warn(&z->dev, "cannot use 24 bit DMA\n");
210 return -ENODEV;
211 }
212
c737e22c
GU
213 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
214 return -EBUSY;
215
216 instance = scsi_host_alloc(&a2091_scsi_template,
65c2784a 217 sizeof(struct a2091_hostdata));
c737e22c
GU
218 if (!instance) {
219 error = -ENOMEM;
220 goto fail_alloc;
221 }
222
c737e22c
GU
223 instance->irq = IRQ_AMIGA_PORTS;
224 instance->unique_id = z->slotaddr;
225
6112ea08 226 regs = ZTWO_VADDR(z->resource.start);
c737e22c
GU
227 regs->DAWR = DAWR_A2091;
228
229 wdregs.SASR = &regs->SASR;
230 wdregs.SCMD = &regs->SCMD;
1da177e4 231
c737e22c 232 hdata = shost_priv(instance);
479accbb 233 hdata->dev = &z->dev;
65c2784a
GU
234 hdata->wh.no_sync = 0xff;
235 hdata->wh.fast = 0;
236 hdata->wh.dma_mode = CTRL_DMA;
237 hdata->regs = regs;
c737e22c
GU
238
239 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
240 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
241 "A2091 SCSI", instance);
242 if (error)
243 goto fail_irq;
244
245 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
246
247 error = scsi_add_host(instance, NULL);
248 if (error)
249 goto fail_host;
250
251 zorro_set_drvdata(z, instance);
252
253 scsi_scan_host(instance);
254 return 0;
1da177e4 255
c737e22c
GU
256fail_host:
257 free_irq(IRQ_AMIGA_PORTS, instance);
258fail_irq:
259 scsi_host_put(instance);
260fail_alloc:
261 release_mem_region(z->resource.start, 256);
262 return error;
263}
264
6f039790 265static void a2091_remove(struct zorro_dev *z)
1da177e4 266{
c737e22c 267 struct Scsi_Host *instance = zorro_get_drvdata(z);
65c2784a 268 struct a2091_hostdata *hdata = shost_priv(instance);
b1de6ab7 269
65c2784a 270 hdata->regs->CNTR = 0;
c737e22c 271 scsi_remove_host(instance);
1da177e4 272 free_irq(IRQ_AMIGA_PORTS, instance);
c737e22c
GU
273 scsi_host_put(instance);
274 release_mem_region(z->resource.start, 256);
275}
276
6f039790 277static struct zorro_device_id a2091_zorro_tbl[] = {
c737e22c
GU
278 { ZORRO_PROD_CBM_A590_A2091_1 },
279 { ZORRO_PROD_CBM_A590_A2091_2 },
280 { 0 }
281};
282MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
283
284static struct zorro_driver a2091_driver = {
285 .name = "a2091",
286 .id_table = a2091_zorro_tbl,
287 .probe = a2091_probe,
6f039790 288 .remove = a2091_remove,
c737e22c
GU
289};
290
291static int __init a2091_init(void)
292{
293 return zorro_register_driver(&a2091_driver);
294}
295module_init(a2091_init);
296
297static void __exit a2091_exit(void)
298{
299 zorro_unregister_driver(&a2091_driver);
1da177e4 300}
c737e22c 301module_exit(a2091_exit);
1da177e4 302
c737e22c 303MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
1da177e4 304MODULE_LICENSE("GPL");