blob: 564b354736723aa45ad4294c1b2220996a9186e7 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/init.h>
4#include <linux/interrupt.h>
Geert Uytterhoevenc737e222009-08-16 11:17:35 +02005#include <linux/mm.h>
6#include <linux/slab.h>
7#include <linux/spinlock.h>
8#include <linux/zorro.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -04009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/page.h>
12#include <asm/pgtable.h>
13#include <asm/amigaints.h>
14#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include "scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "wd33c93.h"
18#include "a2091.h"
19
Adrian Bunk5880f482009-03-04 12:06:07 -080020
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020021struct a2091_hostdata {
22 struct WD33C93_hostdata wh;
23 struct a2091_scsiregs *regs;
24};
25
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020026static irqreturn_t a2091_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020028 struct Scsi_Host *instance = data;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020029 struct a2091_hostdata *hdata = shost_priv(instance);
30 unsigned int status = hdata->regs->ISTR;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020031 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020033 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
34 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020036 spin_lock_irqsave(instance->host_lock, flags);
37 wd33c93_intr(instance);
38 spin_unlock_irqrestore(instance->host_lock, flags);
39 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Henrik Kretzschmar65396412006-09-12 23:49:33 +020042static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020044 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020045 struct a2091_hostdata *hdata = shost_priv(instance);
46 struct WD33C93_hostdata *wh = &hdata->wh;
47 struct a2091_scsiregs *regs = hdata->regs;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020048 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
49 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020051 /* don't allow DMA if the physical address is bad */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (addr & A2091_XFER_MASK) {
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020053 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
54 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
55 GFP_KERNEL);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020056
57 /* can't allocate memory; use PIO */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020058 if (!wh->dma_bounce_buffer) {
59 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020060 return 1;
61 }
62
63 /* get the physical address of the bounce buffer */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020064 addr = virt_to_bus(wh->dma_bounce_buffer);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020065
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 */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020069 kfree(wh->dma_bounce_buffer);
70 wh->dma_bounce_buffer = NULL;
71 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020072 return 1;
73 }
74
75 if (!dir_in) {
76 /* copy to bounce buffer for a write */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020077 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +020078 cmd->SCp.this_residual);
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020082 /* setup dma direction */
83 if (!dir_in)
84 cntr |= CNTR_DDIR;
85
86 /* remember direction */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +020087 wh->dma_dir = dir_in;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020088
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020089 regs->CNTR = cntr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020090
91 /* setup DMA *physical* address */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +020092 regs->ACR = addr;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +020093
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200101 /* start DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200102 regs->ST_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200104 /* return success */
105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200108static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200109 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200111 struct a2091_hostdata *hdata = shost_priv(instance);
112 struct WD33C93_hostdata *wh = &hdata->wh;
113 struct a2091_scsiregs *regs = hdata->regs;
Geert Uytterhoeven6d1d5d42010-04-04 11:00:36 +0200114
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200115 /* disable SCSI interrupts */
116 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200118 if (!wh->dma_dir)
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200119 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200121 /* disable SCSI interrupts */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200122 regs->CNTR = cntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200124 /* flush if we were reading */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200125 if (wh->dma_dir) {
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200126 regs->FLUSH = 1;
127 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200128 ;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200131 /* clear a possible interrupt */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200132 regs->CINT = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200134 /* stop DMA */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200135 regs->SP_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200137 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200138 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200140 /* copy from a bounce buffer, if necessary */
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200141 if (status && wh->dma_bounce_buffer) {
142 if (wh->dma_dir)
143 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200144 SCpnt->SCp.this_residual);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200145 kfree(wh->dma_bounce_buffer);
146 wh->dma_bounce_buffer = NULL;
147 wh->dma_bounce_len = 0;
Geert Uytterhoeven09bc85b2010-04-04 11:00:32 +0200148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200151static struct scsi_host_template a2091_scsi_template = {
152 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .name = "Commodore A2091/A590 SCSI",
Al Viro408bb252013-03-31 00:30:35 -0400154 .show_info = wd33c93_show_info,
155 .write_info = wd33c93_write_info,
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200156 .proc_name = "A2901",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .queuecommand = wd33c93_queuecommand,
158 .eh_abort_handler = wd33c93_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .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,
Christoph Hellwig4af14d12018-12-13 16:17:09 +0100164 .dma_boundary = PAGE_SIZE - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800167static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200169 struct Scsi_Host *instance;
170 int error;
171 struct a2091_scsiregs *regs;
172 wd33c93_regs wdregs;
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200173 struct a2091_hostdata *hdata;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200174
175 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
176 return -EBUSY;
177
178 instance = scsi_host_alloc(&a2091_scsi_template,
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200179 sizeof(struct a2091_hostdata));
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200180 if (!instance) {
181 error = -ENOMEM;
182 goto fail_alloc;
183 }
184
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200185 instance->irq = IRQ_AMIGA_PORTS;
186 instance->unique_id = z->slotaddr;
187
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +0100188 regs = ZTWO_VADDR(z->resource.start);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200189 regs->DAWR = DAWR_A2091;
190
191 wdregs.SASR = &regs->SASR;
192 wdregs.SCMD = &regs->SCMD;
193
194 hdata = shost_priv(instance);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200195 hdata->wh.no_sync = 0xff;
196 hdata->wh.fast = 0;
197 hdata->wh.dma_mode = CTRL_DMA;
198 hdata->regs = regs;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200199
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;
216
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
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800226static void a2091_remove(struct zorro_dev *z)
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200227{
228 struct Scsi_Host *instance = zorro_get_drvdata(z);
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200229 struct a2091_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenb1de6ab2010-04-04 11:00:40 +0200230
Geert Uytterhoeven65c27842010-04-12 21:55:03 +0200231 hdata->regs->CNTR = 0;
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200232 scsi_remove_host(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 free_irq(IRQ_AMIGA_PORTS, instance);
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200234 scsi_host_put(instance);
235 release_mem_region(z->resource.start, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800238static struct zorro_device_id a2091_zorro_tbl[] = {
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200239 { 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,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800249 .remove = a2091_remove,
Geert Uytterhoevenc737e222009-08-16 11:17:35 +0200250};
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);
261}
262module_exit(a2091_exit);
263
264MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265MODULE_LICENSE("GPL");