make oldconfig will rebuild these...
[linux-2.4.21-pre4.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@oh.verio.com)
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_scsi.c,v $
50  * Revision 1.1.1.1  2005/04/11 02:50:37  jack
51  * first release
52  *
53  * Revision 1.1.1.1  2005/01/10 13:16:36  jack
54  * First release
55  *
56  */
57
58 #define AUTOSENSE
59
60 #include <linux/types.h>
61 #include <linux/stddef.h>
62 #include <linux/ctype.h>
63 #include <linux/delay.h>
64
65 #include <linux/module.h>
66 #include <linux/signal.h>
67 #include <linux/sched.h>
68 #include <linux/ioport.h>
69 #include <linux/init.h>
70 #include <linux/blk.h>
71
72 #include <asm/io.h>
73 #include <asm/system.h>
74
75 #include <asm/sun3ints.h>
76 #include <asm/dvma.h>
77 #include <asm/idprom.h>
78 #include <asm/machines.h>
79
80 /* dma on! */
81 #define REAL_DMA
82
83 #include "scsi.h"
84 #include "hosts.h"
85 #include "sun3_scsi.h"
86 #include "NCR5380.h"
87 #include "constants.h"
88
89 /* #define OLDDMA */
90
91 #define USE_WRAPPER
92 /*#define RESET_BOOT */
93 #define DRIVER_SETUP
94
95 #define NDEBUG 0
96
97 /*
98  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
99  */
100 #ifdef BUG
101 #undef RESET_BOOT
102 #undef DRIVER_SETUP
103 #endif
104
105 /* #define SUPPORT_TAGS */
106
107 #define ENABLE_IRQ()    enable_irq( IRQ_SUN3_SCSI ); 
108
109
110 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
111 static inline unsigned char sun3scsi_read(int reg);
112 static inline void sun3scsi_write(int reg, int value);
113
114 static int setup_can_queue = -1;
115 static int setup_cmd_per_lun = -1;
116 static int setup_sg_tablesize = -1;
117 #ifdef SUPPORT_TAGS
118 static int setup_use_tagged_queuing = -1;
119 #endif
120 static int setup_hostid = -1;
121
122 static Scsi_Cmnd *sun3_dma_setup_done = NULL;
123
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
135 static volatile unsigned char *sun3_scsi_regp;
136 static volatile struct sun3_dma_regs *dregs;
137 #ifdef OLDDMA
138 static unsigned char *dmabuf = NULL; /* dma memory buffer */
139 #endif
140 static struct sun3_udc_regs *udc_regs = NULL;
141 static unsigned char *sun3_dma_orig_addr = NULL;
142 static unsigned long sun3_dma_orig_count = 0;
143 static int sun3_dma_active = 0;
144 static unsigned long last_residual = 0;
145
146 /*
147  * NCR 5380 register access functions
148  */
149
150 static inline unsigned char sun3scsi_read(int reg)
151 {
152         return( sun3_scsi_regp[reg] );
153 }
154
155 static inline void sun3scsi_write(int reg, int value)
156 {
157         sun3_scsi_regp[reg] = value;
158 }
159
160 /* dma controller register access functions */
161
162 static 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
174 static 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  */
185 static struct Scsi_Host *default_instance;
186
187 /*
188  * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
189  *
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  
199 int sun3scsi_detect(Scsi_Host_Template * tpnt)
200 {
201         unsigned long ioaddr, iopte;
202         int count = 0;
203         static int called = 0;
204         struct Scsi_Host *instance;
205
206         /* check that this machine has an onboard 5380 */
207         switch(idprom->id_machtype) {
208         case SM_SUN3|SM_3_50:
209         case SM_SUN3|SM_3_60:
210                 break;
211
212         default:
213                 return 0;
214         }
215
216         if(called)
217                 return 0;
218
219         tpnt->proc_name = "Sun3 5380 SCSI";
220
221         /* setup variables */
222         tpnt->can_queue =
223                 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
224         tpnt->cmd_per_lun =
225                 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
226         tpnt->sg_tablesize = 
227                 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
228
229         if (setup_hostid >= 0)
230                 tpnt->this_id = setup_hostid;
231         else {
232                 /* use 7 as default */
233                 tpnt->this_id = 7;
234         }
235
236         /* Taken from Sammy's lance driver: */
237         /* IOBASE_SUN3_SCSI can be found within the IO pmeg with some effort */
238         for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 + SUN3_PMEG_SIZE);
239             ioaddr += SUN3_PTE_SIZE) {
240
241                 iopte = sun3_get_pte(ioaddr);
242                 if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
243                         continue;
244
245                 if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
246                    IOBASE_SUN3_SCSI) {
247                         count = 1;
248                         break;
249                 }
250         }
251
252         if(!count) {
253                 printk("No Sun3 NCR5380 found!\n");
254                 return 0;
255         }
256
257         sun3_scsi_regp = (unsigned char *)ioaddr;
258         dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
259
260         if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
261            == NULL) {
262              printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
263              return 0;
264         }
265 #ifdef OLDDMA
266         if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
267              printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
268              return 0;
269         }
270 #endif
271 #ifdef SUPPORT_TAGS
272         if (setup_use_tagged_queuing < 0)
273                 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
274 #endif
275
276         instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
277         if(instance == NULL)
278                 return 0;
279                 
280         default_instance = instance;
281
282         instance->io_port = (unsigned long) ioaddr;
283         instance->irq = IRQ_SUN3_SCSI;
284
285         NCR5380_init(instance, 0);
286
287         instance->n_io_port = 32;
288
289         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
290
291         if (request_irq(instance->irq, scsi_sun3_intr,
292                              0, "Sun3SCSI-5380", NULL)) {
293 #ifndef REAL_DMA
294                 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
295                        instance->host_no, instance->irq);
296                 instance->irq = IRQ_NONE;
297 #else
298                 printk("scsi%d: IRQ%d not free, bailing out\n",
299                        instance->host_no, instance->irq);
300                 return 0;
301 #endif
302         }
303         
304         printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
305         if (instance->irq == IRQ_NONE)
306                 printk ("s disabled");
307         else
308                 printk (" %d", instance->irq);
309         printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
310                instance->can_queue, instance->cmd_per_lun,
311                SUN3SCSI_PUBLIC_RELEASE);
312         printk("\nscsi%d:", instance->host_no);
313         NCR5380_print_options(instance);
314         printk("\n");
315
316         dregs->csr = 0;
317         udelay(SUN3_DMA_DELAY);
318         dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
319         udelay(SUN3_DMA_DELAY);
320         dregs->fifo_count = 0;
321
322         called = 1;
323
324 #ifdef RESET_BOOT
325         sun3_scsi_reset_boot(instance);
326 #endif
327
328         return 1;
329 }
330
331 int sun3scsi_release (struct Scsi_Host *shpnt)
332 {
333         if (shpnt->irq != IRQ_NONE)
334                 free_irq (shpnt->irq, NULL);
335
336         return 0;
337 }
338
339 #ifdef RESET_BOOT
340 /*
341  * Our 'bus reset on boot' function
342  */
343
344 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
345 {
346         unsigned long end;
347
348         NCR5380_local_declare();
349         NCR5380_setup(instance);
350         
351         /*
352          * Do a SCSI reset to clean up the bus during initialization. No
353          * messing with the queues, interrupts, or locks necessary here.
354          */
355
356         printk( "Sun3 SCSI: resetting the SCSI bus..." );
357
358         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
359 //              sun3_disable_irq( IRQ_SUN3_SCSI );
360
361         /* get in phase */
362         NCR5380_write( TARGET_COMMAND_REG,
363                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
364
365         /* assert RST */
366         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
367
368         /* The min. reset hold time is 25us, so 40us should be enough */
369         udelay( 50 );
370
371         /* reset RST and interrupt */
372         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
373         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
374
375         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
376                 barrier();
377
378         /* switch on SCSI IRQ again */
379 //              sun3_enable_irq( IRQ_SUN3_SCSI );
380
381         printk( " done\n" );
382 }
383 #endif
384
385 const char * sun3scsi_info (struct Scsi_Host *spnt) {
386     return "";
387 }
388
389 // safe bits for the CSR
390 #define CSR_GOOD 0x060f
391
392 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
393 {
394         unsigned short csr = dregs->csr;
395
396         if(csr & ~CSR_GOOD) {
397                 if(csr & CSR_DMA_BUSERR) {
398                         printk("scsi%d: bus error in dma\n", default_instance->host_no);
399                 }
400
401                 if(csr & CSR_DMA_CONFLICT) {
402                         printk("scsi%d: dma conflict\n", default_instance->host_no);
403                 }
404         }
405
406         if(csr & (CSR_SDB_INT | CSR_DMA_INT))
407                 NCR5380_intr(irq, dummy, fp);
408 }
409
410 /*
411  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk; 
412  * reentering NCR5380_print_status seems to have ugly side effects
413  */
414
415 /* this doesn't seem to get used at all -- sam */
416 #if 0
417 void sun3_sun3_debug (void)
418 {
419         unsigned long flags;
420         NCR5380_local_declare();
421
422         if (default_instance) {
423                         save_flags(flags);
424                         cli();
425                         NCR5380_print_status(default_instance);
426                         restore_flags(flags);
427         }
428 }
429 #endif
430
431
432 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
433 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
434 {
435 #ifdef OLDDMA
436         if(write_flag) 
437                 memcpy(dmabuf, data, count);
438         else {
439                 sun3_dma_orig_addr = data;
440                 sun3_dma_orig_count = count;
441         }
442 #else
443         void *addr;
444
445         if(sun3_dma_orig_addr != NULL)
446                 dvma_unmap(sun3_dma_orig_addr);
447
448 //      addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
449         addr = (void *)dvma_map((unsigned long) data, count);
450                 
451         sun3_dma_orig_addr = addr;
452         sun3_dma_orig_count = count;
453 #endif
454         dregs->fifo_count = 0;
455         sun3_udc_write(UDC_RESET, UDC_CSR);
456         
457         /* reset fifo */
458         dregs->csr &= ~CSR_FIFO;
459         dregs->csr |= CSR_FIFO;
460         
461         /* set direction */
462         if(write_flag)
463                 dregs->csr |= CSR_SEND;
464         else
465                 dregs->csr &= ~CSR_SEND;
466         
467         /* byte count for fifo */
468         dregs->fifo_count = count;
469
470         sun3_udc_write(UDC_RESET, UDC_CSR);
471         
472         /* reset fifo */
473         dregs->csr &= ~CSR_FIFO;
474         dregs->csr |= CSR_FIFO;
475         
476         if(dregs->fifo_count != count) { 
477                 printk("scsi%d: fifo_mismatch %04x not %04x\n",
478                        default_instance->host_no, dregs->fifo_count,
479                        (unsigned int) count);
480                 NCR5380_print(default_instance);
481         }
482
483         /* setup udc */
484 #ifdef OLDDMA
485         udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
486         udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
487 #else
488         udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
489         udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
490 #endif
491         udc_regs->count = count/2; /* count in words */
492         udc_regs->mode_hi = UDC_MODE_HIWORD;
493         if(write_flag) {
494                 if(count & 1)
495                         udc_regs->count++;
496                 udc_regs->mode_lo = UDC_MODE_LSEND;
497                 udc_regs->rsel = UDC_RSEL_SEND;
498         } else {
499                 udc_regs->mode_lo = UDC_MODE_LRECV;
500                 udc_regs->rsel = UDC_RSEL_RECV;
501         }
502         
503         /* announce location of regs block */
504         sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
505                        UDC_CHN_HI); 
506
507         sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
508
509         /* set dma master on */
510         sun3_udc_write(0xd, UDC_MODE);
511
512         /* interrupt enable */
513         sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
514         
515         return count;
516
517 }
518
519 static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
520 {
521         unsigned short resid;
522
523         dregs->udc_addr = 0x32; 
524         udelay(SUN3_DMA_DELAY);
525         resid = dregs->udc_data;
526         udelay(SUN3_DMA_DELAY);
527         resid *= 2;
528
529         return (unsigned long) resid;
530 }
531
532 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
533 {
534         return last_residual;
535 }
536
537 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
538                                     int write_flag)
539 {
540         if((cmd->request.cmd == 0) || (cmd->request.cmd == 1))
541                 return wanted;
542         else
543                 return 0;
544 }
545
546 /* clean up after our dma is done */
547 static int sun3scsi_dma_finish(int write_flag)
548 {
549         unsigned short count;
550         unsigned short fifo;
551         int ret = 0;
552         
553         sun3_dma_active = 0;
554 #if 1
555         // check to empty the fifo on a read
556         if(!write_flag) {
557                 int tmo = 20000; /* .2 sec */
558                 
559                 while(1) {
560                         if(dregs->csr & CSR_FIFO_EMPTY)
561                                 break;
562
563                         if(--tmo <= 0) 
564                                 return 1;
565
566                         udelay(10);
567                 }
568         }
569                 
570 #endif
571
572         count = sun3scsi_dma_count(default_instance);
573 #ifdef OLDDMA
574
575         /* if we've finished a read, copy out the data we read */
576         if(sun3_dma_orig_addr) {
577                 /* check for residual bytes after dma end */
578                 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
579                              (BASR_PHASE_MATCH | BASR_ACK))) {
580                         printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
581                         printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
582                         ret = count;
583                 }
584                 
585                 /* copy in what we dma'd no matter what */
586                 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
587                 sun3_dma_orig_addr = NULL;
588
589         }
590 #else
591
592         fifo = dregs->fifo_count;
593         last_residual = fifo;
594
595         /* empty bytes from the fifo which didn't make it */
596         if((!write_flag) && (count - fifo) == 2) {
597                 unsigned short data;
598                 unsigned char *vaddr;
599
600                 data = dregs->fifo_data;
601                 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
602                 
603                 vaddr += (sun3_dma_orig_count - fifo);
604
605                 vaddr[-2] = (data & 0xff00) >> 8;
606                 vaddr[-1] = (data & 0xff);
607         }
608
609         dvma_unmap(sun3_dma_orig_addr);
610         sun3_dma_orig_addr = NULL;
611 #endif
612         sun3_udc_write(UDC_RESET, UDC_CSR);
613         dregs->fifo_count = 0;
614         dregs->csr &= ~CSR_SEND;
615
616         /* reset fifo */
617         dregs->csr &= ~CSR_FIFO;
618         dregs->csr |= CSR_FIFO;
619         
620         sun3_dma_setup_done = NULL;
621
622         return ret;
623
624 }
625         
626 #include "sun3_NCR5380.c"
627
628 static Scsi_Host_Template driver_template = SUN3_NCR5380;
629
630 #include "scsi_module.c"
631
632 MODULE_LICENSE("GPL");