ncr5380: Remove *_RELEASE macros
[cascardo/linux.git] / drivers / scsi / sun3_scsi.c
1 /*
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  * VME support added by Sam Creasey
7  *
8  * TODO: modify this driver to support multiple Sun3 SCSI VME boards
9  *
10  * 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
25 #include <linux/types.h>
26 #include <linux/stddef.h>
27 #include <linux/ctype.h>
28 #include <linux/delay.h>
29
30 #include <linux/module.h>
31 #include <linux/signal.h>
32 #include <linux/ioport.h>
33 #include <linux/init.h>
34 #include <linux/blkdev.h>
35
36 #include <asm/io.h>
37
38 #include <asm/sun3ints.h>
39 #include <asm/dvma.h>
40 #include <asm/idprom.h>
41 #include <asm/machines.h>
42
43 /* dma on! */
44 #define REAL_DMA
45
46 #include "scsi.h"
47 #include <scsi/scsi_host.h>
48 #include "sun3_scsi.h"
49 #include "NCR5380.h"
50
51 extern int sun3_map_test(unsigned long, char *);
52
53 /*#define RESET_BOOT */
54 /* #define SUPPORT_TAGS */
55
56 #ifdef SUN3_SCSI_VME
57 #define ENABLE_IRQ()
58 #else
59 #define ENABLE_IRQ()    enable_irq( IRQ_SUN3_SCSI ); 
60 #endif
61
62
63 static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
64
65 static int setup_can_queue = -1;
66 module_param(setup_can_queue, int, 0);
67 static int setup_cmd_per_lun = -1;
68 module_param(setup_cmd_per_lun, int, 0);
69 static int setup_sg_tablesize = -1;
70 module_param(setup_sg_tablesize, int, 0);
71 #ifdef SUPPORT_TAGS
72 static int setup_use_tagged_queuing = -1;
73 module_param(setup_use_tagged_queuing, int, 0);
74 #endif
75 static int setup_hostid = -1;
76 module_param(setup_hostid, int, 0);
77
78 static struct scsi_cmnd *sun3_dma_setup_done = NULL;
79
80 #define RESET_RUN_DONE
81
82 #define AFTER_RESET_DELAY       (HZ/2)
83
84 /* ms to wait after hitting dma regs */
85 #define SUN3_DMA_DELAY 10
86
87 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
88 #define SUN3_DVMA_BUFSIZE 0xe000
89
90 /* minimum number of bytes to do dma on */
91 #define SUN3_DMA_MINSIZE 128
92
93 static volatile unsigned char *sun3_scsi_regp;
94 static volatile struct sun3_dma_regs *dregs;
95 #ifndef SUN3_SCSI_VME
96 static struct sun3_udc_regs *udc_regs = NULL;
97 #endif
98 static unsigned char *sun3_dma_orig_addr = NULL;
99 static unsigned long sun3_dma_orig_count = 0;
100 static int sun3_dma_active = 0;
101 static unsigned long last_residual = 0;
102
103 /*
104  * NCR 5380 register access functions
105  */
106
107 static inline unsigned char sun3scsi_read(int reg)
108 {
109         return( sun3_scsi_regp[reg] );
110 }
111
112 static inline void sun3scsi_write(int reg, int value)
113 {
114         sun3_scsi_regp[reg] = value;
115 }
116
117 #ifndef SUN3_SCSI_VME
118 /* dma controller register access functions */
119
120 static inline unsigned short sun3_udc_read(unsigned char reg)
121 {
122         unsigned short ret;
123
124         dregs->udc_addr = UDC_CSR;
125         udelay(SUN3_DMA_DELAY);
126         ret = dregs->udc_data;
127         udelay(SUN3_DMA_DELAY);
128         
129         return ret;
130 }
131
132 static inline void sun3_udc_write(unsigned short val, unsigned char reg)
133 {
134         dregs->udc_addr = reg;
135         udelay(SUN3_DMA_DELAY);
136         dregs->udc_data = val;
137         udelay(SUN3_DMA_DELAY);
138 }
139 #endif
140
141 /*
142  * XXX: status debug
143  */
144 static struct Scsi_Host *default_instance;
145
146 /*
147  * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
148  *
149  * Purpose : initializes mac NCR5380 driver based on the
150  *      command line / compile time port and irq definitions.
151  *
152  * Inputs : tpnt - template for this SCSI adapter.
153  *
154  * Returns : 1 if a host adapter was found, 0 if not.
155  *
156  */
157  
158 static int __init sun3scsi_detect(struct scsi_host_template *tpnt)
159 {
160         unsigned long ioaddr, irq;
161         static int called = 0;
162         struct Scsi_Host *instance;
163 #ifdef SUN3_SCSI_VME
164         int i;
165         unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
166                                    IOBASE_SUN3_VMESCSI + 0x4000,
167                                    0 };
168         unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
169                                   SUN3_VEC_VMESCSI1,
170                                   0 };
171 #endif
172
173         /* check that this machine has an onboard 5380 */
174         switch(idprom->id_machtype) {
175 #ifdef SUN3_SCSI_VME
176         case SM_SUN3|SM_3_160:
177         case SM_SUN3|SM_3_260:
178                 break;
179 #else
180         case SM_SUN3|SM_3_50:
181         case SM_SUN3|SM_3_60:
182                 break;
183 #endif
184
185         default:
186                 return 0;
187         }
188
189         if(called)
190                 return 0;
191
192 #ifdef SUN3_SCSI_VME
193         tpnt->proc_name = "Sun3 5380 VME SCSI";
194 #else
195         tpnt->proc_name = "Sun3 5380 SCSI";
196 #endif
197
198         /* setup variables */
199         if (setup_can_queue > 0)
200                 tpnt->can_queue = setup_can_queue;
201         if (setup_cmd_per_lun > 0)
202                 tpnt->cmd_per_lun = setup_cmd_per_lun;
203         if (setup_sg_tablesize >= 0)
204                 tpnt->sg_tablesize = setup_sg_tablesize;
205
206         if (setup_hostid >= 0)
207                 tpnt->this_id = setup_hostid;
208         else {
209                 /* use 7 as default */
210                 tpnt->this_id = 7;
211         }
212
213 #ifdef SUN3_SCSI_VME
214         ioaddr = 0;
215         for (i = 0; addrs[i] != 0; i++) {
216                 unsigned char x;
217
218                 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
219                                                      SUN3_PAGE_TYPE_VME16);
220                 irq = vecs[i];
221                 sun3_scsi_regp = (unsigned char *)ioaddr;
222
223                 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
224
225                 if (sun3_map_test((unsigned long)dregs, &x)) {
226                         unsigned short oldcsr;
227
228                         oldcsr = dregs->csr;
229                         dregs->csr = 0;
230                         udelay(SUN3_DMA_DELAY);
231                         if (dregs->csr == 0x1400)
232                                 break;
233
234                         dregs->csr = oldcsr;
235                 }
236
237                 iounmap((void *)ioaddr);
238                 ioaddr = 0;
239         }
240
241         if (!ioaddr)
242                 return 0;
243 #else
244         irq = IRQ_SUN3_SCSI;
245         ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
246         sun3_scsi_regp = (unsigned char *)ioaddr;
247
248         dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
249
250         if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
251            == NULL) {
252              printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
253              return 0;
254         }
255 #endif
256 #ifdef SUPPORT_TAGS
257         if (setup_use_tagged_queuing < 0)
258                 setup_use_tagged_queuing = 1;
259 #endif
260
261         instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
262         if(instance == NULL)
263                 return 0;
264                 
265         default_instance = instance;
266
267         instance->io_port = (unsigned long) ioaddr;
268         instance->irq = irq;
269
270         NCR5380_init(instance, 0);
271
272         instance->n_io_port = 32;
273
274         if (request_irq(instance->irq, scsi_sun3_intr,
275                              0, "Sun3SCSI-5380", instance)) {
276 #ifndef REAL_DMA
277                 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
278                        instance->host_no, instance->irq);
279                 instance->irq = NO_IRQ;
280 #else
281                 printk("scsi%d: IRQ%d not free, bailing out\n",
282                        instance->host_no, instance->irq);
283                 return 0;
284 #endif
285         }
286         
287         dregs->csr = 0;
288         udelay(SUN3_DMA_DELAY);
289         dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
290         udelay(SUN3_DMA_DELAY);
291         dregs->fifo_count = 0;
292 #ifdef SUN3_SCSI_VME
293         dregs->fifo_count_hi = 0;
294         dregs->dma_addr_hi = 0;
295         dregs->dma_addr_lo = 0;
296         dregs->dma_count_hi = 0;
297         dregs->dma_count_lo = 0;
298
299         dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
300 #endif
301
302         called = 1;
303
304 #ifdef RESET_BOOT
305         sun3_scsi_reset_boot(instance);
306 #endif
307
308         return 1;
309 }
310
311 static int sun3scsi_release(struct Scsi_Host *shpnt)
312 {
313         if (shpnt->irq != NO_IRQ)
314                 free_irq(shpnt->irq, shpnt);
315
316         iounmap((void *)sun3_scsi_regp);
317
318         NCR5380_exit(shpnt);
319         return 0;
320 }
321
322 #ifdef RESET_BOOT
323 /*
324  * Our 'bus reset on boot' function
325  */
326
327 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
328 {
329         unsigned long end;
330
331         NCR5380_local_declare();
332         NCR5380_setup(instance);
333         
334         /*
335          * Do a SCSI reset to clean up the bus during initialization. No
336          * messing with the queues, interrupts, or locks necessary here.
337          */
338
339         printk( "Sun3 SCSI: resetting the SCSI bus..." );
340
341         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
342 //              sun3_disable_irq( IRQ_SUN3_SCSI );
343
344         /* get in phase */
345         NCR5380_write( TARGET_COMMAND_REG,
346                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
347
348         /* assert RST */
349         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
350
351         /* The min. reset hold time is 25us, so 40us should be enough */
352         udelay( 50 );
353
354         /* reset RST and interrupt */
355         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
356         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
357
358         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
359                 barrier();
360
361         /* switch on SCSI IRQ again */
362 //              sun3_enable_irq( IRQ_SUN3_SCSI );
363
364         printk( " done\n" );
365 }
366 #endif
367
368 // safe bits for the CSR
369 #define CSR_GOOD 0x060f
370
371 static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
372 {
373         unsigned short csr = dregs->csr;
374         int handled = 0;
375
376 #ifdef SUN3_SCSI_VME
377         dregs->csr &= ~CSR_DMA_ENABLE;
378 #endif
379
380         if(csr & ~CSR_GOOD) {
381                 if(csr & CSR_DMA_BUSERR) {
382                         printk("scsi%d: bus error in dma\n", default_instance->host_no);
383                 }
384
385                 if(csr & CSR_DMA_CONFLICT) {
386                         printk("scsi%d: dma conflict\n", default_instance->host_no);
387                 }
388                 handled = 1;
389         }
390
391         if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
392                 NCR5380_intr(irq, dummy);
393                 handled = 1;
394         }
395
396         return IRQ_RETVAL(handled);
397 }
398
399 /*
400  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk; 
401  * reentering NCR5380_print_status seems to have ugly side effects
402  */
403
404 /* this doesn't seem to get used at all -- sam */
405 #if 0
406 void sun3_sun3_debug (void)
407 {
408         unsigned long flags;
409         NCR5380_local_declare();
410
411         if (default_instance) {
412                         local_irq_save(flags);
413                         NCR5380_print_status(default_instance);
414                         local_irq_restore(flags);
415         }
416 }
417 #endif
418
419
420 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
421 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
422 {
423         void *addr;
424
425         if(sun3_dma_orig_addr != NULL)
426                 dvma_unmap(sun3_dma_orig_addr);
427
428 #ifdef SUN3_SCSI_VME
429         addr = (void *)dvma_map_vme((unsigned long) data, count);
430 #else
431         addr = (void *)dvma_map((unsigned long) data, count);
432 #endif
433                 
434         sun3_dma_orig_addr = addr;
435         sun3_dma_orig_count = count;
436
437 #ifndef SUN3_SCSI_VME
438         dregs->fifo_count = 0;
439         sun3_udc_write(UDC_RESET, UDC_CSR);
440         
441         /* reset fifo */
442         dregs->csr &= ~CSR_FIFO;
443         dregs->csr |= CSR_FIFO;
444 #endif
445         
446         /* set direction */
447         if(write_flag)
448                 dregs->csr |= CSR_SEND;
449         else
450                 dregs->csr &= ~CSR_SEND;
451         
452 #ifdef SUN3_SCSI_VME
453         dregs->csr |= CSR_PACK_ENABLE;
454
455         dregs->dma_addr_hi = ((unsigned long)addr >> 16);
456         dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
457
458         dregs->dma_count_hi = 0;
459         dregs->dma_count_lo = 0;
460         dregs->fifo_count_hi = 0;
461         dregs->fifo_count = 0;
462 #else
463         /* byte count for fifo */
464         dregs->fifo_count = count;
465
466         sun3_udc_write(UDC_RESET, UDC_CSR);
467         
468         /* reset fifo */
469         dregs->csr &= ~CSR_FIFO;
470         dregs->csr |= CSR_FIFO;
471         
472         if(dregs->fifo_count != count) { 
473                 printk("scsi%d: fifo_mismatch %04x not %04x\n",
474                        default_instance->host_no, dregs->fifo_count,
475                        (unsigned int) count);
476                 NCR5380_dprint(NDEBUG_DMA, default_instance);
477         }
478
479         /* setup udc */
480         udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
481         udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
482         udc_regs->count = count/2; /* count in words */
483         udc_regs->mode_hi = UDC_MODE_HIWORD;
484         if(write_flag) {
485                 if(count & 1)
486                         udc_regs->count++;
487                 udc_regs->mode_lo = UDC_MODE_LSEND;
488                 udc_regs->rsel = UDC_RSEL_SEND;
489         } else {
490                 udc_regs->mode_lo = UDC_MODE_LRECV;
491                 udc_regs->rsel = UDC_RSEL_RECV;
492         }
493         
494         /* announce location of regs block */
495         sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
496                        UDC_CHN_HI); 
497
498         sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
499
500         /* set dma master on */
501         sun3_udc_write(0xd, UDC_MODE);
502
503         /* interrupt enable */
504         sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
505 #endif
506         
507         return count;
508
509 }
510
511 #ifndef SUN3_SCSI_VME
512 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
513 {
514         unsigned short resid;
515
516         dregs->udc_addr = 0x32; 
517         udelay(SUN3_DMA_DELAY);
518         resid = dregs->udc_data;
519         udelay(SUN3_DMA_DELAY);
520         resid *= 2;
521
522         return (unsigned long) resid;
523 }
524 #endif
525
526 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
527 {
528         return last_residual;
529 }
530
531 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
532                                                   struct scsi_cmnd *cmd,
533                                                   int write_flag)
534 {
535         if (cmd->request->cmd_type == REQ_TYPE_FS)
536                 return wanted;
537         else
538                 return 0;
539 }
540
541 static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
542 {
543 #ifdef SUN3_SCSI_VME
544         unsigned short csr;
545
546         csr = dregs->csr;
547
548         dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
549         dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
550
551         dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
552         dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
553
554 /*      if(!(csr & CSR_DMA_ENABLE))
555  *              dregs->csr |= CSR_DMA_ENABLE;
556  */
557 #else
558     sun3_udc_write(UDC_CHN_START, UDC_CSR);
559 #endif
560     
561     return 0;
562 }
563
564 /* clean up after our dma is done */
565 static int sun3scsi_dma_finish(int write_flag)
566 {
567         unsigned short __maybe_unused count;
568         unsigned short fifo;
569         int ret = 0;
570         
571         sun3_dma_active = 0;
572
573 #ifdef SUN3_SCSI_VME
574         dregs->csr &= ~CSR_DMA_ENABLE;
575
576         fifo = dregs->fifo_count;
577         if (write_flag) {
578                 if ((fifo > 0) && (fifo < sun3_dma_orig_count))
579                         fifo++;
580         }
581
582         last_residual = fifo;
583         /* empty bytes from the fifo which didn't make it */
584         if ((!write_flag) && (dregs->csr & CSR_LEFT)) {
585                 unsigned char *vaddr;
586
587                 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
588
589                 vaddr += (sun3_dma_orig_count - fifo);
590                 vaddr--;
591
592                 switch (dregs->csr & CSR_LEFT) {
593                 case CSR_LEFT_3:
594                         *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
595                         vaddr--;
596
597                 case CSR_LEFT_2:
598                         *vaddr = (dregs->bpack_hi & 0x00ff);
599                         vaddr--;
600
601                 case CSR_LEFT_1:
602                         *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
603                         break;
604                 }
605         }
606 #else
607         // check to empty the fifo on a read
608         if(!write_flag) {
609                 int tmo = 20000; /* .2 sec */
610                 
611                 while(1) {
612                         if(dregs->csr & CSR_FIFO_EMPTY)
613                                 break;
614
615                         if(--tmo <= 0) {
616                                 printk("sun3scsi: fifo failed to empty!\n");
617                                 return 1;
618                         }
619                         udelay(10);
620                 }
621         }
622
623         count = sun3scsi_dma_count(default_instance);
624
625         fifo = dregs->fifo_count;
626         last_residual = fifo;
627
628         /* empty bytes from the fifo which didn't make it */
629         if((!write_flag) && (count - fifo) == 2) {
630                 unsigned short data;
631                 unsigned char *vaddr;
632
633                 data = dregs->fifo_data;
634                 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
635                 
636                 vaddr += (sun3_dma_orig_count - fifo);
637
638                 vaddr[-2] = (data & 0xff00) >> 8;
639                 vaddr[-1] = (data & 0xff);
640         }
641 #endif
642
643         dvma_unmap(sun3_dma_orig_addr);
644         sun3_dma_orig_addr = NULL;
645
646 #ifdef SUN3_SCSI_VME
647         dregs->dma_addr_hi = 0;
648         dregs->dma_addr_lo = 0;
649         dregs->dma_count_hi = 0;
650         dregs->dma_count_lo = 0;
651
652         dregs->fifo_count = 0;
653         dregs->fifo_count_hi = 0;
654
655         dregs->csr &= ~CSR_SEND;
656 /*      dregs->csr |= CSR_DMA_ENABLE; */
657 #else
658         sun3_udc_write(UDC_RESET, UDC_CSR);
659         dregs->fifo_count = 0;
660         dregs->csr &= ~CSR_SEND;
661
662         /* reset fifo */
663         dregs->csr &= ~CSR_FIFO;
664         dregs->csr |= CSR_FIFO;
665 #endif
666         
667         sun3_dma_setup_done = NULL;
668
669         return ret;
670
671 }
672         
673 #include "sun3_NCR5380.c"
674
675 static struct scsi_host_template driver_template = {
676         .show_info              = sun3scsi_show_info,
677         .name                   = SUN3_SCSI_NAME,
678         .detect                 = sun3scsi_detect,
679         .release                = sun3scsi_release,
680         .info                   = sun3scsi_info,
681         .queuecommand           = sun3scsi_queue_command,
682         .eh_abort_handler       = sun3scsi_abort,
683         .eh_bus_reset_handler   = sun3scsi_bus_reset,
684         .can_queue              = 16,
685         .this_id                = 7,
686         .sg_tablesize           = SG_NONE,
687         .cmd_per_lun            = 2,
688         .use_clustering         = DISABLE_CLUSTERING
689 };
690
691
692 #include "scsi_module.c"
693
694 MODULE_LICENSE("GPL");