blob: 12a991b19e5872cffe8abe0b85dc6fc7072180c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
3 *
4 * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
5 *
Finn Thain757f5ba2014-03-18 11:42:24 +11006 * VME support added by Sam Creasey
7 *
8 * TODO: modify this driver to support multiple Sun3 SCSI VME boards
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Adapted from mac_scsinew.c:
11 */
12/*
13 * Generic Macintosh NCR5380 driver
14 *
15 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
16 *
17 * derived in part from:
18 */
19/*
20 * Generic Generic NCR5380 driver
21 *
22 * Copyright 1995, Russell King
23 *
24 * ALPHA RELEASE 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/types.h>
28#include <linux/stddef.h>
29#include <linux/ctype.h>
30#include <linux/delay.h>
31
32#include <linux/module.h>
33#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/blkdev.h>
37
38#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/sun3ints.h>
41#include <asm/dvma.h>
42#include <asm/idprom.h>
43#include <asm/machines.h>
44
45/* dma on! */
46#define REAL_DMA
47
48#include "scsi.h"
49#include <scsi/scsi_host.h>
50#include "sun3_scsi.h"
Finn Thain9f6620a2014-03-18 11:42:23 +110051#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Finn Thain757f5ba2014-03-18 11:42:24 +110053extern int sun3_map_test(unsigned long, char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*#define RESET_BOOT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* #define SUPPORT_TAGS */
57
Finn Thain757f5ba2014-03-18 11:42:24 +110058#ifdef SUN3_SCSI_VME
59#define ENABLE_IRQ()
60#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
Finn Thain757f5ba2014-03-18 11:42:24 +110062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
David Howells7d12e782006-10-05 14:55:46 +010065static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int setup_can_queue = -1;
68module_param(setup_can_queue, int, 0);
69static int setup_cmd_per_lun = -1;
70module_param(setup_cmd_per_lun, int, 0);
71static int setup_sg_tablesize = -1;
72module_param(setup_sg_tablesize, int, 0);
73#ifdef SUPPORT_TAGS
74static int setup_use_tagged_queuing = -1;
75module_param(setup_use_tagged_queuing, int, 0);
76#endif
77static int setup_hostid = -1;
78module_param(setup_hostid, int, 0);
79
Henne811c9362006-10-03 19:51:59 +020080static struct scsi_cmnd *sun3_dma_setup_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Michael Schmitz2b0f8342014-05-02 20:43:01 +120082#define RESET_RUN_DONE
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define AFTER_RESET_DELAY (HZ/2)
85
86/* ms to wait after hitting dma regs */
87#define SUN3_DMA_DELAY 10
88
89/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
90#define SUN3_DVMA_BUFSIZE 0xe000
91
92/* minimum number of bytes to do dma on */
93#define SUN3_DMA_MINSIZE 128
94
95static volatile unsigned char *sun3_scsi_regp;
96static volatile struct sun3_dma_regs *dregs;
Finn Thain757f5ba2014-03-18 11:42:24 +110097#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct sun3_udc_regs *udc_regs = NULL;
Finn Thain757f5ba2014-03-18 11:42:24 +110099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static unsigned char *sun3_dma_orig_addr = NULL;
101static unsigned long sun3_dma_orig_count = 0;
102static int sun3_dma_active = 0;
103static unsigned long last_residual = 0;
104
105/*
106 * NCR 5380 register access functions
107 */
108
109static inline unsigned char sun3scsi_read(int reg)
110{
111 return( sun3_scsi_regp[reg] );
112}
113
114static inline void sun3scsi_write(int reg, int value)
115{
116 sun3_scsi_regp[reg] = value;
117}
118
Finn Thain757f5ba2014-03-18 11:42:24 +1100119#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* dma controller register access functions */
121
122static inline unsigned short sun3_udc_read(unsigned char reg)
123{
124 unsigned short ret;
125
126 dregs->udc_addr = UDC_CSR;
127 udelay(SUN3_DMA_DELAY);
128 ret = dregs->udc_data;
129 udelay(SUN3_DMA_DELAY);
130
131 return ret;
132}
133
134static inline void sun3_udc_write(unsigned short val, unsigned char reg)
135{
136 dregs->udc_addr = reg;
137 udelay(SUN3_DMA_DELAY);
138 dregs->udc_data = val;
139 udelay(SUN3_DMA_DELAY);
140}
Finn Thain757f5ba2014-03-18 11:42:24 +1100141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * XXX: status debug
145 */
146static struct Scsi_Host *default_instance;
147
148/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100149 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Purpose : initializes mac NCR5380 driver based on the
152 * command line / compile time port and irq definitions.
153 *
154 * Inputs : tpnt - template for this SCSI adapter.
155 *
156 * Returns : 1 if a host adapter was found, 0 if not.
157 *
158 */
159
Finn Thain757f5ba2014-03-18 11:42:24 +1100160static int __init sun3scsi_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Finn Thain757f5ba2014-03-18 11:42:24 +1100162 unsigned long ioaddr, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 static int called = 0;
164 struct Scsi_Host *instance;
Finn Thain757f5ba2014-03-18 11:42:24 +1100165#ifdef SUN3_SCSI_VME
166 int i;
167 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
168 IOBASE_SUN3_VMESCSI + 0x4000,
169 0 };
170 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
171 SUN3_VEC_VMESCSI1,
172 0 };
173#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* check that this machine has an onboard 5380 */
176 switch(idprom->id_machtype) {
Finn Thain757f5ba2014-03-18 11:42:24 +1100177#ifdef SUN3_SCSI_VME
178 case SM_SUN3|SM_3_160:
179 case SM_SUN3|SM_3_260:
180 break;
181#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case SM_SUN3|SM_3_50:
183 case SM_SUN3|SM_3_60:
184 break;
Finn Thain757f5ba2014-03-18 11:42:24 +1100185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 default:
188 return 0;
189 }
190
191 if(called)
192 return 0;
193
Finn Thain757f5ba2014-03-18 11:42:24 +1100194#ifdef SUN3_SCSI_VME
195 tpnt->proc_name = "Sun3 5380 VME SCSI";
196#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 tpnt->proc_name = "Sun3 5380 SCSI";
Finn Thain757f5ba2014-03-18 11:42:24 +1100198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /* setup variables */
Finn Thaind572f65f2014-11-12 16:12:00 +1100201 if (setup_can_queue > 0)
202 tpnt->can_queue = setup_can_queue;
203 if (setup_cmd_per_lun > 0)
204 tpnt->cmd_per_lun = setup_cmd_per_lun;
205 if (setup_sg_tablesize >= 0)
206 tpnt->sg_tablesize = setup_sg_tablesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (setup_hostid >= 0)
209 tpnt->this_id = setup_hostid;
210 else {
211 /* use 7 as default */
212 tpnt->this_id = 7;
213 }
214
Finn Thain757f5ba2014-03-18 11:42:24 +1100215#ifdef SUN3_SCSI_VME
216 ioaddr = 0;
217 for (i = 0; addrs[i] != 0; i++) {
218 unsigned char x;
219
220 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
221 SUN3_PAGE_TYPE_VME16);
222 irq = vecs[i];
223 sun3_scsi_regp = (unsigned char *)ioaddr;
224
225 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
226
227 if (sun3_map_test((unsigned long)dregs, &x)) {
228 unsigned short oldcsr;
229
230 oldcsr = dregs->csr;
231 dregs->csr = 0;
232 udelay(SUN3_DMA_DELAY);
233 if (dregs->csr == 0x1400)
234 break;
235
236 dregs->csr = oldcsr;
237 }
238
239 iounmap((void *)ioaddr);
240 ioaddr = 0;
241 }
242
243 if (!ioaddr)
244 return 0;
245#else
246 irq = IRQ_SUN3_SCSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
248 sun3_scsi_regp = (unsigned char *)ioaddr;
249
250 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
251
252 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
253 == NULL) {
254 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
255 return 0;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif
258#ifdef SUPPORT_TAGS
259 if (setup_use_tagged_queuing < 0)
Finn Thaind572f65f2014-11-12 16:12:00 +1100260 setup_use_tagged_queuing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#endif
262
263 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
264 if(instance == NULL)
265 return 0;
266
267 default_instance = instance;
268
269 instance->io_port = (unsigned long) ioaddr;
Finn Thain757f5ba2014-03-18 11:42:24 +1100270 instance->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 NCR5380_init(instance, 0);
273
274 instance->n_io_port = 32;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (request_irq(instance->irq, scsi_sun3_intr,
Jeff Garzik1e641662007-11-11 19:52:05 -0500277 0, "Sun3SCSI-5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#ifndef REAL_DMA
279 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
280 instance->host_no, instance->irq);
Finn Thain22f5f102014-11-12 16:11:56 +1100281 instance->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#else
283 printk("scsi%d: IRQ%d not free, bailing out\n",
284 instance->host_no, instance->irq);
285 return 0;
286#endif
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dregs->csr = 0;
290 udelay(SUN3_DMA_DELAY);
291 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
292 udelay(SUN3_DMA_DELAY);
293 dregs->fifo_count = 0;
Finn Thain757f5ba2014-03-18 11:42:24 +1100294#ifdef SUN3_SCSI_VME
295 dregs->fifo_count_hi = 0;
296 dregs->dma_addr_hi = 0;
297 dregs->dma_addr_lo = 0;
298 dregs->dma_count_hi = 0;
299 dregs->dma_count_lo = 0;
300
301 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 called = 1;
305
306#ifdef RESET_BOOT
307 sun3_scsi_reset_boot(instance);
308#endif
309
310 return 1;
311}
312
Finn Thained8b9e72014-11-12 16:11:51 +1100313static int sun3scsi_release(struct Scsi_Host *shpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Finn Thain22f5f102014-11-12 16:11:56 +1100315 if (shpnt->irq != NO_IRQ)
Jeff Garzik1e641662007-11-11 19:52:05 -0500316 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 iounmap((void *)sun3_scsi_regp);
319
Geert Uytterhoeven19b6c512011-06-13 20:39:19 +0200320 NCR5380_exit(shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322}
323
324#ifdef RESET_BOOT
325/*
326 * Our 'bus reset on boot' function
327 */
328
329static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
330{
331 unsigned long end;
332
333 NCR5380_local_declare();
334 NCR5380_setup(instance);
335
336 /*
337 * Do a SCSI reset to clean up the bus during initialization. No
338 * messing with the queues, interrupts, or locks necessary here.
339 */
340
341 printk( "Sun3 SCSI: resetting the SCSI bus..." );
342
343 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
344// sun3_disable_irq( IRQ_SUN3_SCSI );
345
346 /* get in phase */
347 NCR5380_write( TARGET_COMMAND_REG,
348 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
349
350 /* assert RST */
351 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
352
353 /* The min. reset hold time is 25us, so 40us should be enough */
354 udelay( 50 );
355
356 /* reset RST and interrupt */
357 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
358 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
359
360 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
361 barrier();
362
363 /* switch on SCSI IRQ again */
364// sun3_enable_irq( IRQ_SUN3_SCSI );
365
366 printk( " done\n" );
367}
368#endif
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370// safe bits for the CSR
371#define CSR_GOOD 0x060f
372
David Howells7d12e782006-10-05 14:55:46 +0100373static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 unsigned short csr = dregs->csr;
376 int handled = 0;
377
Finn Thain757f5ba2014-03-18 11:42:24 +1100378#ifdef SUN3_SCSI_VME
379 dregs->csr &= ~CSR_DMA_ENABLE;
380#endif
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if(csr & ~CSR_GOOD) {
383 if(csr & CSR_DMA_BUSERR) {
384 printk("scsi%d: bus error in dma\n", default_instance->host_no);
385 }
386
387 if(csr & CSR_DMA_CONFLICT) {
388 printk("scsi%d: dma conflict\n", default_instance->host_no);
389 }
390 handled = 1;
391 }
392
393 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
David Howells7d12e782006-10-05 14:55:46 +0100394 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 handled = 1;
396 }
397
398 return IRQ_RETVAL(handled);
399}
400
401/*
402 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
403 * reentering NCR5380_print_status seems to have ugly side effects
404 */
405
406/* this doesn't seem to get used at all -- sam */
407#if 0
408void sun3_sun3_debug (void)
409{
410 unsigned long flags;
411 NCR5380_local_declare();
412
413 if (default_instance) {
414 local_irq_save(flags);
415 NCR5380_print_status(default_instance);
416 local_irq_restore(flags);
417 }
418}
419#endif
420
421
422/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
423static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
424{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 void *addr;
426
427 if(sun3_dma_orig_addr != NULL)
428 dvma_unmap(sun3_dma_orig_addr);
429
Finn Thain757f5ba2014-03-18 11:42:24 +1100430#ifdef SUN3_SCSI_VME
431 addr = (void *)dvma_map_vme((unsigned long) data, count);
432#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 addr = (void *)dvma_map((unsigned long) data, count);
Finn Thain757f5ba2014-03-18 11:42:24 +1100434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 sun3_dma_orig_addr = addr;
437 sun3_dma_orig_count = count;
Finn Thain757f5ba2014-03-18 11:42:24 +1100438
439#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dregs->fifo_count = 0;
441 sun3_udc_write(UDC_RESET, UDC_CSR);
442
443 /* reset fifo */
444 dregs->csr &= ~CSR_FIFO;
445 dregs->csr |= CSR_FIFO;
Finn Thain757f5ba2014-03-18 11:42:24 +1100446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* set direction */
449 if(write_flag)
450 dregs->csr |= CSR_SEND;
451 else
452 dregs->csr &= ~CSR_SEND;
453
Finn Thain757f5ba2014-03-18 11:42:24 +1100454#ifdef SUN3_SCSI_VME
455 dregs->csr |= CSR_PACK_ENABLE;
456
457 dregs->dma_addr_hi = ((unsigned long)addr >> 16);
458 dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
459
460 dregs->dma_count_hi = 0;
461 dregs->dma_count_lo = 0;
462 dregs->fifo_count_hi = 0;
463 dregs->fifo_count = 0;
464#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* byte count for fifo */
466 dregs->fifo_count = count;
467
468 sun3_udc_write(UDC_RESET, UDC_CSR);
469
470 /* reset fifo */
471 dregs->csr &= ~CSR_FIFO;
472 dregs->csr |= CSR_FIFO;
473
474 if(dregs->fifo_count != count) {
475 printk("scsi%d: fifo_mismatch %04x not %04x\n",
476 default_instance->host_no, dregs->fifo_count,
477 (unsigned int) count);
Finn Thaind614f062014-03-18 11:42:16 +1100478 NCR5380_dprint(NDEBUG_DMA, default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 /* setup udc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
483 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 udc_regs->count = count/2; /* count in words */
485 udc_regs->mode_hi = UDC_MODE_HIWORD;
486 if(write_flag) {
487 if(count & 1)
488 udc_regs->count++;
489 udc_regs->mode_lo = UDC_MODE_LSEND;
490 udc_regs->rsel = UDC_RSEL_SEND;
491 } else {
492 udc_regs->mode_lo = UDC_MODE_LRECV;
493 udc_regs->rsel = UDC_RSEL_RECV;
494 }
495
496 /* announce location of regs block */
497 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
498 UDC_CHN_HI);
499
500 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
501
502 /* set dma master on */
503 sun3_udc_write(0xd, UDC_MODE);
504
505 /* interrupt enable */
506 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
Finn Thain757f5ba2014-03-18 11:42:24 +1100507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 return count;
510
511}
512
Finn Thain757f5ba2014-03-18 11:42:24 +1100513#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
515{
516 unsigned short resid;
517
518 dregs->udc_addr = 0x32;
519 udelay(SUN3_DMA_DELAY);
520 resid = dregs->udc_data;
521 udelay(SUN3_DMA_DELAY);
522 resid *= 2;
523
524 return (unsigned long) resid;
525}
Finn Thain757f5ba2014-03-18 11:42:24 +1100526#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
529{
530 return last_residual;
531}
532
Henne811c9362006-10-03 19:51:59 +0200533static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
534 struct scsi_cmnd *cmd,
535 int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200537 if (cmd->request->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return wanted;
539 else
540 return 0;
541}
542
543static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
544{
Finn Thain757f5ba2014-03-18 11:42:24 +1100545#ifdef SUN3_SCSI_VME
546 unsigned short csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Finn Thain757f5ba2014-03-18 11:42:24 +1100548 csr = dregs->csr;
549
550 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
551 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
552
553 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
554 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
555
556/* if(!(csr & CSR_DMA_ENABLE))
557 * dregs->csr |= CSR_DMA_ENABLE;
558 */
559#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 sun3_udc_write(UDC_CHN_START, UDC_CSR);
Finn Thain757f5ba2014-03-18 11:42:24 +1100561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return 0;
564}
565
566/* clean up after our dma is done */
567static int sun3scsi_dma_finish(int write_flag)
568{
Finn Thain757f5ba2014-03-18 11:42:24 +1100569 unsigned short __maybe_unused count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned short fifo;
571 int ret = 0;
572
573 sun3_dma_active = 0;
Finn Thain757f5ba2014-03-18 11:42:24 +1100574
575#ifdef SUN3_SCSI_VME
576 dregs->csr &= ~CSR_DMA_ENABLE;
577
578 fifo = dregs->fifo_count;
579 if (write_flag) {
580 if ((fifo > 0) && (fifo < sun3_dma_orig_count))
581 fifo++;
582 }
583
584 last_residual = fifo;
585 /* empty bytes from the fifo which didn't make it */
586 if ((!write_flag) && (dregs->csr & CSR_LEFT)) {
587 unsigned char *vaddr;
588
589 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
590
591 vaddr += (sun3_dma_orig_count - fifo);
592 vaddr--;
593
594 switch (dregs->csr & CSR_LEFT) {
595 case CSR_LEFT_3:
596 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
597 vaddr--;
598
599 case CSR_LEFT_2:
600 *vaddr = (dregs->bpack_hi & 0x00ff);
601 vaddr--;
602
603 case CSR_LEFT_1:
604 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
605 break;
606 }
607 }
608#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 // check to empty the fifo on a read
610 if(!write_flag) {
611 int tmo = 20000; /* .2 sec */
612
613 while(1) {
614 if(dregs->csr & CSR_FIFO_EMPTY)
615 break;
616
617 if(--tmo <= 0) {
618 printk("sun3scsi: fifo failed to empty!\n");
619 return 1;
620 }
621 udelay(10);
622 }
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 count = sun3scsi_dma_count(default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 fifo = dregs->fifo_count;
628 last_residual = fifo;
629
630 /* empty bytes from the fifo which didn't make it */
631 if((!write_flag) && (count - fifo) == 2) {
632 unsigned short data;
633 unsigned char *vaddr;
634
635 data = dregs->fifo_data;
636 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
637
638 vaddr += (sun3_dma_orig_count - fifo);
639
640 vaddr[-2] = (data & 0xff00) >> 8;
641 vaddr[-1] = (data & 0xff);
642 }
Finn Thain757f5ba2014-03-18 11:42:24 +1100643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 dvma_unmap(sun3_dma_orig_addr);
646 sun3_dma_orig_addr = NULL;
Finn Thain757f5ba2014-03-18 11:42:24 +1100647
648#ifdef SUN3_SCSI_VME
649 dregs->dma_addr_hi = 0;
650 dregs->dma_addr_lo = 0;
651 dregs->dma_count_hi = 0;
652 dregs->dma_count_lo = 0;
653
654 dregs->fifo_count = 0;
655 dregs->fifo_count_hi = 0;
656
657 dregs->csr &= ~CSR_SEND;
658/* dregs->csr |= CSR_DMA_ENABLE; */
659#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 sun3_udc_write(UDC_RESET, UDC_CSR);
661 dregs->fifo_count = 0;
662 dregs->csr &= ~CSR_SEND;
663
664 /* reset fifo */
665 dregs->csr &= ~CSR_FIFO;
666 dregs->csr |= CSR_FIFO;
Finn Thain757f5ba2014-03-18 11:42:24 +1100667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 sun3_dma_setup_done = NULL;
670
671 return ret;
672
673}
674
675#include "sun3_NCR5380.c"
676
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100677static struct scsi_host_template driver_template = {
Geert Uytterhoeven9dcc26c2013-04-10 13:52:09 +0200678 .show_info = sun3scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .name = SUN3_SCSI_NAME,
680 .detect = sun3scsi_detect,
681 .release = sun3scsi_release,
682 .info = sun3scsi_info,
683 .queuecommand = sun3scsi_queue_command,
684 .eh_abort_handler = sun3scsi_abort,
685 .eh_bus_reset_handler = sun3scsi_bus_reset,
Finn Thaind572f65f2014-11-12 16:12:00 +1100686 .can_queue = 16,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 .this_id = 7,
Finn Thaind572f65f2014-11-12 16:12:00 +1100688 .sg_tablesize = SG_NONE,
689 .cmd_per_lun = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .use_clustering = DISABLE_CLUSTERING
691};
692
693
694#include "scsi_module.c"
695
696MODULE_LICENSE("GPL");