blob: 4b38c4750f77ab2f27af3a4ea9088d25e34cd64f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/mm.h>
3#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/init.h>
5#include <linux/interrupt.h>
6
7#include <asm/setup.h>
8#include <asm/page.h>
9#include <asm/pgtable.h>
10#include <asm/amigaints.h>
11#include <asm/amigahw.h>
12#include <linux/zorro.h>
13#include <asm/irq.h>
14#include <linux/spinlock.h>
15
16#include "scsi.h"
17#include <scsi/scsi_host.h>
18#include "wd33c93.h"
19#include "a2091.h"
20
21#include<linux/stat.h>
22
23#define DMA(ptr) ((a2091_scsiregs *)((ptr)->base))
24#define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
25
Adrian Bunk5880f482009-03-04 12:06:07 -080026static int a2091_release(struct Scsi_Host *instance);
27
David Howells7d12e782006-10-05 14:55:46 +010028static irqreturn_t a2091_intr (int irq, void *_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 unsigned long flags;
31 unsigned int status;
32 struct Scsi_Host *instance = (struct Scsi_Host *)_instance;
33
34 status = DMA(instance)->ISTR;
35 if (!(status & (ISTR_INT_F|ISTR_INT_P)) || !(status & ISTR_INTS))
36 return IRQ_NONE;
37
38 spin_lock_irqsave(instance->host_lock, flags);
39 wd33c93_intr(instance);
40 spin_unlock_irqrestore(instance->host_lock, flags);
41 return IRQ_HANDLED;
42}
43
Henrik Kretzschmar65396412006-09-12 23:49:33 +020044static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
47 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
48 struct Scsi_Host *instance = cmd->device->host;
49
50 /* don't allow DMA if the physical address is bad */
Adrian Bunk7b892802008-02-06 01:36:29 -080051 if (addr & A2091_XFER_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 {
53 HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
54 & ~0x1ff;
55 HDATA(instance)->dma_bounce_buffer =
56 kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
57
58 /* can't allocate memory; use PIO */
59 if (!HDATA(instance)->dma_bounce_buffer) {
60 HDATA(instance)->dma_bounce_len = 0;
61 return 1;
62 }
63
64 /* get the physical address of the bounce buffer */
65 addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
66
67 /* the bounce buffer may not be in the first 16M of physmem */
68 if (addr & A2091_XFER_MASK) {
69 /* we could use chipmem... maybe later */
70 kfree (HDATA(instance)->dma_bounce_buffer);
71 HDATA(instance)->dma_bounce_buffer = NULL;
72 HDATA(instance)->dma_bounce_len = 0;
73 return 1;
74 }
75
76 if (!dir_in) {
Boaz Harroshf2c1afa2007-09-09 20:57:05 +030077 /* copy to bounce buffer for a write */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 memcpy (HDATA(instance)->dma_bounce_buffer,
79 cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81 }
82
83 /* setup dma direction */
84 if (!dir_in)
85 cntr |= CNTR_DDIR;
86
87 /* remember direction */
88 HDATA(cmd->device->host)->dma_dir = dir_in;
89
90 DMA(cmd->device->host)->CNTR = cntr;
91
92 /* setup DMA *physical* address */
93 DMA(cmd->device->host)->ACR = addr;
94
95 if (dir_in){
96 /* invalidate any cache */
97 cache_clear (addr, cmd->SCp.this_residual);
98 }else{
99 /* push any dirty cache */
100 cache_push (addr, cmd->SCp.this_residual);
101 }
102 /* start DMA */
103 DMA(cmd->device->host)->ST_DMA = 1;
104
105 /* return success */
106 return 0;
107}
108
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200109static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int status)
111{
112 /* disable SCSI interrupts */
113 unsigned short cntr = CNTR_PDMD;
114
115 if (!HDATA(instance)->dma_dir)
116 cntr |= CNTR_DDIR;
117
118 /* disable SCSI interrupts */
119 DMA(instance)->CNTR = cntr;
120
121 /* flush if we were reading */
122 if (HDATA(instance)->dma_dir) {
123 DMA(instance)->FLUSH = 1;
124 while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
125 ;
126 }
127
128 /* clear a possible interrupt */
129 DMA(instance)->CINT = 1;
130
131 /* stop DMA */
132 DMA(instance)->SP_DMA = 1;
133
134 /* restore the CONTROL bits (minus the direction flag) */
135 DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
136
137 /* copy from a bounce buffer, if necessary */
138 if (status && HDATA(instance)->dma_bounce_buffer) {
Boaz Harroshf2c1afa2007-09-09 20:57:05 +0300139 if( HDATA(instance)->dma_dir )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 memcpy (SCpnt->SCp.ptr,
141 HDATA(instance)->dma_bounce_buffer,
142 SCpnt->SCp.this_residual);
Boaz Harroshf2c1afa2007-09-09 20:57:05 +0300143 kfree (HDATA(instance)->dma_bounce_buffer);
144 HDATA(instance)->dma_bounce_buffer = NULL;
145 HDATA(instance)->dma_bounce_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147}
148
Adrian Bunk5880f482009-03-04 12:06:07 -0800149static int __init a2091_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 static unsigned char called = 0;
152 struct Scsi_Host *instance;
153 unsigned long address;
154 struct zorro_dev *z = NULL;
155 wd33c93_regs regs;
156 int num_a2091 = 0;
157
158 if (!MACH_IS_AMIGA || called)
159 return 0;
160 called = 1;
161
162 tpnt->proc_name = "A2091";
163 tpnt->proc_info = &wd33c93_proc_info;
164
165 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
166 if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
167 z->id != ZORRO_PROD_CBM_A590_A2091_2)
168 continue;
169 address = z->resource.start;
170 if (!request_mem_region(address, 256, "wd33c93"))
171 continue;
172
173 instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100174 if (instance == NULL)
175 goto release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 instance->base = ZTWO_VADDR(address);
177 instance->irq = IRQ_AMIGA_PORTS;
178 instance->unique_id = z->slotaddr;
179 DMA(instance)->DAWR = DAWR_A2091;
180 regs.SASR = &(DMA(instance)->SASR);
181 regs.SCMD = &(DMA(instance)->SCMD);
James Bottomleya579dab2008-03-31 22:06:50 -0500182 HDATA(instance)->no_sync = 0xff;
183 HDATA(instance)->fast = 0;
184 HDATA(instance)->dma_mode = CTRL_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100186 if (request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
187 instance))
188 goto unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
190 num_a2091++;
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100191 continue;
192
193unregister:
194 scsi_unregister(instance);
195 wd33c93_release();
196release:
197 release_mem_region(address, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 return num_a2091;
201}
202
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200203static int a2091_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 /* FIXME perform bus-specific reset */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400206
Jeff Garzik df0ae242005-05-28 07:57:14 -0400207 /* FIXME 2: kill this function, and let midlayer fall back
208 to the same action, calling wd33c93_host_reset() */
209
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400210 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400212 spin_unlock_irq(cmd->device->host->host_lock);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return SUCCESS;
215}
216
217#define HOSTS_C
218
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100219static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .proc_name = "A2901",
221 .name = "Commodore A2091/A590 SCSI",
222 .detect = a2091_detect,
223 .release = a2091_release,
224 .queuecommand = wd33c93_queuecommand,
225 .eh_abort_handler = wd33c93_abort,
226 .eh_bus_reset_handler = a2091_bus_reset,
227 .eh_host_reset_handler = wd33c93_host_reset,
228 .can_queue = CAN_QUEUE,
229 .this_id = 7,
230 .sg_tablesize = SG_ALL,
231 .cmd_per_lun = CMD_PER_LUN,
232 .use_clustering = DISABLE_CLUSTERING
233};
234
235
236#include "scsi_module.c"
237
Adrian Bunk5880f482009-03-04 12:06:07 -0800238static int a2091_release(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240#ifdef MODULE
241 DMA(instance)->CNTR = 0;
242 release_mem_region(ZTWO_PADDR(instance->base), 256);
243 free_irq(IRQ_AMIGA_PORTS, instance);
244 wd33c93_release();
245#endif
246 return 1;
247}
248
249MODULE_LICENSE("GPL");