Merge branch 'master' into upstream
[pandora-kernel.git] / drivers / scsi / a2091.c
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blkdev.h>
4 #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
26 static irqreturn_t a2091_intr (int irq, void *_instance)
27 {
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
42 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
43 {
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 */
49     if (addr & A2091_XFER_MASK ||
50         (!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
51     {
52         HDATA(instance)->dma_bounce_len = (cmd->SCp.this_residual + 511)
53             & ~0x1ff;
54         HDATA(instance)->dma_bounce_buffer =
55             kmalloc (HDATA(instance)->dma_bounce_len, GFP_KERNEL);
56         
57         /* can't allocate memory; use PIO */
58         if (!HDATA(instance)->dma_bounce_buffer) {
59             HDATA(instance)->dma_bounce_len = 0;
60             return 1;
61         }
62
63         /* get the physical address of the bounce buffer */
64         addr = virt_to_bus(HDATA(instance)->dma_bounce_buffer);
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 */
69             kfree (HDATA(instance)->dma_bounce_buffer);
70             HDATA(instance)->dma_bounce_buffer = NULL;
71             HDATA(instance)->dma_bounce_len = 0;
72             return 1;
73         }
74
75         if (!dir_in) {
76             /* copy to bounce buffer for a write */
77             if (cmd->use_sg)
78 #if 0
79                 panic ("scsi%ddma: incomplete s/g support",
80                        instance->host_no);
81 #else
82                 memcpy (HDATA(instance)->dma_bounce_buffer,
83                         cmd->SCp.ptr, cmd->SCp.this_residual);
84 #endif
85             else
86                 memcpy (HDATA(instance)->dma_bounce_buffer,
87                         cmd->request_buffer, cmd->request_bufflen);
88         }
89     }
90
91     /* setup dma direction */
92     if (!dir_in)
93         cntr |= CNTR_DDIR;
94
95     /* remember direction */
96     HDATA(cmd->device->host)->dma_dir = dir_in;
97
98     DMA(cmd->device->host)->CNTR = cntr;
99
100     /* setup DMA *physical* address */
101     DMA(cmd->device->host)->ACR = addr;
102
103     if (dir_in){
104         /* invalidate any cache */
105         cache_clear (addr, cmd->SCp.this_residual);
106     }else{
107         /* push any dirty cache */
108         cache_push (addr, cmd->SCp.this_residual);
109       }
110     /* start DMA */
111     DMA(cmd->device->host)->ST_DMA = 1;
112
113     /* return success */
114     return 0;
115 }
116
117 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
118                       int status)
119 {
120     /* disable SCSI interrupts */
121     unsigned short cntr = CNTR_PDMD;
122
123     if (!HDATA(instance)->dma_dir)
124             cntr |= CNTR_DDIR;
125
126     /* disable SCSI interrupts */
127     DMA(instance)->CNTR = cntr;
128
129     /* flush if we were reading */
130     if (HDATA(instance)->dma_dir) {
131         DMA(instance)->FLUSH = 1;
132         while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
133             ;
134     }
135
136     /* clear a possible interrupt */
137     DMA(instance)->CINT = 1;
138
139     /* stop DMA */
140     DMA(instance)->SP_DMA = 1;
141
142     /* restore the CONTROL bits (minus the direction flag) */
143     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
144
145     /* copy from a bounce buffer, if necessary */
146     if (status && HDATA(instance)->dma_bounce_buffer) {
147         if (SCpnt && SCpnt->use_sg) {
148 #if 0
149             panic ("scsi%d: incomplete s/g support",
150                    instance->host_no);
151 #else
152             if( HDATA(instance)->dma_dir )
153                 memcpy (SCpnt->SCp.ptr, 
154                         HDATA(instance)->dma_bounce_buffer,
155                         SCpnt->SCp.this_residual);
156             kfree (HDATA(instance)->dma_bounce_buffer);
157             HDATA(instance)->dma_bounce_buffer = NULL;
158             HDATA(instance)->dma_bounce_len = 0;
159             
160 #endif
161         } else {
162             if (HDATA(instance)->dma_dir && SCpnt)
163                 memcpy (SCpnt->request_buffer,
164                         HDATA(instance)->dma_bounce_buffer,
165                         SCpnt->request_bufflen);
166
167             kfree (HDATA(instance)->dma_bounce_buffer);
168             HDATA(instance)->dma_bounce_buffer = NULL;
169             HDATA(instance)->dma_bounce_len = 0;
170         }
171     }
172 }
173
174 int __init a2091_detect(struct scsi_host_template *tpnt)
175 {
176     static unsigned char called = 0;
177     struct Scsi_Host *instance;
178     unsigned long address;
179     struct zorro_dev *z = NULL;
180     wd33c93_regs regs;
181     int num_a2091 = 0;
182
183     if (!MACH_IS_AMIGA || called)
184         return 0;
185     called = 1;
186
187     tpnt->proc_name = "A2091";
188     tpnt->proc_info = &wd33c93_proc_info;
189
190     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
191         if (z->id != ZORRO_PROD_CBM_A590_A2091_1 &&
192             z->id != ZORRO_PROD_CBM_A590_A2091_2)
193             continue;
194         address = z->resource.start;
195         if (!request_mem_region(address, 256, "wd33c93"))
196             continue;
197
198         instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
199         if (instance == NULL) {
200             release_mem_region(address, 256);
201             continue;
202         }
203         instance->base = ZTWO_VADDR(address);
204         instance->irq = IRQ_AMIGA_PORTS;
205         instance->unique_id = z->slotaddr;
206         DMA(instance)->DAWR = DAWR_A2091;
207         regs.SASR = &(DMA(instance)->SASR);
208         regs.SCMD = &(DMA(instance)->SCMD);
209         wd33c93_init(instance, regs, dma_setup, dma_stop, WD33C93_FS_8_10);
210         request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED, "A2091 SCSI",
211                     instance);
212         DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
213         num_a2091++;
214     }
215
216     return num_a2091;
217 }
218
219 static int a2091_bus_reset(struct scsi_cmnd *cmd)
220 {
221         /* FIXME perform bus-specific reset */
222
223         /* FIXME 2: kill this function, and let midlayer fall back
224            to the same action, calling wd33c93_host_reset() */
225
226         spin_lock_irq(cmd->device->host->host_lock);
227         wd33c93_host_reset(cmd);
228         spin_unlock_irq(cmd->device->host->host_lock);
229
230         return SUCCESS;
231 }
232
233 #define HOSTS_C
234
235 static struct scsi_host_template driver_template = {
236         .proc_name              = "A2901",
237         .name                   = "Commodore A2091/A590 SCSI",
238         .detect                 = a2091_detect,
239         .release                = a2091_release,
240         .queuecommand           = wd33c93_queuecommand,
241         .eh_abort_handler       = wd33c93_abort,
242         .eh_bus_reset_handler   = a2091_bus_reset,
243         .eh_host_reset_handler  = wd33c93_host_reset,
244         .can_queue              = CAN_QUEUE,
245         .this_id                = 7,
246         .sg_tablesize           = SG_ALL,
247         .cmd_per_lun            = CMD_PER_LUN,
248         .use_clustering         = DISABLE_CLUSTERING
249 };
250
251
252 #include "scsi_module.c"
253
254 int a2091_release(struct Scsi_Host *instance)
255 {
256 #ifdef MODULE
257         DMA(instance)->CNTR = 0;
258         release_mem_region(ZTWO_PADDR(instance->base), 256);
259         free_irq(IRQ_AMIGA_PORTS, instance);
260         wd33c93_release();
261 #endif
262         return 1;
263 }
264
265 MODULE_LICENSE("GPL");