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