blob: bd5d90328ee0e24788df6c02a31ba8b6f705b13b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#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
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020022#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020025#define DMA(ptr) ((gvp11_scsiregs *)((ptr)->base))
26#define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
27
28static irqreturn_t gvp11_intr(int irq, void *_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020030 unsigned long flags;
31 unsigned int status;
32 struct Scsi_Host *instance = (struct Scsi_Host *)_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020034 status = DMA(instance)->CNTR;
35 if (!(status & GVP11_DMAC_INT_PENDING))
36 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020038 spin_lock_irqsave(instance->host_lock, flags);
39 wd33c93_intr(instance);
40 spin_unlock_irqrestore(instance->host_lock, flags);
41 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44static int gvp11_xfer_mask = 0;
45
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020046void gvp11_setup(char *str, int *ints)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020048 gvp11_xfer_mask = ints[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Henrik Kretzschmar65396412006-09-12 23:49:33 +020051static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020053 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020058 /* use bounce buffer if the physical address is bad */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (addr & HDATA(cmd->device->host)->dma_xfer_mask) {
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020060 HDATA(cmd->device->host)->dma_bounce_len =
61 (cmd->SCp.this_residual + 511) & ~0x1ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020063 if (!scsi_alloc_out_of_range) {
64 HDATA(cmd->device->host)->dma_bounce_buffer =
65 kmalloc(HDATA(cmd->device->host)->dma_bounce_len,
66 GFP_KERNEL);
67 HDATA(cmd->device->host)->dma_buffer_pool =
68 BUF_SCSI_ALLOCED;
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +020071 if (scsi_alloc_out_of_range ||
72 !HDATA(cmd->device->host)->dma_bounce_buffer) {
73 HDATA(cmd->device->host)->dma_bounce_buffer =
74 amiga_chip_alloc(HDATA(cmd->device->host)->dma_bounce_len,
75 "GVP II SCSI Bounce Buffer");
76
77 if (!HDATA(cmd->device->host)->dma_bounce_buffer) {
78 HDATA(cmd->device->host)->dma_bounce_len = 0;
79 return 1;
80 }
81
82 HDATA(cmd->device->host)->dma_buffer_pool =
83 BUF_CHIP_ALLOCED;
84 }
85
86 /* check if the address of the bounce buffer is OK */
87 addr = virt_to_bus(HDATA(cmd->device->host)->dma_bounce_buffer);
88
89 if (addr & HDATA(cmd->device->host)->dma_xfer_mask) {
90 /* fall back to Chip RAM if address out of range */
91 if (HDATA(cmd->device->host)->dma_buffer_pool ==
92 BUF_SCSI_ALLOCED) {
93 kfree(HDATA(cmd->device->host)->dma_bounce_buffer);
94 scsi_alloc_out_of_range = 1;
95 } else {
96 amiga_chip_free(HDATA(cmd->device->host)->dma_bounce_buffer);
97 }
98
99 HDATA(cmd->device->host)->dma_bounce_buffer =
100 amiga_chip_alloc(HDATA(cmd->device->host)->dma_bounce_len,
101 "GVP II SCSI Bounce Buffer");
102
103 if (!HDATA(cmd->device->host)->dma_bounce_buffer) {
104 HDATA(cmd->device->host)->dma_bounce_len = 0;
105 return 1;
106 }
107
108 addr = virt_to_bus(HDATA(cmd->device->host)->dma_bounce_buffer);
109 HDATA(cmd->device->host)->dma_buffer_pool =
110 BUF_CHIP_ALLOCED;
111 }
112
113 if (!dir_in) {
114 /* copy to bounce buffer for a write */
115 memcpy(HDATA(cmd->device->host)->dma_bounce_buffer,
116 cmd->SCp.ptr, cmd->SCp.this_residual);
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200119
120 /* setup dma direction */
121 if (!dir_in)
122 cntr |= GVP11_DMAC_DIR_WRITE;
123
124 HDATA(cmd->device->host)->dma_dir = dir_in;
125 DMA(cmd->device->host)->CNTR = cntr;
126
127 /* setup DMA *physical* address */
128 DMA(cmd->device->host)->ACR = addr;
129
130 if (dir_in) {
131 /* invalidate any cache */
132 cache_clear(addr, cmd->SCp.this_residual);
133 } else {
134 /* push any dirty cache */
135 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200138 if ((bank_mask = (~HDATA(cmd->device->host)->dma_xfer_mask >> 18) & 0x01c0))
139 DMA(cmd->device->host)->BANK = bank_mask & (addr >> 18);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200141 /* start DMA */
142 DMA(cmd->device->host)->ST_DMA = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200144 /* return success */
145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200148static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
149 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200151 /* stop DMA */
152 DMA(instance)->SP_DMA = 1;
153 /* remove write bit from CONTROL bits */
154 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200156 /* copy from a bounce buffer, if necessary */
157 if (status && HDATA(instance)->dma_bounce_buffer) {
158 if (HDATA(instance)->dma_dir && SCpnt)
159 memcpy(SCpnt->SCp.ptr,
160 HDATA(instance)->dma_bounce_buffer,
161 SCpnt->SCp.this_residual);
162
163 if (HDATA(instance)->dma_buffer_pool == BUF_SCSI_ALLOCED)
164 kfree(HDATA(instance)->dma_bounce_buffer);
165 else
166 amiga_chip_free(HDATA(instance)->dma_bounce_buffer);
167
168 HDATA(instance)->dma_bounce_buffer = NULL;
169 HDATA(instance)->dma_bounce_len = 0;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173#define CHECK_WD33C93
174
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100175int __init gvp11_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200177 static unsigned char called = 0;
178 struct Scsi_Host *instance;
179 unsigned long address;
180 unsigned int epc;
181 struct zorro_dev *z = NULL;
182 unsigned int default_dma_xfer_mask;
183 wd33c93_regs regs;
184 int num_gvp11 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#ifdef CHECK_WD33C93
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200186 volatile unsigned char *sasr_3393, *scmd_3393;
187 unsigned char save_sasr;
188 unsigned char q, qq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200191 if (!MACH_IS_AMIGA || called)
192 return 0;
193 called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200195 tpnt->proc_name = "GVP11";
196 tpnt->proc_info = &wd33c93_proc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200198 while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
199 /*
200 * This should (hopefully) be the correct way to identify
201 * all the different GVP SCSI controllers (except for the
202 * SERIES I though).
203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200205 if (z->id == ZORRO_PROD_GVP_COMBO_030_R3_SCSI ||
206 z->id == ZORRO_PROD_GVP_SERIES_II)
207 default_dma_xfer_mask = ~0x00ffffff;
208 else if (z->id == ZORRO_PROD_GVP_GFORCE_030_SCSI ||
209 z->id == ZORRO_PROD_GVP_A530_SCSI ||
210 z->id == ZORRO_PROD_GVP_COMBO_030_R4_SCSI)
211 default_dma_xfer_mask = ~0x01ffffff;
212 else if (z->id == ZORRO_PROD_GVP_A1291 ||
213 z->id == ZORRO_PROD_GVP_GFORCE_040_SCSI_1)
214 default_dma_xfer_mask = ~0x07ffffff;
215 else
216 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200218 /*
219 * Rumors state that some GVP ram boards use the same product
220 * code as the SCSI controllers. Therefore if the board-size
221 * is not 64KB we asume it is a ram board and bail out.
222 */
223 if (z->resource.end - z->resource.start != 0xffff)
224 continue;
225
226 address = z->resource.start;
227 if (!request_mem_region(address, 256, "wd33c93"))
228 continue;
229
230#ifdef CHECK_WD33C93
231
232 /*
233 * These darn GVP boards are a problem - it can be tough to tell
234 * whether or not they include a SCSI controller. This is the
235 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
236 * probes for a WD33c93 chip: If we find one, it's extremely
237 * likely that this card supports SCSI, regardless of Product_
238 * Code, Board_Size, etc.
239 */
240
241 /* Get pointers to the presumed register locations and save contents */
242
243 sasr_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SASR);
244 scmd_3393 = &(((gvp11_scsiregs *)(ZTWO_VADDR(address)))->SCMD);
245 save_sasr = *sasr_3393;
246
247 /* First test the AuxStatus Reg */
248
249 q = *sasr_3393; /* read it */
250 if (q & 0x08) /* bit 3 should always be clear */
251 goto release;
252 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
253 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
254 *sasr_3393 = save_sasr; /* Oops - restore this byte */
255 goto release;
256 }
257 if (*sasr_3393 != q) { /* should still read the same */
258 *sasr_3393 = save_sasr; /* Oops - restore this byte */
259 goto release;
260 }
261 if (*scmd_3393 != q) /* and so should the image at 0x1f */
262 goto release;
263
264 /*
265 * Ok, we probably have a wd33c93, but let's check a few other places
266 * for good measure. Make sure that this works for both 'A and 'B
267 * chip versions.
268 */
269
270 *sasr_3393 = WD_SCSI_STATUS;
271 q = *scmd_3393;
272 *sasr_3393 = WD_SCSI_STATUS;
273 *scmd_3393 = ~q;
274 *sasr_3393 = WD_SCSI_STATUS;
275 qq = *scmd_3393;
276 *sasr_3393 = WD_SCSI_STATUS;
277 *scmd_3393 = q;
278 if (qq != q) /* should be read only */
279 goto release;
280 *sasr_3393 = 0x1e; /* this register is unimplemented */
281 q = *scmd_3393;
282 *sasr_3393 = 0x1e;
283 *scmd_3393 = ~q;
284 *sasr_3393 = 0x1e;
285 qq = *scmd_3393;
286 *sasr_3393 = 0x1e;
287 *scmd_3393 = q;
288 if (qq != q || qq != 0xff) /* should be read only, all 1's */
289 goto release;
290 *sasr_3393 = WD_TIMEOUT_PERIOD;
291 q = *scmd_3393;
292 *sasr_3393 = WD_TIMEOUT_PERIOD;
293 *scmd_3393 = ~q;
294 *sasr_3393 = WD_TIMEOUT_PERIOD;
295 qq = *scmd_3393;
296 *sasr_3393 = WD_TIMEOUT_PERIOD;
297 *scmd_3393 = q;
298 if (qq != (~q & 0xff)) /* should be read/write */
299 goto release;
300#endif
301
302 instance = scsi_register(tpnt, sizeof(struct WD33C93_hostdata));
303 if (instance == NULL)
304 goto release;
305 instance->base = ZTWO_VADDR(address);
306 instance->irq = IRQ_AMIGA_PORTS;
307 instance->unique_id = z->slotaddr;
308
309 if (gvp11_xfer_mask)
310 HDATA(instance)->dma_xfer_mask = gvp11_xfer_mask;
311 else
312 HDATA(instance)->dma_xfer_mask = default_dma_xfer_mask;
313
314 DMA(instance)->secret2 = 1;
315 DMA(instance)->secret1 = 0;
316 DMA(instance)->secret3 = 15;
317 while (DMA(instance)->CNTR & GVP11_DMAC_BUSY)
318 ;
319 DMA(instance)->CNTR = 0;
320
321 DMA(instance)->BANK = 0;
322
323 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
324
325 /*
326 * Check for 14MHz SCSI clock
327 */
328 regs.SASR = &(DMA(instance)->SASR);
329 regs.SCMD = &(DMA(instance)->SCMD);
330 HDATA(instance)->no_sync = 0xff;
331 HDATA(instance)->fast = 0;
332 HDATA(instance)->dma_mode = CTRL_DMA;
333 wd33c93_init(instance, regs, dma_setup, dma_stop,
334 (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
335 : WD33C93_FS_12_15);
336
337 if (request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
338 "GVP11 SCSI", instance))
339 goto unregister;
340 DMA(instance)->CNTR = GVP11_DMAC_INT_ENABLE;
341 num_gvp11++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 continue;
343
Geert Uytterhoevend38f47a2009-01-02 11:41:24 +0100344unregister:
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200345 scsi_unregister(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346release:
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200347 release_mem_region(address, 256);
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200350 return num_gvp11;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200353static int gvp11_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 /* FIXME perform bus-specific reset */
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400356
Jeff Garzik df0ae242005-05-28 07:57:14 -0400357 /* FIXME 2: shouldn't we no-op this function (return
358 FAILED), and fall back to host reset function,
359 wd33c93_host_reset ? */
360
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400361 spin_lock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 wd33c93_host_reset(cmd);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400363 spin_unlock_irq(cmd->device->host->host_lock);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return SUCCESS;
366}
367
368
369#define HOSTS_C
370
371#include "gvp11.h"
372
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100373static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .proc_name = "GVP11",
375 .name = "GVP Series II SCSI",
376 .detect = gvp11_detect,
377 .release = gvp11_release,
378 .queuecommand = wd33c93_queuecommand,
379 .eh_abort_handler = wd33c93_abort,
380 .eh_bus_reset_handler = gvp11_bus_reset,
381 .eh_host_reset_handler = wd33c93_host_reset,
382 .can_queue = CAN_QUEUE,
383 .this_id = 7,
384 .sg_tablesize = SG_ALL,
385 .cmd_per_lun = CMD_PER_LUN,
386 .use_clustering = DISABLE_CLUSTERING
387};
388
389
390#include "scsi_module.c"
391
392int gvp11_release(struct Scsi_Host *instance)
393{
394#ifdef MODULE
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200395 DMA(instance)->CNTR = 0;
396 release_mem_region(ZTWO_PADDR(instance->base), 256);
397 free_irq(IRQ_AMIGA_PORTS, instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#endif
Geert Uytterhoevenbb17b782010-04-04 11:00:33 +0200399 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402MODULE_LICENSE("GPL");