added mtd driver
[linux-2.4.git] / drivers / scsi / sun3_scsi_vme.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  * Adapted from sun3_scsi.c -- see there for other headers
9  *
10  * TODO: modify this driver to support multiple Sun3 SCSI VME boards
11  *
12  */
13
14 #define AUTOSENSE
15
16 #include <linux/types.h>
17 #include <linux/stddef.h>
18 #include <linux/ctype.h>
19 #include <linux/delay.h>
20
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/blk.h>
27
28 #include <asm/io.h>
29 #include <asm/system.h>
30
31 #include <asm/sun3ints.h>
32 #include <asm/dvma.h>
33 #include <asm/idprom.h>
34 #include <asm/machines.h>
35
36 #define SUN3_SCSI_VME
37
38 #undef SUN3_SCSI_DEBUG
39
40 /* dma on! */
41 #define REAL_DMA
42
43 #include "scsi.h"
44 #include "hosts.h"
45 #include "sun3_scsi.h"
46 #include "NCR5380.h"
47 #include "constants.h"
48
49 extern int sun3_map_test(unsigned long, char *);
50
51 #define USE_WRAPPER
52 /*#define RESET_BOOT */
53 #define DRIVER_SETUP
54
55 #define NDEBUG 0
56
57 /*
58  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
59  */
60 #ifdef BUG
61 #undef RESET_BOOT
62 #undef DRIVER_SETUP
63 #endif
64
65 /* #define SUPPORT_TAGS */
66
67 //#define       ENABLE_IRQ()    enable_irq( SUN3_VEC_VMESCSI0 ); 
68 #define ENABLE_IRQ()
69
70
71 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
72 static inline unsigned char sun3scsi_read(int reg);
73 static inline void sun3scsi_write(int reg, int value);
74
75 static int setup_can_queue = -1;
76 MODULE_PARM(setup_can_queue, "i");
77 static int setup_cmd_per_lun = -1;
78 MODULE_PARM(setup_cmd_per_lun, "i");
79 static int setup_sg_tablesize = -1;
80 MODULE_PARM(setup_sg_tablesize, "i");
81 #ifdef SUPPORT_TAGS
82 static int setup_use_tagged_queuing = -1;
83 MODULE_PARM(setup_use_tagged_queuing, "i");
84 #endif
85 static int setup_hostid = -1;
86 MODULE_PARM(setup_hostid, "i");
87
88 static Scsi_Cmnd *sun3_dma_setup_done = NULL;
89
90 #define AFTER_RESET_DELAY       (HZ/2)
91
92 /* ms to wait after hitting dma regs */
93 #define SUN3_DMA_DELAY 10
94
95 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
96 #define SUN3_DVMA_BUFSIZE 0xe000
97
98 /* minimum number of bytes to do dma on */
99 #define SUN3_DMA_MINSIZE 128
100
101 static volatile unsigned char *sun3_scsi_regp;
102 static volatile struct sun3_dma_regs *dregs;
103 #ifdef OLDDMA
104 static unsigned char *dmabuf = NULL; /* dma memory buffer */
105 #endif
106 static unsigned char *sun3_dma_orig_addr = NULL;
107 static unsigned long sun3_dma_orig_count = 0;
108 static int sun3_dma_active = 0;
109 static unsigned long last_residual = 0;
110
111 /*
112  * NCR 5380 register access functions
113  */
114
115 static inline unsigned char sun3scsi_read(int reg)
116 {
117         return( sun3_scsi_regp[reg] );
118 }
119
120 static inline void sun3scsi_write(int reg, int value)
121 {
122         sun3_scsi_regp[reg] = value;
123 }
124
125 /*
126  * XXX: status debug
127  */
128 static struct Scsi_Host *default_instance;
129
130 /*
131  * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
132  *
133  * Purpose : initializes mac NCR5380 driver based on the
134  *      command line / compile time port and irq definitions.
135  *
136  * Inputs : tpnt - template for this SCSI adapter.
137  *
138  * Returns : 1 if a host adapter was found, 0 if not.
139  *
140  */
141  
142 static int sun3scsi_detect(Scsi_Host_Template * tpnt)
143 {
144         unsigned long ioaddr, irq;
145         static int called = 0;
146         struct Scsi_Host *instance;
147         int i;
148         unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI, 
149                                    IOBASE_SUN3_VMESCSI + 0x4000,
150                                    0 };
151         unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
152                                   SUN3_VEC_VMESCSI1,
153                                   0 };
154         /* check that this machine has an onboard 5380 */
155         switch(idprom->id_machtype) {
156         case SM_SUN3|SM_3_160:
157         case SM_SUN3|SM_3_260:
158                 break;
159
160         default:
161                 return 0;
162         }
163
164         if(called)
165                 return 0;
166
167         tpnt->proc_name = "Sun3 5380 VME SCSI";
168
169         /* setup variables */
170         tpnt->can_queue =
171                 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
172         tpnt->cmd_per_lun =
173                 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
174         tpnt->sg_tablesize = 
175                 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
176         
177         if (setup_hostid >= 0)
178                 tpnt->this_id = setup_hostid;
179         else {
180                 /* use 7 as default */
181                 tpnt->this_id = 7;
182         }
183         
184         ioaddr = 0;
185         for(i = 0; addrs[i] != 0; i++) {
186                 unsigned char x;
187                 
188                 ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
189                                                      SUN3_PAGE_TYPE_VME16);
190                 irq = vecs[i];
191                 sun3_scsi_regp = (unsigned char *)ioaddr;
192                 
193                 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
194                 
195                 if(sun3_map_test((unsigned long)dregs, &x)) {
196                         unsigned short oldcsr;
197
198                         oldcsr = dregs->csr;
199                         dregs->csr = 0;
200                         udelay(SUN3_DMA_DELAY);
201                         if(dregs->csr == 0x1400)
202                                 break;
203                         
204                         dregs->csr = oldcsr;
205                 }
206
207                 iounmap((void *)ioaddr);
208                 ioaddr = 0;
209         }
210
211         if(!ioaddr)
212                 return 0;
213         
214 #ifdef SUPPORT_TAGS
215         if (setup_use_tagged_queuing < 0)
216                 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
217 #endif
218
219         instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
220         if(instance == NULL)
221                 return 0;
222                 
223         default_instance = instance;
224
225         instance->io_port = (unsigned long) ioaddr;
226         instance->irq = irq;
227
228         NCR5380_init(instance, 0);
229
230         instance->n_io_port = 32;
231
232         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
233
234         if (request_irq(instance->irq, scsi_sun3_intr,
235                              0, "Sun3SCSI-5380VME", NULL)) {
236 #ifndef REAL_DMA
237                 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
238                        instance->host_no, instance->irq);
239                 instance->irq = SCSI_IRQ_NONE;
240 #else
241                 printk("scsi%d: IRQ%d not free, bailing out\n",
242                        instance->host_no, instance->irq);
243                 return 0;
244 #endif
245         }
246
247         printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
248         if (instance->irq == SCSI_IRQ_NONE)
249                 printk ("s disabled");
250         else
251                 printk (" %d", instance->irq);
252         printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
253                instance->can_queue, instance->cmd_per_lun,
254                SUN3SCSI_PUBLIC_RELEASE);
255         printk("\nscsi%d:", instance->host_no);
256         NCR5380_print_options(instance);
257         printk("\n");
258
259         dregs->csr = 0;
260         udelay(SUN3_DMA_DELAY);
261         dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
262         udelay(SUN3_DMA_DELAY);
263         dregs->fifo_count = 0;
264         dregs->fifo_count_hi = 0;
265         dregs->dma_addr_hi = 0;
266         dregs->dma_addr_lo = 0;
267         dregs->dma_count_hi = 0;
268         dregs->dma_count_lo = 0;
269
270         dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
271
272         called = 1;
273
274 #ifdef RESET_BOOT
275         sun3_scsi_reset_boot(instance);
276 #endif
277
278         return 1;
279 }
280
281 #ifdef MODULE
282 int sun3scsi_release (struct Scsi_Host *shpnt)
283 {
284         if (shpnt->irq != SCSI_IRQ_NONE)
285                 free_irq (shpnt->irq, NULL);
286
287         iounmap(sun3_scsi_regp);
288
289         return 0;
290 }
291 #endif
292
293 #ifdef RESET_BOOT
294 /*
295  * Our 'bus reset on boot' function
296  */
297
298 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
299 {
300         unsigned long end;
301
302         NCR5380_local_declare();
303         NCR5380_setup(instance);
304         
305         /*
306          * Do a SCSI reset to clean up the bus during initialization. No
307          * messing with the queues, interrupts, or locks necessary here.
308          */
309
310         printk( "Sun3 SCSI: resetting the SCSI bus..." );
311
312         /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
313 //              sun3_disable_irq( IRQ_SUN3_SCSI );
314
315         /* get in phase */
316         NCR5380_write( TARGET_COMMAND_REG,
317                       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
318
319         /* assert RST */
320         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
321
322         /* The min. reset hold time is 25us, so 40us should be enough */
323         udelay( 50 );
324
325         /* reset RST and interrupt */
326         NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
327         NCR5380_read( RESET_PARITY_INTERRUPT_REG );
328
329         for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
330                 barrier();
331
332         /* switch on SCSI IRQ again */
333 //              sun3_enable_irq( IRQ_SUN3_SCSI );
334
335         printk( " done\n" );
336 }
337 #endif
338
339 static const char * sun3scsi_info (struct Scsi_Host *spnt) {
340     return "";
341 }
342
343 // safe bits for the CSR
344 #define CSR_GOOD 0x060f
345
346 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
347 {
348         unsigned short csr = dregs->csr;
349
350         dregs->csr &= ~CSR_DMA_ENABLE;
351
352
353 #ifdef SUN3_SCSI_DEBUG
354         printk("scsi_intr csr %x\n", csr);
355 #endif
356
357         if(csr & ~CSR_GOOD) {
358                 if(csr & CSR_DMA_BUSERR) {
359                         printk("scsi%d: bus error in dma\n", default_instance->host_no);
360 #ifdef SUN3_SCSI_DEBUG
361                         printk("scsi: residual %x count %x addr %p dmaaddr %x\n", 
362                                dregs->fifo_count,
363                                dregs->dma_count_lo | (dregs->dma_count_hi << 16),
364                                sun3_dma_orig_addr,
365                                dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
366 #endif
367                 }
368
369                 if(csr & CSR_DMA_CONFLICT) {
370                         printk("scsi%d: dma conflict\n", default_instance->host_no);
371                 }
372         }
373
374         if(csr & (CSR_SDB_INT | CSR_DMA_INT))
375                 NCR5380_intr(irq, dummy, fp);
376 }
377
378 /*
379  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk; 
380  * reentering NCR5380_print_status seems to have ugly side effects
381  */
382
383 /* this doesn't seem to get used at all -- sam */
384 #if 0
385 void sun3_sun3_debug (void)
386 {
387         unsigned long flags;
388         NCR5380_local_declare();
389
390         if (default_instance) {
391                         save_flags(flags);
392                         cli();
393                         NCR5380_print_status(default_instance);
394                         restore_flags(flags);
395         }
396 }
397 #endif
398
399
400 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
401 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
402 {
403         void *addr;
404
405         if(sun3_dma_orig_addr != NULL)
406                 dvma_unmap(sun3_dma_orig_addr);
407
408 //      addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
409         addr = (void *)dvma_map_vme((unsigned long) data, count);
410                 
411         sun3_dma_orig_addr = addr;
412         sun3_dma_orig_count = count;
413         
414 #ifdef SUN3_SCSI_DEBUG
415         printk("scsi: dma_setup addr %p count %x\n", addr, count);
416 #endif
417
418 //      dregs->fifo_count = 0;
419 #if 0   
420         /* reset fifo */
421         dregs->csr &= ~CSR_FIFO;
422         dregs->csr |= CSR_FIFO;
423 #endif  
424         /* set direction */
425         if(write_flag)
426                 dregs->csr |= CSR_SEND;
427         else
428                 dregs->csr &= ~CSR_SEND;
429         
430         /* reset fifo */
431 //      dregs->csr &= ~CSR_FIFO;
432 //      dregs->csr |= CSR_FIFO;
433
434         dregs->csr |= CSR_PACK_ENABLE;
435
436         dregs->dma_addr_hi = ((unsigned long)addr >> 16);
437         dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
438         
439         dregs->dma_count_hi = 0;
440         dregs->dma_count_lo = 0;
441         dregs->fifo_count_hi = 0;
442         dregs->fifo_count = 0;
443                 
444 #ifdef SUN3_SCSI_DEBUG
445         printk("scsi: dma_setup done csr %x\n", dregs->csr);
446 #endif
447         return count;
448
449 }
450
451 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
452 {
453         return last_residual;
454 }
455
456 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
457                                     int write_flag)
458 {
459         if((cmd->request.cmd == 0) || (cmd->request.cmd == 1))
460                 return wanted;
461         else
462                 return 0;
463 }
464
465 static int sun3scsi_dma_start(unsigned long count, char *data)
466 {
467         
468         unsigned short csr;
469
470         csr = dregs->csr;
471 #ifdef SUN3_SCSI_DEBUG
472         printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
473 #endif
474         
475         dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
476         dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
477
478         dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
479         dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
480
481 //      if(!(csr & CSR_DMA_ENABLE))
482 //              dregs->csr |= CSR_DMA_ENABLE;
483
484         return 0;
485 }
486
487 /* clean up after our dma is done */
488 static int sun3scsi_dma_finish(int write_flag)
489 {
490         unsigned short fifo;
491         int ret = 0;
492         
493         sun3_dma_active = 0;
494
495         dregs->csr &= ~CSR_DMA_ENABLE;
496         
497         fifo = dregs->fifo_count;
498         if(write_flag) {
499                 if((fifo > 0) && (fifo < sun3_dma_orig_count))
500                         fifo++;
501         }
502
503         last_residual = fifo;
504 #ifdef SUN3_SCSI_DEBUG
505         printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
506 #endif
507         /* empty bytes from the fifo which didn't make it */
508         if((!write_flag) && (dregs->csr & CSR_LEFT)) {
509                 unsigned char *vaddr;
510
511 #ifdef SUN3_SCSI_DEBUG
512                 printk("scsi: got left over bytes\n");
513 #endif
514
515                 vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
516                 
517                 vaddr += (sun3_dma_orig_count - fifo);
518                 vaddr--;
519                 
520                 switch(dregs->csr & CSR_LEFT) {
521                 case CSR_LEFT_3:
522                         *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
523                         vaddr--;
524                         
525                 case CSR_LEFT_2:
526                         *vaddr = (dregs->bpack_hi & 0x00ff);
527                         vaddr--;
528                         
529                 case CSR_LEFT_1:
530                         *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
531                         break;
532                 }
533                 
534                 
535         }
536
537         dvma_unmap(sun3_dma_orig_addr);
538         sun3_dma_orig_addr = NULL;
539
540         dregs->dma_addr_hi = 0;
541         dregs->dma_addr_lo = 0;
542         dregs->dma_count_hi = 0;
543         dregs->dma_count_lo = 0;
544
545         dregs->fifo_count = 0;
546         dregs->fifo_count_hi = 0;
547
548         dregs->csr &= ~CSR_SEND;
549         
550 //      dregs->csr |= CSR_DMA_ENABLE;
551         
552 #if 0
553         /* reset fifo */
554         dregs->csr &= ~CSR_FIFO;
555         dregs->csr |= CSR_FIFO;
556 #endif  
557         sun3_dma_setup_done = NULL;
558
559         return ret;
560
561 }
562
563 #include "sun3_NCR5380.c"
564
565 static Scsi_Host_Template driver_template = SUN3_NCR5380;
566
567 #include "scsi_module.c"
568