Import 2.3.18pre1
[davej-history.git] / drivers / scsi / gvp11.c
blob365351cb95e7cad0ee6545f229d6e4aa77e9b253
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 struct proc_dir_entry proc_scsi_gvp11 = {
25 PROC_SCSI_GVP11, 5, "GVP11",
26 S_IFDIR | S_IRUGO | S_IXUGO, 2
29 #define DMA(ptr) ((gvp11_scsiregs *)((ptr)->base))
30 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
32 static struct Scsi_Host *first_instance = NULL;
33 static Scsi_Host_Template *gvp11_template;
35 static void gvp11_intr (int irq, void *dummy, struct pt_regs *fp)
37 unsigned long flags;
38 unsigned int status;
39 struct Scsi_Host *instance;
41 for (instance = first_instance; instance &&
42 instance->hostt == gvp11_template; instance = instance->next)
44 status = DMA(instance)->CNTR;
45 if (!(status & GVP11_DMAC_INT_PENDING))
46 continue;
48 spin_lock_irqsave(&io_request_lock, flags);
49 wd33c93_intr (instance);
50 spin_unlock_irqrestore(&io_request_lock, flags);
54 static int gvp11_xfer_mask = 0;
56 void gvp11_setup (char *str, int *ints)
58 gvp11_xfer_mask = ints[1];
61 static int dma_setup (Scsi_Cmnd *cmd, int dir_in)
63 unsigned short cntr = GVP11_DMAC_INT_ENABLE;
64 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
65 int bank_mask;
66 static int scsi_alloc_out_of_range = 0;
68 /* use bounce buffer if the physical address is bad */
69 if (addr & HDATA(cmd->host)->dma_xfer_mask ||
70 (!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
72 HDATA(cmd->host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
73 & ~0x1ff;
75 if( !scsi_alloc_out_of_range ) {
76 HDATA(cmd->host)->dma_bounce_buffer =
77 scsi_malloc (HDATA(cmd->host)->dma_bounce_len);
78 HDATA(cmd->host)->dma_buffer_pool = BUF_SCSI_ALLOCED;
81 if ( scsi_alloc_out_of_range || !HDATA(cmd->host)->dma_bounce_buffer) {
82 HDATA(cmd->host)->dma_bounce_buffer =
83 amiga_chip_alloc(HDATA(cmd->host)->dma_bounce_len);
85 if(!HDATA(cmd->host)->dma_bounce_buffer)
87 HDATA(cmd->host)->dma_bounce_len = 0;
88 return 1;
91 HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
94 /* check if the address of the bounce buffer is OK */
95 addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
97 if (addr & HDATA(cmd->host)->dma_xfer_mask) {
98 /* fall back to Chip RAM if address out of range */
99 if( HDATA(cmd->host)->dma_buffer_pool == BUF_SCSI_ALLOCED) {
100 scsi_free (HDATA(cmd->host)->dma_bounce_buffer,
101 HDATA(cmd->host)->dma_bounce_len);
102 scsi_alloc_out_of_range = 1;
103 } else {
104 amiga_chip_free (HDATA(cmd->host)->dma_bounce_buffer);
107 HDATA(cmd->host)->dma_bounce_buffer =
108 amiga_chip_alloc(HDATA(cmd->host)->dma_bounce_len);
110 if(!HDATA(cmd->host)->dma_bounce_buffer)
112 HDATA(cmd->host)->dma_bounce_len = 0;
113 return 1;
116 addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
117 HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
120 if (!dir_in) {
121 /* copy to bounce buffer for a write */
122 memcpy (HDATA(cmd->host)->dma_bounce_buffer,
123 cmd->SCp.ptr, cmd->SCp.this_residual);
127 /* setup dma direction */
128 if (!dir_in)
129 cntr |= GVP11_DMAC_DIR_WRITE;
131 HDATA(cmd->host)->dma_dir = dir_in;
132 DMA(cmd->host)->CNTR = cntr;
134 /* setup DMA *physical* address */
135 DMA(cmd->host)->ACR = addr;
137 if (dir_in)
138 /* invalidate any cache */
139 cache_clear (addr, cmd->SCp.this_residual);
140 else
141 /* push any dirty cache */
142 cache_push (addr, cmd->SCp.this_residual);
144 if ((bank_mask = (~HDATA(cmd->host)->dma_xfer_mask >> 18) & 0x01c0))
145 DMA(cmd->host)->BANK = bank_mask & (addr >> 18);
147 /* start DMA */
148 DMA(cmd->host)->ST_DMA = 1;
150 /* return success */
151 return 0;
154 static void dma_stop (struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
155 int status)
157 /* stop DMA */
158 DMA(instance)->SP_DMA = 1;
159 /* remove write bit from CONTROL bits */
160 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
162 /* copy from a bounce buffer, if necessary */
163 if (status && HDATA(instance)->dma_bounce_buffer) {
164 if (HDATA(instance)->dma_dir && SCpnt)
165 memcpy (SCpnt->SCp.ptr,
166 HDATA(instance)->dma_bounce_buffer,
167 SCpnt->SCp.this_residual);
169 if (HDATA(instance)->dma_buffer_pool == BUF_SCSI_ALLOCED)
170 scsi_free (HDATA(instance)->dma_bounce_buffer,
171 HDATA(instance)->dma_bounce_len);
172 else
173 amiga_chip_free(HDATA(instance)->dma_bounce_buffer);
175 HDATA(instance)->dma_bounce_buffer = NULL;
176 HDATA(instance)->dma_bounce_len = 0;
180 static int num_gvp11 = 0;
182 #define CHECK_WD33C93
184 int __init gvp11_detect(Scsi_Host_Template *tpnt)
186 static unsigned char called = 0;
187 struct Scsi_Host *instance;
188 caddr_t address;
189 unsigned int epc;
190 unsigned int key = 0, skey;
191 const struct ConfigDev *cd;
192 unsigned int default_dma_xfer_mask;
193 #ifdef CHECK_WD33C93
194 volatile unsigned char *sasr_3393, *scmd_3393;
195 unsigned char save_sasr;
196 unsigned char q, qq;
197 #endif
199 if (!MACH_IS_AMIGA || called)
200 return 0;
201 called = 1;
203 tpnt->proc_dir = &proc_scsi_gvp11;
204 tpnt->proc_info = &wd33c93_proc_info;
206 while (1) {
208 * This should (hopefully) be the correct way to identify
209 * all the different GVP SCSI controllers (except for the
210 * SERIES I though).
212 skey = key;
214 if ((key = zorro_find(ZORRO_PROD_GVP_COMBO_030_R3_SCSI, 0, skey)) ||
215 (key = zorro_find(ZORRO_PROD_GVP_SERIES_II, 0, skey)))
216 default_dma_xfer_mask = ~0x00ffffff;
217 else if ((key = zorro_find(ZORRO_PROD_GVP_GFORCE_030_SCSI, 0, skey)) ||
218 (key = zorro_find(ZORRO_PROD_GVP_A530_SCSI, 0, skey)) ||
219 (key = zorro_find(ZORRO_PROD_GVP_COMBO_030_R4_SCSI, 0, skey)))
220 default_dma_xfer_mask = ~0x01ffffff;
221 else if ((key = zorro_find(ZORRO_PROD_GVP_A1291, 0, skey)) ||
222 (key = zorro_find(ZORRO_PROD_GVP_GFORCE_040_SCSI_1, 0, skey)))
223 default_dma_xfer_mask = ~0x07ffffff;
224 else
225 break;
227 cd = zorro_get_board(key);
228 address = cd->cd_BoardAddr;
231 * Rumors state that some GVP ram boards use the same product
232 * code as the SCSI controllers. Therefore if the board-size
233 * is not 64KB we asume it is a ram board and bail out.
235 if (cd->cd_BoardSize != 0x10000)
236 continue;
238 #ifdef CHECK_WD33C93
241 * These darn GVP boards are a problem - it can be tough to tell
242 * whether or not they include a SCSI controller. This is the
243 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
244 * probes for a WD33c93 chip: If we find one, it's extremely
245 * likely that this card supports SCSI, regardless of Product_
246 * Code, Board_Size, etc.
249 /* Get pointers to the presumed register locations and save contents */
251 sasr_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SASR);
252 scmd_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SCMD);
253 save_sasr = *sasr_3393;
255 /* First test the AuxStatus Reg */
257 q = *sasr_3393; /* read it */
258 if (q & 0x08) /* bit 3 should always be clear */
259 continue;
260 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
261 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
262 *sasr_3393 = save_sasr; /* Oops - restore this byte */
263 continue;
265 if (*sasr_3393 != q) { /* should still read the same */
266 *sasr_3393 = save_sasr; /* Oops - restore this byte */
267 continue;
269 if (*scmd_3393 != q) /* and so should the image at 0x1f */
270 continue;
273 /* Ok, we probably have a wd33c93, but let's check a few other places
274 * for good measure. Make sure that this works for both 'A and 'B
275 * chip versions.
278 *sasr_3393 = WD_SCSI_STATUS;
279 q = *scmd_3393;
280 *sasr_3393 = WD_SCSI_STATUS;
281 *scmd_3393 = ~q;
282 *sasr_3393 = WD_SCSI_STATUS;
283 qq = *scmd_3393;
284 *sasr_3393 = WD_SCSI_STATUS;
285 *scmd_3393 = q;
286 if (qq != q) /* should be read only */
287 continue;
288 *sasr_3393 = 0x1e; /* this register is unimplemented */
289 q = *scmd_3393;
290 *sasr_3393 = 0x1e;
291 *scmd_3393 = ~q;
292 *sasr_3393 = 0x1e;
293 qq = *scmd_3393;
294 *sasr_3393 = 0x1e;
295 *scmd_3393 = q;
296 if (qq != q || qq != 0xff) /* should be read only, all 1's */
297 continue;
298 *sasr_3393 = WD_TIMEOUT_PERIOD;
299 q = *scmd_3393;
300 *sasr_3393 = WD_TIMEOUT_PERIOD;
301 *scmd_3393 = ~q;
302 *sasr_3393 = WD_TIMEOUT_PERIOD;
303 qq = *scmd_3393;
304 *sasr_3393 = WD_TIMEOUT_PERIOD;
305 *scmd_3393 = q;
306 if (qq != (~q & 0xff)) /* should be read/write */
307 continue;
308 #endif
310 instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
311 instance->base = (unsigned char *)ZTWO_VADDR(address);
312 instance->irq = IRQ_AMIGA_PORTS;
313 instance->unique_id = key;
315 if (gvp11_xfer_mask)
316 HDATA(instance)->dma_xfer_mask = gvp11_xfer_mask;
317 else
318 HDATA(instance)->dma_xfer_mask = default_dma_xfer_mask;
321 DMA(instance)->secret2 = 1;
322 DMA(instance)->secret1 = 0;
323 DMA(instance)->secret3 = 15;
324 while (DMA(instance)->CNTR & GVP11_DMAC_BUSY) ;
325 DMA(instance)->CNTR = 0;
327 DMA(instance)->BANK = 0;
329 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
332 * Check for 14MHz SCSI clock
334 if (epc & GVP_SCSICLKMASK)
335 wd33c93_init(instance, (wd33c93_regs *)&(DMA(instance)->SASR),
336 dma_setup, dma_stop, WD33C93_FS_8_10);
337 else
338 wd33c93_init(instance, (wd33c93_regs *)&(DMA(instance)->SASR),
339 dma_setup, dma_stop, WD33C93_FS_12_15);
341 if (num_gvp11++ == 0) {
342 first_instance = instance;
343 gvp11_template = instance->hostt;
344 request_irq(IRQ_AMIGA_PORTS, gvp11_intr, SA_SHIRQ,
345 "GVP11 SCSI", gvp11_intr);
347 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
348 zorro_config_board(key, 0);
351 return num_gvp11;
355 #ifdef MODULE
357 #define HOSTS_C
359 #include "gvp11.h"
361 Scsi_Host_Template driver_template = GVP11_SCSI;
363 #include "scsi_module.c"
365 #endif
367 int gvp11_release(struct Scsi_Host *instance)
369 #ifdef MODULE
370 DMA(instance)->CNTR = 0;
371 zorro_unconfig_board(instance->unique_id, 0);
372 if (--num_gvp11 == 0)
373 free_irq(IRQ_AMIGA_PORTS, gvp11_intr);
374 wd33c93_release();
375 #endif
376 return 1;