blob: d56d85dd9ba0ed60c943e8bd99956a22c06418ca [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 *
6 * Adapted from mac_scsinew.c:
7 */
8/*
9 * Generic Macintosh NCR5380 driver
10 *
11 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
12 *
13 * derived in part from:
14 */
15/*
16 * Generic Generic NCR5380 driver
17 *
18 * Copyright 1995, Russell King
19 *
20 * ALPHA RELEASE 1.
21 *
22 * For more information, please consult
23 *
24 * NCR 5380 Family
25 * SCSI Protocol Controller
26 * Databook
27 *
28 * NCR Microelectronics
29 * 1635 Aeroplaza Drive
30 * Colorado Springs, CO 80916
31 * 1+ (719) 578-3400
32 * 1+ (800) 334-5454
33 */
34
35
36/*
37 * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
38 *
39 * Options :
40 *
41 * PARITY - enable parity checking. Not supported.
42 *
43 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
44 *
45 * USLEEP - enable support for devices that don't disconnect. Untested.
46 */
47
48/*
49 * $Log: sun3_NCR5380.c,v $
50 */
51
52#define AUTOSENSE
53
54#include <linux/types.h>
55#include <linux/stddef.h>
56#include <linux/ctype.h>
57#include <linux/delay.h>
58
59#include <linux/module.h>
60#include <linux/signal.h>
61#include <linux/sched.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/blkdev.h>
65
66#include <asm/io.h>
67#include <asm/system.h>
68
69#include <asm/sun3ints.h>
70#include <asm/dvma.h>
71#include <asm/idprom.h>
72#include <asm/machines.h>
73
74/* dma on! */
75#define REAL_DMA
76
77#include "scsi.h"
78#include <scsi/scsi_host.h>
79#include "sun3_scsi.h"
80#include "NCR5380.h"
81
82static void NCR5380_print(struct Scsi_Host *instance);
83
84/* #define OLDDMA */
85
86#define USE_WRAPPER
87/*#define RESET_BOOT */
88#define DRIVER_SETUP
89
90#define NDEBUG 0
91
92/*
93 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
94 */
95#ifdef BUG
96#undef RESET_BOOT
97#undef DRIVER_SETUP
98#endif
99
100/* #define SUPPORT_TAGS */
101
102#define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
103
104
David Howells7d12e782006-10-05 14:55:46 +0100105static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static inline unsigned char sun3scsi_read(int reg);
107static inline void sun3scsi_write(int reg, int value);
108
109static int setup_can_queue = -1;
110module_param(setup_can_queue, int, 0);
111static int setup_cmd_per_lun = -1;
112module_param(setup_cmd_per_lun, int, 0);
113static int setup_sg_tablesize = -1;
114module_param(setup_sg_tablesize, int, 0);
115#ifdef SUPPORT_TAGS
116static int setup_use_tagged_queuing = -1;
117module_param(setup_use_tagged_queuing, int, 0);
118#endif
119static int setup_hostid = -1;
120module_param(setup_hostid, int, 0);
121
Henne811c9362006-10-03 19:51:59 +0200122static struct scsi_cmnd *sun3_dma_setup_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#define AFTER_RESET_DELAY (HZ/2)
125
126/* ms to wait after hitting dma regs */
127#define SUN3_DMA_DELAY 10
128
129/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
130#define SUN3_DVMA_BUFSIZE 0xe000
131
132/* minimum number of bytes to do dma on */
133#define SUN3_DMA_MINSIZE 128
134
135static volatile unsigned char *sun3_scsi_regp;
136static volatile struct sun3_dma_regs *dregs;
137#ifdef OLDDMA
138static unsigned char *dmabuf = NULL; /* dma memory buffer */
139#endif
140static struct sun3_udc_regs *udc_regs = NULL;
141static unsigned char *sun3_dma_orig_addr = NULL;
142static unsigned long sun3_dma_orig_count = 0;
143static int sun3_dma_active = 0;
144static unsigned long last_residual = 0;
145
146/*
147 * NCR 5380 register access functions
148 */
149
150static inline unsigned char sun3scsi_read(int reg)
151{
152 return( sun3_scsi_regp[reg] );
153}
154
155static inline void sun3scsi_write(int reg, int value)
156{
157 sun3_scsi_regp[reg] = value;
158}
159
160/* dma controller register access functions */
161
162static inline unsigned short sun3_udc_read(unsigned char reg)
163{
164 unsigned short ret;
165
166 dregs->udc_addr = UDC_CSR;
167 udelay(SUN3_DMA_DELAY);
168 ret = dregs->udc_data;
169 udelay(SUN3_DMA_DELAY);
170
171 return ret;
172}
173
174static inline void sun3_udc_write(unsigned short val, unsigned char reg)
175{
176 dregs->udc_addr = reg;
177 udelay(SUN3_DMA_DELAY);
178 dregs->udc_data = val;
179 udelay(SUN3_DMA_DELAY);
180}
181
182/*
183 * XXX: status debug
184 */
185static struct Scsi_Host *default_instance;
186
187/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100188 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * Purpose : initializes mac NCR5380 driver based on the
191 * command line / compile time port and irq definitions.
192 *
193 * Inputs : tpnt - template for this SCSI adapter.
194 *
195 * Returns : 1 if a host adapter was found, 0 if not.
196 *
197 */
198
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100199int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 unsigned long ioaddr;
202 static int called = 0;
203 struct Scsi_Host *instance;
204
205 /* check that this machine has an onboard 5380 */
206 switch(idprom->id_machtype) {
207 case SM_SUN3|SM_3_50:
208 case SM_SUN3|SM_3_60:
209 break;
210
211 default:
212 return 0;
213 }
214
215 if(called)
216 return 0;
217
218 tpnt->proc_name = "Sun3 5380 SCSI";
219
220 /* setup variables */
221 tpnt->can_queue =
222 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
223 tpnt->cmd_per_lun =
224 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
225 tpnt->sg_tablesize =
226 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
227
228 if (setup_hostid >= 0)
229 tpnt->this_id = setup_hostid;
230 else {
231 /* use 7 as default */
232 tpnt->this_id = 7;
233 }
234
235 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
236 sun3_scsi_regp = (unsigned char *)ioaddr;
237
238 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
239
240 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
241 == NULL) {
242 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
243 return 0;
244 }
245#ifdef OLDDMA
246 if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
247 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
248 return 0;
249 }
250#endif
251#ifdef SUPPORT_TAGS
252 if (setup_use_tagged_queuing < 0)
253 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
254#endif
255
256 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
257 if(instance == NULL)
258 return 0;
259
260 default_instance = instance;
261
262 instance->io_port = (unsigned long) ioaddr;
263 instance->irq = IRQ_SUN3_SCSI;
264
265 NCR5380_init(instance, 0);
266
267 instance->n_io_port = 32;
268
269 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
270
271 if (request_irq(instance->irq, scsi_sun3_intr,
272 0, "Sun3SCSI-5380", NULL)) {
273#ifndef REAL_DMA
274 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
275 instance->host_no, instance->irq);
276 instance->irq = SCSI_IRQ_NONE;
277#else
278 printk("scsi%d: IRQ%d not free, bailing out\n",
279 instance->host_no, instance->irq);
280 return 0;
281#endif
282 }
283
284 printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
285 if (instance->irq == SCSI_IRQ_NONE)
286 printk ("s disabled");
287 else
288 printk (" %d", instance->irq);
289 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
290 instance->can_queue, instance->cmd_per_lun,
291 SUN3SCSI_PUBLIC_RELEASE);
292 printk("\nscsi%d:", instance->host_no);
293 NCR5380_print_options(instance);
294 printk("\n");
295
296 dregs->csr = 0;
297 udelay(SUN3_DMA_DELAY);
298 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
299 udelay(SUN3_DMA_DELAY);
300 dregs->fifo_count = 0;
301
302 called = 1;
303
304#ifdef RESET_BOOT
305 sun3_scsi_reset_boot(instance);
306#endif
307
308 return 1;
309}
310
311int sun3scsi_release (struct Scsi_Host *shpnt)
312{
313 if (shpnt->irq != SCSI_IRQ_NONE)
314 free_irq (shpnt->irq, NULL);
315
316 iounmap((void *)sun3_scsi_regp);
317
318 return 0;
319}
320
321#ifdef RESET_BOOT
322/*
323 * Our 'bus reset on boot' function
324 */
325
326static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
327{
328 unsigned long end;
329
330 NCR5380_local_declare();
331 NCR5380_setup(instance);
332
333 /*
334 * Do a SCSI reset to clean up the bus during initialization. No
335 * messing with the queues, interrupts, or locks necessary here.
336 */
337
338 printk( "Sun3 SCSI: resetting the SCSI bus..." );
339
340 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
341// sun3_disable_irq( IRQ_SUN3_SCSI );
342
343 /* get in phase */
344 NCR5380_write( TARGET_COMMAND_REG,
345 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
346
347 /* assert RST */
348 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
349
350 /* The min. reset hold time is 25us, so 40us should be enough */
351 udelay( 50 );
352
353 /* reset RST and interrupt */
354 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
355 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
356
357 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
358 barrier();
359
360 /* switch on SCSI IRQ again */
361// sun3_enable_irq( IRQ_SUN3_SCSI );
362
363 printk( " done\n" );
364}
365#endif
366
367const char * sun3scsi_info (struct Scsi_Host *spnt) {
368 return "";
369}
370
371// safe bits for the CSR
372#define CSR_GOOD 0x060f
373
David Howells7d12e782006-10-05 14:55:46 +0100374static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 unsigned short csr = dregs->csr;
377 int handled = 0;
378
379 if(csr & ~CSR_GOOD) {
380 if(csr & CSR_DMA_BUSERR) {
381 printk("scsi%d: bus error in dma\n", default_instance->host_no);
382 }
383
384 if(csr & CSR_DMA_CONFLICT) {
385 printk("scsi%d: dma conflict\n", default_instance->host_no);
386 }
387 handled = 1;
388 }
389
390 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
David Howells7d12e782006-10-05 14:55:46 +0100391 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 handled = 1;
393 }
394
395 return IRQ_RETVAL(handled);
396}
397
398/*
399 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
400 * reentering NCR5380_print_status seems to have ugly side effects
401 */
402
403/* this doesn't seem to get used at all -- sam */
404#if 0
405void sun3_sun3_debug (void)
406{
407 unsigned long flags;
408 NCR5380_local_declare();
409
410 if (default_instance) {
411 local_irq_save(flags);
412 NCR5380_print_status(default_instance);
413 local_irq_restore(flags);
414 }
415}
416#endif
417
418
419/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
420static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
421{
422#ifdef OLDDMA
423 if(write_flag)
424 memcpy(dmabuf, data, count);
425 else {
426 sun3_dma_orig_addr = data;
427 sun3_dma_orig_count = count;
428 }
429#else
430 void *addr;
431
432 if(sun3_dma_orig_addr != NULL)
433 dvma_unmap(sun3_dma_orig_addr);
434
435// addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
436 addr = (void *)dvma_map((unsigned long) data, count);
437
438 sun3_dma_orig_addr = addr;
439 sun3_dma_orig_count = count;
440#endif
441 dregs->fifo_count = 0;
442 sun3_udc_write(UDC_RESET, UDC_CSR);
443
444 /* reset fifo */
445 dregs->csr &= ~CSR_FIFO;
446 dregs->csr |= CSR_FIFO;
447
448 /* set direction */
449 if(write_flag)
450 dregs->csr |= CSR_SEND;
451 else
452 dregs->csr &= ~CSR_SEND;
453
454 /* byte count for fifo */
455 dregs->fifo_count = count;
456
457 sun3_udc_write(UDC_RESET, UDC_CSR);
458
459 /* reset fifo */
460 dregs->csr &= ~CSR_FIFO;
461 dregs->csr |= CSR_FIFO;
462
463 if(dregs->fifo_count != count) {
464 printk("scsi%d: fifo_mismatch %04x not %04x\n",
465 default_instance->host_no, dregs->fifo_count,
466 (unsigned int) count);
467 NCR5380_print(default_instance);
468 }
469
470 /* setup udc */
471#ifdef OLDDMA
472 udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
473 udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
474#else
475 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
476 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
477#endif
478 udc_regs->count = count/2; /* count in words */
479 udc_regs->mode_hi = UDC_MODE_HIWORD;
480 if(write_flag) {
481 if(count & 1)
482 udc_regs->count++;
483 udc_regs->mode_lo = UDC_MODE_LSEND;
484 udc_regs->rsel = UDC_RSEL_SEND;
485 } else {
486 udc_regs->mode_lo = UDC_MODE_LRECV;
487 udc_regs->rsel = UDC_RSEL_RECV;
488 }
489
490 /* announce location of regs block */
491 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
492 UDC_CHN_HI);
493
494 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
495
496 /* set dma master on */
497 sun3_udc_write(0xd, UDC_MODE);
498
499 /* interrupt enable */
500 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
501
502 return count;
503
504}
505
506static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
507{
508 unsigned short resid;
509
510 dregs->udc_addr = 0x32;
511 udelay(SUN3_DMA_DELAY);
512 resid = dregs->udc_data;
513 udelay(SUN3_DMA_DELAY);
514 resid *= 2;
515
516 return (unsigned long) resid;
517}
518
519static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
520{
521 return last_residual;
522}
523
Henne811c9362006-10-03 19:51:59 +0200524static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
525 struct scsi_cmnd *cmd,
526 int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200528 if(blk_fs_request(cmd->request))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return wanted;
530 else
531 return 0;
532}
533
534static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
535{
536
537 sun3_udc_write(UDC_CHN_START, UDC_CSR);
538
539 return 0;
540}
541
542/* clean up after our dma is done */
543static int sun3scsi_dma_finish(int write_flag)
544{
545 unsigned short count;
546 unsigned short fifo;
547 int ret = 0;
548
549 sun3_dma_active = 0;
550#if 1
551 // check to empty the fifo on a read
552 if(!write_flag) {
553 int tmo = 20000; /* .2 sec */
554
555 while(1) {
556 if(dregs->csr & CSR_FIFO_EMPTY)
557 break;
558
559 if(--tmo <= 0) {
560 printk("sun3scsi: fifo failed to empty!\n");
561 return 1;
562 }
563 udelay(10);
564 }
565 }
566
567#endif
568
569 count = sun3scsi_dma_count(default_instance);
570#ifdef OLDDMA
571
572 /* if we've finished a read, copy out the data we read */
573 if(sun3_dma_orig_addr) {
574 /* check for residual bytes after dma end */
575 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
576 (BASR_PHASE_MATCH | BASR_ACK))) {
577 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
578 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
579 ret = count;
580 }
581
582 /* copy in what we dma'd no matter what */
583 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
584 sun3_dma_orig_addr = NULL;
585
586 }
587#else
588
589 fifo = dregs->fifo_count;
590 last_residual = fifo;
591
592 /* empty bytes from the fifo which didn't make it */
593 if((!write_flag) && (count - fifo) == 2) {
594 unsigned short data;
595 unsigned char *vaddr;
596
597 data = dregs->fifo_data;
598 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
599
600 vaddr += (sun3_dma_orig_count - fifo);
601
602 vaddr[-2] = (data & 0xff00) >> 8;
603 vaddr[-1] = (data & 0xff);
604 }
605
606 dvma_unmap(sun3_dma_orig_addr);
607 sun3_dma_orig_addr = NULL;
608#endif
609 sun3_udc_write(UDC_RESET, UDC_CSR);
610 dregs->fifo_count = 0;
611 dregs->csr &= ~CSR_SEND;
612
613 /* reset fifo */
614 dregs->csr &= ~CSR_FIFO;
615 dregs->csr |= CSR_FIFO;
616
617 sun3_dma_setup_done = NULL;
618
619 return ret;
620
621}
622
623#include "sun3_NCR5380.c"
624
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100625static struct scsi_host_template driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 .name = SUN3_SCSI_NAME,
627 .detect = sun3scsi_detect,
628 .release = sun3scsi_release,
629 .info = sun3scsi_info,
630 .queuecommand = sun3scsi_queue_command,
631 .eh_abort_handler = sun3scsi_abort,
632 .eh_bus_reset_handler = sun3scsi_bus_reset,
633 .can_queue = CAN_QUEUE,
634 .this_id = 7,
635 .sg_tablesize = SG_TABLESIZE,
636 .cmd_per_lun = CMD_PER_LUN,
637 .use_clustering = DISABLE_CLUSTERING
638};
639
640
641#include "scsi_module.c"
642
643MODULE_LICENSE("GPL");