blob: 30353f6b599c4d2b512d4b54134bcc4409898d85 [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.
25 *
26 * For more information, please consult
27 *
28 * NCR 5380 Family
29 * SCSI Protocol Controller
30 * Databook
31 *
32 * NCR Microelectronics
33 * 1635 Aeroplaza Drive
34 * Colorado Springs, CO 80916
35 * 1+ (719) 578-3400
36 * 1+ (800) 334-5454
37 */
38
39
40/*
41 * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
42 *
43 * Options :
44 *
45 * PARITY - enable parity checking. Not supported.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define AUTOSENSE
49
50#include <linux/types.h>
51#include <linux/stddef.h>
52#include <linux/ctype.h>
53#include <linux/delay.h>
54
55#include <linux/module.h>
56#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/ioport.h>
58#include <linux/init.h>
59#include <linux/blkdev.h>
60
61#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/sun3ints.h>
64#include <asm/dvma.h>
65#include <asm/idprom.h>
66#include <asm/machines.h>
67
68/* dma on! */
69#define REAL_DMA
70
71#include "scsi.h"
72#include <scsi/scsi_host.h>
73#include "sun3_scsi.h"
Finn Thain9f6620a2014-03-18 11:42:23 +110074#include "NCR5380.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Finn Thain757f5ba2014-03-18 11:42:24 +110076extern int sun3_map_test(unsigned long, char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*#define RESET_BOOT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* #define SUPPORT_TAGS */
80
Finn Thain757f5ba2014-03-18 11:42:24 +110081#ifdef SUN3_SCSI_VME
82#define ENABLE_IRQ()
83#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
Finn Thain757f5ba2014-03-18 11:42:24 +110085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
David Howells7d12e782006-10-05 14:55:46 +010088static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static int setup_can_queue = -1;
91module_param(setup_can_queue, int, 0);
92static int setup_cmd_per_lun = -1;
93module_param(setup_cmd_per_lun, int, 0);
94static int setup_sg_tablesize = -1;
95module_param(setup_sg_tablesize, int, 0);
96#ifdef SUPPORT_TAGS
97static int setup_use_tagged_queuing = -1;
98module_param(setup_use_tagged_queuing, int, 0);
99#endif
100static int setup_hostid = -1;
101module_param(setup_hostid, int, 0);
102
Henne811c9362006-10-03 19:51:59 +0200103static struct scsi_cmnd *sun3_dma_setup_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Michael Schmitz2b0f8342014-05-02 20:43:01 +1200105#define RESET_RUN_DONE
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define AFTER_RESET_DELAY (HZ/2)
108
109/* ms to wait after hitting dma regs */
110#define SUN3_DMA_DELAY 10
111
112/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
113#define SUN3_DVMA_BUFSIZE 0xe000
114
115/* minimum number of bytes to do dma on */
116#define SUN3_DMA_MINSIZE 128
117
118static volatile unsigned char *sun3_scsi_regp;
119static volatile struct sun3_dma_regs *dregs;
Finn Thain757f5ba2014-03-18 11:42:24 +1100120#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct sun3_udc_regs *udc_regs = NULL;
Finn Thain757f5ba2014-03-18 11:42:24 +1100122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static unsigned char *sun3_dma_orig_addr = NULL;
124static unsigned long sun3_dma_orig_count = 0;
125static int sun3_dma_active = 0;
126static unsigned long last_residual = 0;
127
128/*
129 * NCR 5380 register access functions
130 */
131
132static inline unsigned char sun3scsi_read(int reg)
133{
134 return( sun3_scsi_regp[reg] );
135}
136
137static inline void sun3scsi_write(int reg, int value)
138{
139 sun3_scsi_regp[reg] = value;
140}
141
Finn Thain757f5ba2014-03-18 11:42:24 +1100142#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/* dma controller register access functions */
144
145static inline unsigned short sun3_udc_read(unsigned char reg)
146{
147 unsigned short ret;
148
149 dregs->udc_addr = UDC_CSR;
150 udelay(SUN3_DMA_DELAY);
151 ret = dregs->udc_data;
152 udelay(SUN3_DMA_DELAY);
153
154 return ret;
155}
156
157static inline void sun3_udc_write(unsigned short val, unsigned char reg)
158{
159 dregs->udc_addr = reg;
160 udelay(SUN3_DMA_DELAY);
161 dregs->udc_data = val;
162 udelay(SUN3_DMA_DELAY);
163}
Finn Thain757f5ba2014-03-18 11:42:24 +1100164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * XXX: status debug
168 */
169static struct Scsi_Host *default_instance;
170
171/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100172 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Purpose : initializes mac NCR5380 driver based on the
175 * command line / compile time port and irq definitions.
176 *
177 * Inputs : tpnt - template for this SCSI adapter.
178 *
179 * Returns : 1 if a host adapter was found, 0 if not.
180 *
181 */
182
Finn Thain757f5ba2014-03-18 11:42:24 +1100183static int __init sun3scsi_detect(struct scsi_host_template *tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Finn Thain757f5ba2014-03-18 11:42:24 +1100185 unsigned long ioaddr, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 static int called = 0;
187 struct Scsi_Host *instance;
Finn Thain757f5ba2014-03-18 11:42:24 +1100188#ifdef SUN3_SCSI_VME
189 int i;
190 unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
191 IOBASE_SUN3_VMESCSI + 0x4000,
192 0 };
193 unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
194 SUN3_VEC_VMESCSI1,
195 0 };
196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* check that this machine has an onboard 5380 */
199 switch(idprom->id_machtype) {
Finn Thain757f5ba2014-03-18 11:42:24 +1100200#ifdef SUN3_SCSI_VME
201 case SM_SUN3|SM_3_160:
202 case SM_SUN3|SM_3_260:
203 break;
204#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 case SM_SUN3|SM_3_50:
206 case SM_SUN3|SM_3_60:
207 break;
Finn Thain757f5ba2014-03-18 11:42:24 +1100208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 default:
211 return 0;
212 }
213
214 if(called)
215 return 0;
216
Finn Thain757f5ba2014-03-18 11:42:24 +1100217#ifdef SUN3_SCSI_VME
218 tpnt->proc_name = "Sun3 5380 VME SCSI";
219#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 tpnt->proc_name = "Sun3 5380 SCSI";
Finn Thain757f5ba2014-03-18 11:42:24 +1100221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* setup variables */
224 tpnt->can_queue =
225 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
226 tpnt->cmd_per_lun =
227 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
228 tpnt->sg_tablesize =
229 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
230
231 if (setup_hostid >= 0)
232 tpnt->this_id = setup_hostid;
233 else {
234 /* use 7 as default */
235 tpnt->this_id = 7;
236 }
237
Finn Thain757f5ba2014-03-18 11:42:24 +1100238#ifdef SUN3_SCSI_VME
239 ioaddr = 0;
240 for (i = 0; addrs[i] != 0; i++) {
241 unsigned char x;
242
243 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
244 SUN3_PAGE_TYPE_VME16);
245 irq = vecs[i];
246 sun3_scsi_regp = (unsigned char *)ioaddr;
247
248 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
249
250 if (sun3_map_test((unsigned long)dregs, &x)) {
251 unsigned short oldcsr;
252
253 oldcsr = dregs->csr;
254 dregs->csr = 0;
255 udelay(SUN3_DMA_DELAY);
256 if (dregs->csr == 0x1400)
257 break;
258
259 dregs->csr = oldcsr;
260 }
261
262 iounmap((void *)ioaddr);
263 ioaddr = 0;
264 }
265
266 if (!ioaddr)
267 return 0;
268#else
269 irq = IRQ_SUN3_SCSI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
271 sun3_scsi_regp = (unsigned char *)ioaddr;
272
273 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
274
275 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
276 == NULL) {
277 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
278 return 0;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif
281#ifdef SUPPORT_TAGS
282 if (setup_use_tagged_queuing < 0)
283 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
284#endif
285
286 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
287 if(instance == NULL)
288 return 0;
289
290 default_instance = instance;
291
292 instance->io_port = (unsigned long) ioaddr;
Finn Thain757f5ba2014-03-18 11:42:24 +1100293 instance->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 NCR5380_init(instance, 0);
296
297 instance->n_io_port = 32;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (request_irq(instance->irq, scsi_sun3_intr,
Jeff Garzik1e641662007-11-11 19:52:05 -0500300 0, "Sun3SCSI-5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifndef REAL_DMA
302 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
303 instance->host_no, instance->irq);
304 instance->irq = SCSI_IRQ_NONE;
305#else
306 printk("scsi%d: IRQ%d not free, bailing out\n",
307 instance->host_no, instance->irq);
308 return 0;
309#endif
310 }
311
Finn Thain757f5ba2014-03-18 11:42:24 +1100312 pr_info("scsi%d: %s at port %lX irq", instance->host_no,
313 tpnt->proc_name, instance->io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (instance->irq == SCSI_IRQ_NONE)
315 printk ("s disabled");
316 else
317 printk (" %d", instance->irq);
318 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
319 instance->can_queue, instance->cmd_per_lun,
320 SUN3SCSI_PUBLIC_RELEASE);
321 printk("\nscsi%d:", instance->host_no);
322 NCR5380_print_options(instance);
323 printk("\n");
324
325 dregs->csr = 0;
326 udelay(SUN3_DMA_DELAY);
327 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
328 udelay(SUN3_DMA_DELAY);
329 dregs->fifo_count = 0;
Finn Thain757f5ba2014-03-18 11:42:24 +1100330#ifdef SUN3_SCSI_VME
331 dregs->fifo_count_hi = 0;
332 dregs->dma_addr_hi = 0;
333 dregs->dma_addr_lo = 0;
334 dregs->dma_count_hi = 0;
335 dregs->dma_count_lo = 0;
336
337 dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 called = 1;
341
342#ifdef RESET_BOOT
343 sun3_scsi_reset_boot(instance);
344#endif
345
346 return 1;
347}
348
Finn Thained8b9e72014-11-12 16:11:51 +1100349static int sun3scsi_release(struct Scsi_Host *shpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 if (shpnt->irq != SCSI_IRQ_NONE)
Jeff Garzik1e641662007-11-11 19:52:05 -0500352 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 iounmap((void *)sun3_scsi_regp);
355
Geert Uytterhoeven19b6c512011-06-13 20:39:19 +0200356 NCR5380_exit(shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360#ifdef RESET_BOOT
361/*
362 * Our 'bus reset on boot' function
363 */
364
365static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
366{
367 unsigned long end;
368
369 NCR5380_local_declare();
370 NCR5380_setup(instance);
371
372 /*
373 * Do a SCSI reset to clean up the bus during initialization. No
374 * messing with the queues, interrupts, or locks necessary here.
375 */
376
377 printk( "Sun3 SCSI: resetting the SCSI bus..." );
378
379 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
380// sun3_disable_irq( IRQ_SUN3_SCSI );
381
382 /* get in phase */
383 NCR5380_write( TARGET_COMMAND_REG,
384 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
385
386 /* assert RST */
387 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
388
389 /* The min. reset hold time is 25us, so 40us should be enough */
390 udelay( 50 );
391
392 /* reset RST and interrupt */
393 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
394 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
395
396 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
397 barrier();
398
399 /* switch on SCSI IRQ again */
400// sun3_enable_irq( IRQ_SUN3_SCSI );
401
402 printk( " done\n" );
403}
404#endif
405
Finn Thain757f5ba2014-03-18 11:42:24 +1100406static const char *sun3scsi_info(struct Scsi_Host *spnt)
407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return "";
409}
410
411// safe bits for the CSR
412#define CSR_GOOD 0x060f
413
David Howells7d12e782006-10-05 14:55:46 +0100414static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 unsigned short csr = dregs->csr;
417 int handled = 0;
418
Finn Thain757f5ba2014-03-18 11:42:24 +1100419#ifdef SUN3_SCSI_VME
420 dregs->csr &= ~CSR_DMA_ENABLE;
421#endif
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if(csr & ~CSR_GOOD) {
424 if(csr & CSR_DMA_BUSERR) {
425 printk("scsi%d: bus error in dma\n", default_instance->host_no);
426 }
427
428 if(csr & CSR_DMA_CONFLICT) {
429 printk("scsi%d: dma conflict\n", default_instance->host_no);
430 }
431 handled = 1;
432 }
433
434 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
David Howells7d12e782006-10-05 14:55:46 +0100435 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 handled = 1;
437 }
438
439 return IRQ_RETVAL(handled);
440}
441
442/*
443 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
444 * reentering NCR5380_print_status seems to have ugly side effects
445 */
446
447/* this doesn't seem to get used at all -- sam */
448#if 0
449void sun3_sun3_debug (void)
450{
451 unsigned long flags;
452 NCR5380_local_declare();
453
454 if (default_instance) {
455 local_irq_save(flags);
456 NCR5380_print_status(default_instance);
457 local_irq_restore(flags);
458 }
459}
460#endif
461
462
463/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
464static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 void *addr;
467
468 if(sun3_dma_orig_addr != NULL)
469 dvma_unmap(sun3_dma_orig_addr);
470
Finn Thain757f5ba2014-03-18 11:42:24 +1100471#ifdef SUN3_SCSI_VME
472 addr = (void *)dvma_map_vme((unsigned long) data, count);
473#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 addr = (void *)dvma_map((unsigned long) data, count);
Finn Thain757f5ba2014-03-18 11:42:24 +1100475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 sun3_dma_orig_addr = addr;
478 sun3_dma_orig_count = count;
Finn Thain757f5ba2014-03-18 11:42:24 +1100479
480#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dregs->fifo_count = 0;
482 sun3_udc_write(UDC_RESET, UDC_CSR);
483
484 /* reset fifo */
485 dregs->csr &= ~CSR_FIFO;
486 dregs->csr |= CSR_FIFO;
Finn Thain757f5ba2014-03-18 11:42:24 +1100487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* set direction */
490 if(write_flag)
491 dregs->csr |= CSR_SEND;
492 else
493 dregs->csr &= ~CSR_SEND;
494
Finn Thain757f5ba2014-03-18 11:42:24 +1100495#ifdef SUN3_SCSI_VME
496 dregs->csr |= CSR_PACK_ENABLE;
497
498 dregs->dma_addr_hi = ((unsigned long)addr >> 16);
499 dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
500
501 dregs->dma_count_hi = 0;
502 dregs->dma_count_lo = 0;
503 dregs->fifo_count_hi = 0;
504 dregs->fifo_count = 0;
505#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* byte count for fifo */
507 dregs->fifo_count = count;
508
509 sun3_udc_write(UDC_RESET, UDC_CSR);
510
511 /* reset fifo */
512 dregs->csr &= ~CSR_FIFO;
513 dregs->csr |= CSR_FIFO;
514
515 if(dregs->fifo_count != count) {
516 printk("scsi%d: fifo_mismatch %04x not %04x\n",
517 default_instance->host_no, dregs->fifo_count,
518 (unsigned int) count);
Finn Thaind614f062014-03-18 11:42:16 +1100519 NCR5380_dprint(NDEBUG_DMA, default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 /* setup udc */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
524 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 udc_regs->count = count/2; /* count in words */
526 udc_regs->mode_hi = UDC_MODE_HIWORD;
527 if(write_flag) {
528 if(count & 1)
529 udc_regs->count++;
530 udc_regs->mode_lo = UDC_MODE_LSEND;
531 udc_regs->rsel = UDC_RSEL_SEND;
532 } else {
533 udc_regs->mode_lo = UDC_MODE_LRECV;
534 udc_regs->rsel = UDC_RSEL_RECV;
535 }
536
537 /* announce location of regs block */
538 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
539 UDC_CHN_HI);
540
541 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
542
543 /* set dma master on */
544 sun3_udc_write(0xd, UDC_MODE);
545
546 /* interrupt enable */
547 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
Finn Thain757f5ba2014-03-18 11:42:24 +1100548#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return count;
551
552}
553
Finn Thain757f5ba2014-03-18 11:42:24 +1100554#ifndef SUN3_SCSI_VME
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
556{
557 unsigned short resid;
558
559 dregs->udc_addr = 0x32;
560 udelay(SUN3_DMA_DELAY);
561 resid = dregs->udc_data;
562 udelay(SUN3_DMA_DELAY);
563 resid *= 2;
564
565 return (unsigned long) resid;
566}
Finn Thain757f5ba2014-03-18 11:42:24 +1100567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
570{
571 return last_residual;
572}
573
Henne811c9362006-10-03 19:51:59 +0200574static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
575 struct scsi_cmnd *cmd,
576 int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200578 if (cmd->request->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return wanted;
580 else
581 return 0;
582}
583
584static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
585{
Finn Thain757f5ba2014-03-18 11:42:24 +1100586#ifdef SUN3_SCSI_VME
587 unsigned short csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Finn Thain757f5ba2014-03-18 11:42:24 +1100589 csr = dregs->csr;
590
591 dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
592 dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
593
594 dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
595 dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
596
597/* if(!(csr & CSR_DMA_ENABLE))
598 * dregs->csr |= CSR_DMA_ENABLE;
599 */
600#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 sun3_udc_write(UDC_CHN_START, UDC_CSR);
Finn Thain757f5ba2014-03-18 11:42:24 +1100602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 return 0;
605}
606
607/* clean up after our dma is done */
608static int sun3scsi_dma_finish(int write_flag)
609{
Finn Thain757f5ba2014-03-18 11:42:24 +1100610 unsigned short __maybe_unused count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 unsigned short fifo;
612 int ret = 0;
613
614 sun3_dma_active = 0;
Finn Thain757f5ba2014-03-18 11:42:24 +1100615
616#ifdef SUN3_SCSI_VME
617 dregs->csr &= ~CSR_DMA_ENABLE;
618
619 fifo = dregs->fifo_count;
620 if (write_flag) {
621 if ((fifo > 0) && (fifo < sun3_dma_orig_count))
622 fifo++;
623 }
624
625 last_residual = fifo;
626 /* empty bytes from the fifo which didn't make it */
627 if ((!write_flag) && (dregs->csr & CSR_LEFT)) {
628 unsigned char *vaddr;
629
630 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
631
632 vaddr += (sun3_dma_orig_count - fifo);
633 vaddr--;
634
635 switch (dregs->csr & CSR_LEFT) {
636 case CSR_LEFT_3:
637 *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
638 vaddr--;
639
640 case CSR_LEFT_2:
641 *vaddr = (dregs->bpack_hi & 0x00ff);
642 vaddr--;
643
644 case CSR_LEFT_1:
645 *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
646 break;
647 }
648 }
649#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 // check to empty the fifo on a read
651 if(!write_flag) {
652 int tmo = 20000; /* .2 sec */
653
654 while(1) {
655 if(dregs->csr & CSR_FIFO_EMPTY)
656 break;
657
658 if(--tmo <= 0) {
659 printk("sun3scsi: fifo failed to empty!\n");
660 return 1;
661 }
662 udelay(10);
663 }
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 count = sun3scsi_dma_count(default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 fifo = dregs->fifo_count;
669 last_residual = fifo;
670
671 /* empty bytes from the fifo which didn't make it */
672 if((!write_flag) && (count - fifo) == 2) {
673 unsigned short data;
674 unsigned char *vaddr;
675
676 data = dregs->fifo_data;
677 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
678
679 vaddr += (sun3_dma_orig_count - fifo);
680
681 vaddr[-2] = (data & 0xff00) >> 8;
682 vaddr[-1] = (data & 0xff);
683 }
Finn Thain757f5ba2014-03-18 11:42:24 +1100684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 dvma_unmap(sun3_dma_orig_addr);
687 sun3_dma_orig_addr = NULL;
Finn Thain757f5ba2014-03-18 11:42:24 +1100688
689#ifdef SUN3_SCSI_VME
690 dregs->dma_addr_hi = 0;
691 dregs->dma_addr_lo = 0;
692 dregs->dma_count_hi = 0;
693 dregs->dma_count_lo = 0;
694
695 dregs->fifo_count = 0;
696 dregs->fifo_count_hi = 0;
697
698 dregs->csr &= ~CSR_SEND;
699/* dregs->csr |= CSR_DMA_ENABLE; */
700#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sun3_udc_write(UDC_RESET, UDC_CSR);
702 dregs->fifo_count = 0;
703 dregs->csr &= ~CSR_SEND;
704
705 /* reset fifo */
706 dregs->csr &= ~CSR_FIFO;
707 dregs->csr |= CSR_FIFO;
Finn Thain757f5ba2014-03-18 11:42:24 +1100708#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 sun3_dma_setup_done = NULL;
711
712 return ret;
713
714}
715
716#include "sun3_NCR5380.c"
717
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100718static struct scsi_host_template driver_template = {
Geert Uytterhoeven9dcc26c2013-04-10 13:52:09 +0200719 .show_info = sun3scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .name = SUN3_SCSI_NAME,
721 .detect = sun3scsi_detect,
722 .release = sun3scsi_release,
723 .info = sun3scsi_info,
724 .queuecommand = sun3scsi_queue_command,
725 .eh_abort_handler = sun3scsi_abort,
726 .eh_bus_reset_handler = sun3scsi_bus_reset,
727 .can_queue = CAN_QUEUE,
728 .this_id = 7,
729 .sg_tablesize = SG_TABLESIZE,
730 .cmd_per_lun = CMD_PER_LUN,
731 .use_clustering = DISABLE_CLUSTERING
732};
733
734
735#include "scsi_module.c"
736
737MODULE_LICENSE("GPL");