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>
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 "hosts.h"
19 #include "wd33c93.h"
20 #include "gvp11.h"
21 
22 #include<linux/stat.h>
23 
24 #define DMA(ptr) ((gvp11_scsiregs *)((ptr)->base))
25 #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
26 
27 static struct Scsi_Host *first_instance = NULL;
28 static Scsi_Host_Template *gvp11_template;
29 
gvp11_intr(int irq,void * dummy,struct pt_regs * fp)30 static void gvp11_intr (int irq, void *dummy, struct pt_regs *fp)
31 {
32     unsigned long flags;
33     unsigned int status;
34     struct Scsi_Host *instance;
35 
36     for (instance = first_instance; instance &&
37 	 instance->hostt == gvp11_template; instance = instance->next)
38     {
39 	status = DMA(instance)->CNTR;
40 	if (!(status & GVP11_DMAC_INT_PENDING))
41 	    continue;
42 
43 	spin_lock_irqsave(&io_request_lock, flags);
44 	wd33c93_intr (instance);
45 	spin_unlock_irqrestore(&io_request_lock, flags);
46     }
47 }
48 
49 static int gvp11_xfer_mask = 0;
50 
gvp11_setup(char * str,int * ints)51 void gvp11_setup (char *str, int *ints)
52 {
53     gvp11_xfer_mask = ints[1];
54 }
55 
dma_setup(Scsi_Cmnd * cmd,int dir_in)56 static int dma_setup (Scsi_Cmnd *cmd, int dir_in)
57 {
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;
62 
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)))
66     {
67 	HDATA(cmd->host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
68 	    & ~0x1ff;
69 
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;
74 	}
75 
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");
80 
81 	    if(!HDATA(cmd->host)->dma_bounce_buffer)
82 	    {
83 		HDATA(cmd->host)->dma_bounce_len = 0;
84 		return 1;
85 	    }
86 
87 	    HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
88 	}
89 
90 	/* check if the address of the bounce buffer is OK */
91 	addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
92 
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);
101             }
102 
103 	    HDATA(cmd->host)->dma_bounce_buffer =
104 		amiga_chip_alloc(HDATA(cmd->host)->dma_bounce_len,
105 				       "GVP II SCSI Bounce Buffer");
106 
107 	    if(!HDATA(cmd->host)->dma_bounce_buffer)
108 	    {
109 		HDATA(cmd->host)->dma_bounce_len = 0;
110 		return 1;
111 	    }
112 
113 	    addr = virt_to_bus(HDATA(cmd->host)->dma_bounce_buffer);
114 	    HDATA(cmd->host)->dma_buffer_pool = BUF_CHIP_ALLOCED;
115 	}
116 
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);
121 	}
122     }
123 
124     /* setup dma direction */
125     if (!dir_in)
126 	cntr |= GVP11_DMAC_DIR_WRITE;
127 
128     HDATA(cmd->host)->dma_dir = dir_in;
129     DMA(cmd->host)->CNTR = cntr;
130 
131     /* setup DMA *physical* address */
132     DMA(cmd->host)->ACR = addr;
133 
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);
140 
141     if ((bank_mask = (~HDATA(cmd->host)->dma_xfer_mask >> 18) & 0x01c0))
142 	    DMA(cmd->host)->BANK = bank_mask & (addr >> 18);
143 
144     /* start DMA */
145     DMA(cmd->host)->ST_DMA = 1;
146 
147     /* return success */
148     return 0;
149 }
150 
dma_stop(struct Scsi_Host * instance,Scsi_Cmnd * SCpnt,int status)151 static void dma_stop (struct Scsi_Host *instance, Scsi_Cmnd *SCpnt,
152 		      int status)
153 {
154     /* stop DMA */
155     DMA(instance)->SP_DMA = 1;
156     /* remove write bit from CONTROL bits */
157     DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
158 
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);
165 
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);
171 
172 	HDATA(instance)->dma_bounce_buffer = NULL;
173 	HDATA(instance)->dma_bounce_len = 0;
174     }
175 }
176 
177 static int num_gvp11 = 0;
178 
179 #define CHECK_WD33C93
180 
gvp11_detect(Scsi_Host_Template * tpnt)181 int __init gvp11_detect(Scsi_Host_Template *tpnt)
182 {
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     wd33c93_regs regs;
190 #ifdef CHECK_WD33C93
191     volatile unsigned char *sasr_3393, *scmd_3393;
192     unsigned char save_sasr;
193     unsigned char q, qq;
194 #endif
195 
196     if (!MACH_IS_AMIGA || called)
197 	return 0;
198     called = 1;
199 
200     tpnt->proc_name = "GVP11";
201     tpnt->proc_info = &wd33c93_proc_info;
202 
203     while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
204 	/*
205 	 * This should (hopefully) be the correct way to identify
206 	 * all the different GVP SCSI controllers (except for the
207 	 * SERIES I though).
208 	 */
209 
210 	if (z->id == ZORRO_PROD_GVP_COMBO_030_R3_SCSI ||
211 	    z->id == ZORRO_PROD_GVP_SERIES_II)
212 	    default_dma_xfer_mask = ~0x00ffffff;
213 	else if (z->id == ZORRO_PROD_GVP_GFORCE_030_SCSI ||
214 		 z->id == ZORRO_PROD_GVP_A530_SCSI ||
215 		 z->id == ZORRO_PROD_GVP_COMBO_030_R4_SCSI)
216 	    default_dma_xfer_mask = ~0x01ffffff;
217 	else if (z->id == ZORRO_PROD_GVP_A1291 ||
218 		 z->id == ZORRO_PROD_GVP_GFORCE_040_SCSI_1)
219 	    default_dma_xfer_mask = ~0x07ffffff;
220 	else
221 	    continue;
222 
223 	/*
224 	 * Rumors state that some GVP ram boards use the same product
225 	 * code as the SCSI controllers. Therefore if the board-size
226 	 * is not 64KB we asume it is a ram board and bail out.
227 	 */
228 	if (z->resource.end-z->resource.start != 0xffff)
229 		continue;
230 
231 	address = z->resource.start;
232 	if (!request_mem_region(address, 256, "wd33c93"))
233 	    continue;
234 
235 #ifdef CHECK_WD33C93
236 
237 	/*
238 	 * These darn GVP boards are a problem - it can be tough to tell
239 	 * whether or not they include a SCSI controller. This is the
240 	 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
241 	 * probes for a WD33c93 chip: If we find one, it's extremely
242 	 * likely that this card supports SCSI, regardless of Product_
243 	 * Code, Board_Size, etc.
244 	 */
245 
246     /* Get pointers to the presumed register locations and save contents */
247 
248 	sasr_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SASR);
249 	scmd_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SCMD);
250 	save_sasr = *sasr_3393;
251 
252     /* First test the AuxStatus Reg */
253 
254 	q = *sasr_3393;		/* read it */
255 	if (q & 0x08)		/* bit 3 should always be clear */
256 		goto release;
257 	*sasr_3393 = WD_AUXILIARY_STATUS;	 /* setup indirect address */
258 	if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
259 		*sasr_3393 = save_sasr;	/* Oops - restore this byte */
260 		goto release;
261 		}
262 	if (*sasr_3393 != q) {	/* should still read the same */
263 		*sasr_3393 = save_sasr;	/* Oops - restore this byte */
264 		goto release;
265 		}
266 	if (*scmd_3393 != q)	/* and so should the image at 0x1f */
267 		goto release;
268 
269 
270     /* Ok, we probably have a wd33c93, but let's check a few other places
271      * for good measure. Make sure that this works for both 'A and 'B
272      * chip versions.
273      */
274 
275 	*sasr_3393 = WD_SCSI_STATUS;
276 	q = *scmd_3393;
277 	*sasr_3393 = WD_SCSI_STATUS;
278 	*scmd_3393 = ~q;
279 	*sasr_3393 = WD_SCSI_STATUS;
280 	qq = *scmd_3393;
281 	*sasr_3393 = WD_SCSI_STATUS;
282 	*scmd_3393 = q;
283 	if (qq != q)			/* should be read only */
284 		goto release;
285 	*sasr_3393 = 0x1e;	/* this register is unimplemented */
286 	q = *scmd_3393;
287 	*sasr_3393 = 0x1e;
288 	*scmd_3393 = ~q;
289 	*sasr_3393 = 0x1e;
290 	qq = *scmd_3393;
291 	*sasr_3393 = 0x1e;
292 	*scmd_3393 = q;
293 	if (qq != q || qq != 0xff)	/* should be read only, all 1's */
294 		goto release;
295 	*sasr_3393 = WD_TIMEOUT_PERIOD;
296 	q = *scmd_3393;
297 	*sasr_3393 = WD_TIMEOUT_PERIOD;
298 	*scmd_3393 = ~q;
299 	*sasr_3393 = WD_TIMEOUT_PERIOD;
300 	qq = *scmd_3393;
301 	*sasr_3393 = WD_TIMEOUT_PERIOD;
302 	*scmd_3393 = q;
303 	if (qq != (~q & 0xff))		/* should be read/write */
304 		goto release;
305 #endif
306 
307 	instance = scsi_register (tpnt, sizeof (struct WD33C93_hostdata));
308 	if(instance == NULL)
309 		goto release;
310 	instance->base = ZTWO_VADDR(address);
311 	instance->irq = IRQ_AMIGA_PORTS;
312 	instance->unique_id = z->slotaddr;
313 
314 	if (gvp11_xfer_mask)
315 		HDATA(instance)->dma_xfer_mask = gvp11_xfer_mask;
316 	else
317 		HDATA(instance)->dma_xfer_mask = default_dma_xfer_mask;
318 
319 
320 	DMA(instance)->secret2 = 1;
321 	DMA(instance)->secret1 = 0;
322 	DMA(instance)->secret3 = 15;
323 	while (DMA(instance)->CNTR & GVP11_DMAC_BUSY) ;
324 	DMA(instance)->CNTR = 0;
325 
326 	DMA(instance)->BANK = 0;
327 
328 	epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
329 
330 	/*
331 	 * Check for 14MHz SCSI clock
332 	 */
333 	regs.SASR = &(DMA(instance)->SASR);
334 	regs.SCMD = &(DMA(instance)->SCMD);
335 	wd33c93_init(instance, regs, dma_setup, dma_stop,
336 		     (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
337 					     : WD33C93_FS_12_15);
338 
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);
344 	}
345 	DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
346 	continue;
347 
348 release:
349 	release_mem_region(address, 256);
350     }
351 
352     return num_gvp11;
353 }
354 
355 
356 #define HOSTS_C
357 
358 #include "gvp11.h"
359 
360 static Scsi_Host_Template driver_template = GVP11_SCSI;
361 
362 #include "scsi_module.c"
363 
gvp11_release(struct Scsi_Host * instance)364 int gvp11_release(struct Scsi_Host *instance)
365 {
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;
374 }
375 
376 MODULE_LICENSE("GPL");
377