blob: 5ac3a3e8dfafcb57020edf4855690f0961550703 [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
David Howells7d12e782006-10-05 14:55:46 +010026static irqreturn_t a2091_intr (int irq, void *_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 unsigned long flags;
29 unsigned int status;
30 struct Scsi_Host *instance = (struct Scsi_Host *)_instance;
31
32 status = DMA(instance)->ISTR;
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;
40}
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{
44 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
45 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
46 struct Scsi_Host *instance = cmd->device->host;
47
48 /* don't allow DMA if the physical address is bad */
Adrian Bunk7b892802008-02-06 01:36:29 -080049 if (addr & A2091_XFER_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 {
51 HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
52 & ~0x1ff;
53 HDATA(instance)->dma_bounce_buffer =
54 kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
55
56 /* can't allocate memory; use PIO */
57 if (!HDATA(instance)->dma_bounce_buffer) {
58 HDATA(instance)->dma_bounce_len = 0;
59 return 1;
60 }
61
62 /* get the physical address of the bounce buffer */
63 addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
64
65 /* the bounce buffer may not be in the first 16M of physmem */
66 if (addr & A2091_XFER_MASK) {
67 /* we could use chipmem... maybe later */
68 kfree (HDATA(instance)->dma_bounce_buffer);
69 HDATA(instance)->dma_bounce_buffer = NULL;
70 HDATA(instance)->dma_bounce_len = 0;
71 return 1;
72 }
73
74 if (!dir_in) {
Boaz Harroshf2c1afa2007-09-09 20:57:05 +030075 /* copy to bounce buffer for a write */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 memcpy (HDATA(instance)->dma_bounce_buffer,
77 cmd->SCp.ptr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 }
80
81 /* setup dma direction */
82 if (!dir_in)
83 cntr |= CNTR_DDIR;
84
85 /* remember direction */
86 HDATA(cmd->device->host)->dma_dir = dir_in;
87
88 DMA(cmd->device->host)->CNTR = cntr;
89
90 /* setup DMA *physical* address */
91 DMA(cmd->device->host)->ACR = addr;
92
93 if (dir_in){
94 /* invalidate any cache */
95 cache_clear (addr, cmd->SCp.this_residual);
96 }else{
97 /* push any dirty cache */
98 cache_push (addr, cmd->SCp.this_residual);
99 }
100 /* start DMA */
101 DMA(cmd->device->host)->ST_DMA = 1;
102
103 /* return success */
104 return 0;
105}
106
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200107static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int status)
109{
110 /* disable SCSI interrupts */
111 unsigned short cntr = CNTR_PDMD;
112
113 if (!HDATA(instance)->dma_dir)
114 cntr |= CNTR_DDIR;
115
116 /* disable SCSI interrupts */
117 DMA(instance)->CNTR = cntr;
118
119 /* flush if we were reading */
120 if (HDATA(instance)->dma_dir) {
121 DMA(instance)->FLUSH = 1;
122 while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
123 ;
124 }
125
126 /* clear a possible interrupt */
127 DMA(instance)->CINT = 1;
128
129 /* stop DMA */
130 DMA(instance)->SP_DMA = 1;
131
132 /* restore the CONTROL bits (minus the direction flag) */
133 DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
134
135 /* copy from a bounce buffer, if necessary */
136 if (status && HDATA(instance)->dma_bounce_buffer) {
Boaz Harroshf2c1afa2007-09-09 20:57:05 +0300137 if( HDATA(instance)->dma_dir )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 memcpy (SCpnt->SCp.ptr,
139 HDATA(instance)->dma_bounce_buffer,
140 SCpnt->SCp.this_residual);
Boaz Harroshf2c1afa2007-09-09 20:57:05 +0300141 kfree (HDATA(instance)->dma_bounce_buffer);
142 HDATA(instance)->dma_bounce_buffer = NULL;
143 HDATA(instance)->dma_bounce_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145}
146
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100147int __init a2091_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 static unsigned char called = 0;
150 struct Scsi_Host *instance;
151 unsigned long address;
152 struct zorro_dev *z = NULL;
153 wd33c93_regs regs;
154 int num_a2091 = 0;
155
156 if (!MACH_IS_AMIGA || called)
157 return 0;
158 called = 1;
159
160 tpnt->proc_name = "A2091";
161 tpnt->proc_info = &wd33c93_proc_info;
162
163 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
164 if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
165 z->id != ZORRO_PROD_CBM_A590_A2091_2)
166 continue;
167 address = z->resource.start;
168 if (!request_mem_region(address, 256, "wd33c93"))
169 continue;
170
171 instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
172 if (instance == NULL) {
173 release_mem_region(address, 256);
174 continue;
175 }
176 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);
182 wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
Thomas Gleixner1d6f3592006-07-01 19:29:42 -0700183 request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 instance);
185 DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
186 num_a2091++;
187 }
188
189 return num_a2091;
190}
191
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200192static int a2091_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 /* FIXME perform bus-specific reset */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400195
Jeff Garzik df0ae242005-05-28 07:57:14 -0400196 /* FIXME 2: kill this function, and let midlayer fall back
197 to the same action, calling wd33c93_host_reset() */
198
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400199 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400201 spin_unlock_irq(cmd->device->host->host_lock);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return SUCCESS;
204}
205
206#define HOSTS_C
207
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100208static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .proc_name = "A2901",
210 .name = "Commodore A2091/A590 SCSI",
211 .detect = a2091_detect,
212 .release = a2091_release,
213 .queuecommand = wd33c93_queuecommand,
214 .eh_abort_handler = wd33c93_abort,
215 .eh_bus_reset_handler = a2091_bus_reset,
216 .eh_host_reset_handler = wd33c93_host_reset,
217 .can_queue = CAN_QUEUE,
218 .this_id = 7,
219 .sg_tablesize = SG_ALL,
220 .cmd_per_lun = CMD_PER_LUN,
221 .use_clustering = DISABLE_CLUSTERING
222};
223
224
225#include "scsi_module.c"
226
227int a2091_release(struct Scsi_Host *instance)
228{
229#ifdef MODULE
230 DMA(instance)->CNTR = 0;
231 release_mem_region(ZTWO_PADDR(instance->base), 256);
232 free_irq(IRQ_AMIGA_PORTS, instance);
233 wd33c93_release();
234#endif
235 return 1;
236}
237
238MODULE_LICENSE("GPL");