- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / gvp11.c
blob116ddbb58d58ed891626c5ab69e7f4f56c5c0285
1 #include <linux/types.h>
2 #include <linux/mm.h>
3 #include <linux/blk.h>
4 #include <linux/sched.h>
5 #include <linux/version.h>
6 #include <linux/init.h>
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>
17 #include "scsi.h"
18 #include "hosts.h"
19 #include "wd33c93.h"
20 #include "gvp11.h"
22 #include<linux/stat.h>
24 #define DMA(ptr) ((gvp11_scsiregs *)((ptr)->base))
25 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
27 static struct Scsi_Host *first_instance = NULL;
28 static Scsi_Host_Template *gvp11_template;
30 static void gvp11_intr (int irq, void *dummy, struct pt_regs *fp)
32 unsigned long flags;
33 unsigned int status;
34 struct Scsi_Host *instance;
36 for (instance = first_instance; instance &&
37 instance->hostt == gvp11_template; instance = instance->next)
39 status = DMA(instance)->CNTR;
40 if (!(status & GVP11_DMAC_INT_PENDING))
41 continue;
43 spin_lock_irqsave(&io_request_lock, flags);
44 wd33c93_intr (instance);
45 spin_unlock_irqrestore(&io_request_lock, flags);
49 static int gvp11_xfer_mask = 0;
51 void gvp11_setup (char *str, int *ints)
53 gvp11_xfer_mask = ints[1];
56 static int dma_setup (Scsi_Cmnd *cmd, int dir_in)
58 unsigned short cntr = GVP11_DMAC_INT_ENABLE;
59 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
60 int bank_mask;
61 static int scsi_alloc_out_of_range = 0;
63 /* use bounce buffer if the physical address is bad */
64 if (addr & HDATA(cmd->host)->dma_xfer_mask ||
65 (!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
67 HDATA(cmd->host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
68 & ~0x1ff;
70 if( !scsi_alloc_out_of_range ) {
71 HDATA(cmd->host)->dma_bounce_buffer =
72 scsi_malloc (HDATA(cmd->host)->dma_bounce_len);
73 HDATA(cmd->host)->dma_buffer_pool = BUF_SCSI_ALLOCED;
76 if ( scsi_alloc_out_of_range || !HDATA(cmd->host)->dma_bounce_buffer) {
77 HDATA(cmd->host)->dma_bounce_buffer =
78 amiga_chip_alloc(HDATA(cmd->host)->dma_bounce_len,
79 "GVP II SCSI Bounce Buffer");
81 if(!HDATA(cmd->host)->dma_bounce_buffer)
83 HDATA(cmd->host)->dma_bounce_len = 0;
84 return 1;
87 HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
90 /* check if the address of the bounce buffer is OK */
91 addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
93 if (addr & HDATA(cmd->host)->dma_xfer_mask) {
94 /* fall back to Chip RAM if address out of range */
95 if( HDATA(cmd->host)->dma_buffer_pool == BUF_SCSI_ALLOCED) {
96 scsi_free (HDATA(cmd->host)->dma_bounce_buffer,
97 HDATA(cmd->host)->dma_bounce_len);
98 scsi_alloc_out_of_range = 1;
99 } else {
100 amiga_chip_free (HDATA(cmd->host)->dma_bounce_buffer);
103 HDATA(cmd->host)->dma_bounce_buffer =
104 amiga_chip_alloc(HDATA(cmd->host)->dma_bounce_len,
105 "GVP II SCSI Bounce Buffer");
107 if(!HDATA(cmd->host)->dma_bounce_buffer)
109 HDATA(cmd->host)->dma_bounce_len = 0;
110 return 1;
113 addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
114 HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
117 if (!dir_in) {
118 /* copy to bounce buffer for a write */
119 memcpy (HDATA(cmd->host)->dma_bounce_buffer,
120 cmd->SCp.ptr, cmd->SCp.this_residual);
124 /* setup dma direction */
125 if (!dir_in)
126 cntr |= GVP11_DMAC_DIR_WRITE;
128 HDATA(cmd->host)->dma_dir = dir_in;
129 DMA(cmd->host)->CNTR = cntr;
131 /* setup DMA *physical* address */
132 DMA(cmd->host)->ACR = addr;
134 if (dir_in)
135 /* invalidate any cache */
136 cache_clear (addr, cmd->SCp.this_residual);
137 else
138 /* push any dirty cache */
139 cache_push (addr, cmd->SCp.this_residual);
141 if ((bank_mask = (~HDATA(cmd->host)->dma_xfer_mask >> 18) & 0x01c0))
142 DMA(cmd->host)->BANK = bank_mask & (addr >> 18);
144 /* start DMA */
145 DMA(cmd->host)->ST_DMA = 1;
147 /* return success */
148 return 0;
151 static void dma_stop (struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
152 int status)
154 /* stop DMA */
155 DMA(instance)->SP_DMA = 1;
156 /* remove write bit from CONTROL bits */
157 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
159 /* copy from a bounce buffer, if necessary */
160 if (status && HDATA(instance)->dma_bounce_buffer) {
161 if (HDATA(instance)->dma_dir && SCpnt)
162 memcpy (SCpnt->SCp.ptr,
163 HDATA(instance)->dma_bounce_buffer,
164 SCpnt->SCp.this_residual);
166 if (HDATA(instance)->dma_buffer_pool == BUF_SCSI_ALLOCED)
167 scsi_free (HDATA(instance)->dma_bounce_buffer,
168 HDATA(instance)->dma_bounce_len);
169 else
170 amiga_chip_free(HDATA(instance)->dma_bounce_buffer);
172 HDATA(instance)->dma_bounce_buffer = NULL;
173 HDATA(instance)->dma_bounce_len = 0;
177 static int num_gvp11 = 0;
179 #define CHECK_WD33C93
181 int __init gvp11_detect(Scsi_Host_Template *tpnt)
183 static unsigned char called = 0;
184 struct Scsi_Host *instance;
185 unsigned long address;
186 unsigned int epc;
187 struct zorro_dev *z = NULL;
188 unsigned int default_dma_xfer_mask;
189 #ifdef CHECK_WD33C93
190 volatile unsigned char *sasr_3393, *scmd_3393;
191 unsigned char save_sasr;
192 unsigned char q, qq;
193 #endif
195 if (!MACH_IS_AMIGA || called)
196 return 0;
197 called = 1;
199 tpnt->proc_name = "GVP11";
200 tpnt->proc_info = &wd33c93_proc_info;
202 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
204 * This should (hopefully) be the correct way to identify
205 * all the different GVP SCSI controllers (except for the
206 * SERIES I though).
209 if (z->id == ZORRO_PROD_GVP_COMBO_030_R3_SCSI ||
210 z->id == ZORRO_PROD_GVP_SERIES_II)
211 default_dma_xfer_mask = ~0x00ffffff;
212 else if (z->id == ZORRO_PROD_GVP_GFORCE_030_SCSI ||
213 z->id == ZORRO_PROD_GVP_A530_SCSI ||
214 z->id == ZORRO_PROD_GVP_COMBO_030_R4_SCSI)
215 default_dma_xfer_mask = ~0x01ffffff;
216 else if (z->id == ZORRO_PROD_GVP_A1291 ||
217 z->id == ZORRO_PROD_GVP_GFORCE_040_SCSI_1)
218 default_dma_xfer_mask = ~0x07ffffff;
219 else
220 continue;
223 * Rumors state that some GVP ram boards use the same product
224 * code as the SCSI controllers. Therefore if the board-size
225 * is not 64KB we asume it is a ram board and bail out.
227 if (z->resource.end-z->resource.start != 0xffff)
228 continue;
230 address = z->resource.start;
231 if (!request_mem_region(address, 256, "wd33c93"))
232 continue;
234 #ifdef CHECK_WD33C93
237 * These darn GVP boards are a problem - it can be tough to tell
238 * whether or not they include a SCSI controller. This is the
239 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
240 * probes for a WD33c93 chip: If we find one, it's extremely
241 * likely that this card supports SCSI, regardless of Product_
242 * Code, Board_Size, etc.
245 /* Get pointers to the presumed register locations and save contents */
247 sasr_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SASR);
248 scmd_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SCMD);
249 save_sasr = *sasr_3393;
251 /* First test the AuxStatus Reg */
253 q = *sasr_3393; /* read it */
254 if (q & 0x08) /* bit 3 should always be clear */
255 goto release;
256 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
257 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
258 *sasr_3393 = save_sasr; /* Oops - restore this byte */
259 goto release;
261 if (*sasr_3393 != q) { /* should still read the same */
262 *sasr_3393 = save_sasr; /* Oops - restore this byte */
263 goto release;
265 if (*scmd_3393 != q) /* and so should the image at 0x1f */
266 goto release;
269 /* Ok, we probably have a wd33c93, but let's check a few other places
270 * for good measure. Make sure that this works for both 'A and 'B
271 * chip versions.
274 *sasr_3393 = WD_SCSI_STATUS;
275 q = *scmd_3393;
276 *sasr_3393 = WD_SCSI_STATUS;
277 *scmd_3393 = ~q;
278 *sasr_3393 = WD_SCSI_STATUS;
279 qq = *scmd_3393;
280 *sasr_3393 = WD_SCSI_STATUS;
281 *scmd_3393 = q;
282 if (qq != q) /* should be read only */
283 goto release;
284 *sasr_3393 = 0x1e; /* this register is unimplemented */
285 q = *scmd_3393;
286 *sasr_3393 = 0x1e;
287 *scmd_3393 = ~q;
288 *sasr_3393 = 0x1e;
289 qq = *scmd_3393;
290 *sasr_3393 = 0x1e;
291 *scmd_3393 = q;
292 if (qq != q || qq != 0xff) /* should be read only, all 1's */
293 goto release;
294 *sasr_3393 = WD_TIMEOUT_PERIOD;
295 q = *scmd_3393;
296 *sasr_3393 = WD_TIMEOUT_PERIOD;
297 *scmd_3393 = ~q;
298 *sasr_3393 = WD_TIMEOUT_PERIOD;
299 qq = *scmd_3393;
300 *sasr_3393 = WD_TIMEOUT_PERIOD;
301 *scmd_3393 = q;
302 if (qq != (~q & 0xff)) /* should be read/write */
303 goto release;
304 #endif
306 instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
307 if(instance == NULL)
308 goto release;
309 instance->base = ZTWO_VADDR(address);
310 instance->irq = IRQ_AMIGA_PORTS;
311 instance->unique_id = z->slotaddr;
313 if (gvp11_xfer_mask)
314 HDATA(instance)->dma_xfer_mask = gvp11_xfer_mask;
315 else
316 HDATA(instance)->dma_xfer_mask = default_dma_xfer_mask;
319 DMA(instance)->secret2 = 1;
320 DMA(instance)->secret1 = 0;
321 DMA(instance)->secret3 = 15;
322 while (DMA(instance)->CNTR & GVP11_DMAC_BUSY) ;
323 DMA(instance)->CNTR = 0;
325 DMA(instance)->BANK = 0;
327 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
330 * Check for 14MHz SCSI clock
332 if (epc & GVP_SCSICLKMASK)
333 wd33c93_init(instance, (wd33c93_regs *)&(DMA(instance)->SASR),
334 dma_setup, dma_stop, WD33C93_FS_8_10);
335 else
336 wd33c93_init(instance, (wd33c93_regs *)&(DMA(instance)->SASR),
337 dma_setup, dma_stop, WD33C93_FS_12_15);
339 if (num_gvp11++ == 0) {
340 first_instance = instance;
341 gvp11_template = instance->hostt;
342 request_irq(IRQ_AMIGA_PORTS, gvp11_intr, SA_SHIRQ,
343 "GVP11 SCSI", gvp11_intr);
345 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
346 continue;
348 release:
349 release_mem_region(address, 256);
352 return num_gvp11;
356 #define HOSTS_C
358 #include "gvp11.h"
360 static Scsi_Host_Template driver_template = GVP11_SCSI;
362 #include "scsi_module.c"
364 int gvp11_release(struct Scsi_Host *instance)
366 #ifdef MODULE
367 DMA(instance)->CNTR = 0;
368 release_mem_region(ZTWO_PADDR(instance->base), 256);
369 if (--num_gvp11 == 0)
370 free_irq(IRQ_AMIGA_PORTS, gvp11_intr);
371 wd33c93_release();
372 #endif
373 return 1;