Merge branch 'next' into for-linus
[linux-2.6] / drivers / scsi / gvp11.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 "gvp11.h"
20
21 #include<linux/stat.h>
22
23 #define DMA(ptr) ((gvp11_scsiregs *)((ptr)->base))
24 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
25
26 static irqreturn_t gvp11_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)->CNTR;
33     if (!(status & GVP11_DMAC_INT_PENDING))
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 gvp11_xfer_mask = 0;
43
44 void gvp11_setup (char *str, int *ints)
45 {
46     gvp11_xfer_mask = ints[1];
47 }
48
49 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
50 {
51     unsigned short cntr = GVP11_DMAC_INT_ENABLE;
52     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
53     int bank_mask;
54     static int scsi_alloc_out_of_range = 0;
55
56     /* use bounce buffer if the physical address is bad */
57     if (addr & HDATA(cmd->device->host)->dma_xfer_mask)
58     {
59         HDATA(cmd->device->host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
60             & ~0x1ff;
61
62         if( !scsi_alloc_out_of_range ) {
63             HDATA(cmd->device->host)->dma_bounce_buffer =
64                 kmalloc (HDATA(cmd->device->host)->dma_bounce_len, GFP_KERNEL);
65             HDATA(cmd->device->host)->dma_buffer_pool = BUF_SCSI_ALLOCED;
66         }
67
68         if (scsi_alloc_out_of_range ||
69             !HDATA(cmd->device->host)->dma_bounce_buffer) {
70             HDATA(cmd->device->host)->dma_bounce_buffer =
71                 amiga_chip_alloc(HDATA(cmd->device->host)->dma_bounce_len,
72                                        "GVP II SCSI Bounce Buffer");
73
74             if(!HDATA(cmd->device->host)->dma_bounce_buffer)
75             {
76                 HDATA(cmd->device->host)->dma_bounce_len = 0;
77                 return 1;
78             }
79
80             HDATA(cmd->device->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
81         }
82
83         /* check if the address of the bounce buffer is OK */
84         addr = virt_to_bus(HDATA(cmd->device->host)->dma_bounce_buffer);
85
86         if (addr & HDATA(cmd->device->host)->dma_xfer_mask) {
87             /* fall back to Chip RAM if address out of range */
88             if( HDATA(cmd->device->host)->dma_buffer_pool == BUF_SCSI_ALLOCED) {
89                 kfree (HDATA(cmd->device->host)->dma_bounce_buffer);
90                 scsi_alloc_out_of_range = 1;
91             } else {
92                 amiga_chip_free (HDATA(cmd->device->host)->dma_bounce_buffer);
93             }
94                 
95             HDATA(cmd->device->host)->dma_bounce_buffer =
96                 amiga_chip_alloc(HDATA(cmd->device->host)->dma_bounce_len,
97                                        "GVP II SCSI Bounce Buffer");
98
99             if(!HDATA(cmd->device->host)->dma_bounce_buffer)
100             {
101                 HDATA(cmd->device->host)->dma_bounce_len = 0;
102                 return 1;
103             }
104
105             addr = virt_to_bus(HDATA(cmd->device->host)->dma_bounce_buffer);
106             HDATA(cmd->device->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
107         }
108             
109         if (!dir_in) {
110             /* copy to bounce buffer for a write */
111             memcpy (HDATA(cmd->device->host)->dma_bounce_buffer,
112                     cmd->SCp.ptr, cmd->SCp.this_residual);
113         }
114     }
115
116     /* setup dma direction */
117     if (!dir_in)
118         cntr |= GVP11_DMAC_DIR_WRITE;
119
120     HDATA(cmd->device->host)->dma_dir = dir_in;
121     DMA(cmd->device->host)->CNTR = cntr;
122
123     /* setup DMA *physical* address */
124     DMA(cmd->device->host)->ACR = addr;
125
126     if (dir_in)
127         /* invalidate any cache */
128         cache_clear (addr, cmd->SCp.this_residual);
129     else
130         /* push any dirty cache */
131         cache_push (addr, cmd->SCp.this_residual);
132
133     if ((bank_mask = (~HDATA(cmd->device->host)->dma_xfer_mask >> 18) & 0x01c0))
134             DMA(cmd->device->host)->BANK = bank_mask & (addr >> 18);
135
136     /* start DMA */
137     DMA(cmd->device->host)->ST_DMA = 1;
138
139     /* return success */
140     return 0;
141 }
142
143 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
144                      int status)
145 {
146     /* stop DMA */
147     DMA(instance)->SP_DMA = 1;
148     /* remove write bit from CONTROL bits */
149     DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
150
151     /* copy from a bounce buffer, if necessary */
152     if (status && HDATA(instance)->dma_bounce_buffer) {
153         if (HDATA(instance)->dma_dir && SCpnt)
154             memcpy (SCpnt->SCp.ptr, 
155                     HDATA(instance)->dma_bounce_buffer,
156                     SCpnt->SCp.this_residual);
157         
158         if (HDATA(instance)->dma_buffer_pool == BUF_SCSI_ALLOCED)
159             kfree (HDATA(instance)->dma_bounce_buffer);
160         else
161             amiga_chip_free(HDATA(instance)->dma_bounce_buffer);
162         
163         HDATA(instance)->dma_bounce_buffer = NULL;
164         HDATA(instance)->dma_bounce_len = 0;
165     }
166 }
167
168 #define CHECK_WD33C93
169
170 int __init gvp11_detect(struct scsi_host_template *tpnt)
171 {
172     static unsigned char called = 0;
173     struct Scsi_Host *instance;
174     unsigned long address;
175     unsigned int epc;
176     struct zorro_dev *z = NULL;
177     unsigned int default_dma_xfer_mask;
178     wd33c93_regs regs;
179     int num_gvp11 = 0;
180 #ifdef CHECK_WD33C93
181     volatile unsigned char *sasr_3393, *scmd_3393;
182     unsigned char save_sasr;
183     unsigned char q, qq;
184 #endif
185
186     if (!MACH_IS_AMIGA || called)
187         return 0;
188     called = 1;
189
190     tpnt->proc_name = "GVP11";
191     tpnt->proc_info = &wd33c93_proc_info;
192
193     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
194         /* 
195          * This should (hopefully) be the correct way to identify
196          * all the different GVP SCSI controllers (except for the
197          * SERIES I though).
198          */
199
200         if (z->id == ZORRO_PROD_GVP_COMBO_030_R3_SCSI ||
201             z->id == ZORRO_PROD_GVP_SERIES_II)
202             default_dma_xfer_mask = ~0x00ffffff;
203         else if (z->id == ZORRO_PROD_GVP_GFORCE_030_SCSI ||
204                  z->id == ZORRO_PROD_GVP_A530_SCSI ||
205                  z->id == ZORRO_PROD_GVP_COMBO_030_R4_SCSI)
206             default_dma_xfer_mask = ~0x01ffffff;
207         else if (z->id == ZORRO_PROD_GVP_A1291 ||
208                  z->id == ZORRO_PROD_GVP_GFORCE_040_SCSI_1)
209             default_dma_xfer_mask = ~0x07ffffff;
210         else
211             continue;
212
213         /*
214          * Rumors state that some GVP ram boards use the same product
215          * code as the SCSI controllers. Therefore if the board-size
216          * is not 64KB we asume it is a ram board and bail out.
217          */
218         if (z->resource.end-z->resource.start != 0xffff)
219                 continue;
220
221         address = z->resource.start;
222         if (!request_mem_region(address, 256, "wd33c93"))
223             continue;
224
225 #ifdef CHECK_WD33C93
226
227         /*
228          * These darn GVP boards are a problem - it can be tough to tell
229          * whether or not they include a SCSI controller. This is the
230          * ultimate Yet-Another-GVP-Detection-Hack in that it actually
231          * probes for a WD33c93 chip: If we find one, it's extremely
232          * likely that this card supports SCSI, regardless of Product_
233          * Code, Board_Size, etc. 
234          */
235
236     /* Get pointers to the presumed register locations and save contents */
237
238         sasr_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SASR);
239         scmd_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SCMD);
240         save_sasr = *sasr_3393;
241
242     /* First test the AuxStatus Reg */
243
244         q = *sasr_3393;         /* read it */
245         if (q & 0x08)           /* bit 3 should always be clear */
246                 goto release;
247         *sasr_3393 = WD_AUXILIARY_STATUS;        /* setup indirect address */
248         if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
249                 *sasr_3393 = save_sasr; /* Oops - restore this byte */
250                 goto release;
251                 }
252         if (*sasr_3393 != q) {  /* should still read the same */
253                 *sasr_3393 = save_sasr; /* Oops - restore this byte */
254                 goto release;
255                 }
256         if (*scmd_3393 != q)    /* and so should the image at 0x1f */
257                 goto release;
258
259
260     /* Ok, we probably have a wd33c93, but let's check a few other places
261      * for good measure. Make sure that this works for both 'A and 'B    
262      * chip versions.
263      */
264
265         *sasr_3393 = WD_SCSI_STATUS;
266         q = *scmd_3393;
267         *sasr_3393 = WD_SCSI_STATUS;
268         *scmd_3393 = ~q;
269         *sasr_3393 = WD_SCSI_STATUS;
270         qq = *scmd_3393;
271         *sasr_3393 = WD_SCSI_STATUS;
272         *scmd_3393 = q;
273         if (qq != q)                    /* should be read only */
274                 goto release;
275         *sasr_3393 = 0x1e;      /* this register is unimplemented */
276         q = *scmd_3393;
277         *sasr_3393 = 0x1e;
278         *scmd_3393 = ~q;
279         *sasr_3393 = 0x1e;
280         qq = *scmd_3393;
281         *sasr_3393 = 0x1e;
282         *scmd_3393 = q;
283         if (qq != q || qq != 0xff)      /* should be read only, all 1's */
284                 goto release;
285         *sasr_3393 = WD_TIMEOUT_PERIOD;
286         q = *scmd_3393;
287         *sasr_3393 = WD_TIMEOUT_PERIOD;
288         *scmd_3393 = ~q;
289         *sasr_3393 = WD_TIMEOUT_PERIOD;
290         qq = *scmd_3393;
291         *sasr_3393 = WD_TIMEOUT_PERIOD;
292         *scmd_3393 = q;
293         if (qq != (~q & 0xff))          /* should be read/write */
294                 goto release;
295 #endif
296
297         instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
298         if(instance == NULL)
299                 goto release;
300         instance->base = ZTWO_VADDR(address);
301         instance->irq = IRQ_AMIGA_PORTS;
302         instance->unique_id = z->slotaddr;
303
304         if (gvp11_xfer_mask)
305                 HDATA(instance)->dma_xfer_mask = gvp11_xfer_mask;
306         else
307                 HDATA(instance)->dma_xfer_mask = default_dma_xfer_mask;
308
309
310         DMA(instance)->secret2 = 1;
311         DMA(instance)->secret1 = 0;
312         DMA(instance)->secret3 = 15;
313         while (DMA(instance)->CNTR & GVP11_DMAC_BUSY) ;
314         DMA(instance)->CNTR = 0;
315
316         DMA(instance)->BANK = 0;
317
318         epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
319
320         /*
321          * Check for 14MHz SCSI clock
322          */
323         regs.SASR = &(DMA(instance)->SASR);
324         regs.SCMD = &(DMA(instance)->SCMD);
325         HDATA(instance)->no_sync = 0xff;
326         HDATA(instance)->fast = 0;
327         HDATA(instance)->dma_mode = CTRL_DMA;
328         wd33c93_init(instance, regs, dma_setup, dma_stop,
329                      (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
330                                              : WD33C93_FS_12_15);
331
332         request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED, "GVP11 SCSI",
333                     instance);
334         DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
335         num_gvp11++;
336         continue;
337
338 release:
339         release_mem_region(address, 256);
340     }
341
342     return num_gvp11;
343 }
344
345 static int gvp11_bus_reset(struct scsi_cmnd *cmd)
346 {
347         /* FIXME perform bus-specific reset */
348
349         /* FIXME 2: shouldn't we no-op this function (return
350            FAILED), and fall back to host reset function,
351            wd33c93_host_reset ? */
352
353         spin_lock_irq(cmd->device->host->host_lock);
354         wd33c93_host_reset(cmd);
355         spin_unlock_irq(cmd->device->host->host_lock);
356
357         return SUCCESS;
358 }
359
360
361 #define HOSTS_C
362
363 #include "gvp11.h"
364
365 static struct scsi_host_template driver_template = {
366         .proc_name              = "GVP11",
367         .name                   = "GVP Series II SCSI",
368         .detect                 = gvp11_detect,
369         .release                = gvp11_release,
370         .queuecommand           = wd33c93_queuecommand,
371         .eh_abort_handler       = wd33c93_abort,
372         .eh_bus_reset_handler   = gvp11_bus_reset,
373         .eh_host_reset_handler  = wd33c93_host_reset,
374         .can_queue              = CAN_QUEUE,
375         .this_id                = 7,
376         .sg_tablesize           = SG_ALL,
377         .cmd_per_lun            = CMD_PER_LUN,
378         .use_clustering         = DISABLE_CLUSTERING
379 };
380
381
382 #include "scsi_module.c"
383
384 int gvp11_release(struct Scsi_Host *instance)
385 {
386 #ifdef MODULE
387     DMA(instance)->CNTR = 0;
388     release_mem_region(ZTWO_PADDR(instance->base), 256);
389     free_irq(IRQ_AMIGA_PORTS, instance);
390     wd33c93_release();
391 #endif
392     return 1;
393 }
394
395 MODULE_LICENSE("GPL");