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