ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / NCR53c406a.c
1 /* 
2  *  NCR53c406.c
3  *  Low-level SCSI driver for NCR53c406a chip.
4  *  Copyright (C) 1994, 1995, 1996 Normunds Saumanis (normunds@fi.ibm.com)
5  * 
6  *  LILO command line usage: ncr53c406a=<PORTBASE>[,<IRQ>[,<FASTPIO>]]
7  *  Specify IRQ = 0 for non-interrupt driven mode.
8  *  FASTPIO = 1 for fast pio mode, 0 for slow mode.
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2, or (at your option) any
13  *  later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  */
21
22 #define NCR53C406A_DEBUG 0
23 #define VERBOSE_NCR53C406A_DEBUG 0
24
25 /* Set this to 1 for PIO mode (recommended) or to 0 for DMA mode */
26 #define USE_PIO 1
27
28 #define USE_BIOS 0
29                                 /* #define BIOS_ADDR 0xD8000 *//* define this if autoprobe fails */
30                                 /* #define PORT_BASE 0x330 *//* define this if autoprobe fails */
31                                 /* #define IRQ_LEV   0  *//* define this if autoprobe fails */
32 #define DMA_CHAN  5             /* this is ignored if DMA is disabled */
33
34 /* Set this to 0 if you encounter kernel lockups while transferring 
35  * data in PIO mode */
36 #define USE_FAST_PIO 1
37
38 /* ============= End of user configurable parameters ============= */
39
40 #include <linux/module.h>
41
42 #include <linux/errno.h>
43 #include <linux/ioport.h>
44 #include <linux/sched.h>
45 #include <linux/interrupt.h>
46 #include <linux/proc_fs.h>
47 #include <linux/stat.h>
48 #include <linux/init.h>
49 #include <asm/io.h>
50 #include <asm/dma.h>
51 #include <asm/bitops.h>
52 #include <asm/irq.h>
53
54 #include <linux/blkdev.h>
55 #include <linux/spinlock.h>
56 #include "scsi.h"
57 #include "hosts.h"
58
59 /* ============================================================= */
60
61 #define WATCHDOG 5000000
62
63 #define SYNC_MODE 0             /* Synchronous transfer mode */
64
65 #if DEBUG
66 #undef NCR53C406A_DEBUG
67 #define NCR53C406A_DEBUG 1
68 #endif
69
70 #if USE_PIO
71 #define USE_DMA 0
72 #else
73 #define USE_DMA 1
74 #endif
75
76 /* Default configuration */
77 #define C1_IMG   0x07           /* ID=7 */
78 #define C2_IMG   0x48           /* FE SCSI2 */
79 #if USE_DMA
80 #define C3_IMG   0x21           /* CDB TE */
81 #else
82 #define C3_IMG   0x20           /* CDB */
83 #endif
84 #define C4_IMG   0x04           /* ANE */
85 #define C5_IMG   0xb6           /* AA PI SIE POL */
86
87 #define REG0 (outb(C4_IMG, CONFIG4))
88 #define REG1 (outb(C5_IMG, CONFIG5))
89
90 #if NCR53C406A_DEBUG
91 #define DEB(x) x
92 #else
93 #define DEB(x)
94 #endif
95
96 #if VERBOSE_NCR53C406A_DEBUG
97 #define VDEB(x) x
98 #else
99 #define VDEB(x)
100 #endif
101
102 #define LOAD_DMA_COUNT(count) \
103   outb(count & 0xff, TC_LSB); \
104   outb((count >> 8) & 0xff, TC_MSB); \
105   outb((count >> 16) & 0xff, TC_HIGH);
106
107 /* Chip commands */
108 #define DMA_OP               0x80
109
110 #define SCSI_NOP             0x00
111 #define FLUSH_FIFO           0x01
112 #define CHIP_RESET           0x02
113 #define SCSI_RESET           0x03
114 #define RESELECT             0x40
115 #define SELECT_NO_ATN        0x41
116 #define SELECT_ATN           0x42
117 #define SELECT_ATN_STOP      0x43
118 #define ENABLE_SEL           0x44
119 #define DISABLE_SEL          0x45
120 #define SELECT_ATN3          0x46
121 #define RESELECT3            0x47
122 #define TRANSFER_INFO        0x10
123 #define INIT_CMD_COMPLETE    0x11
124 #define MSG_ACCEPT           0x12
125 #define TRANSFER_PAD         0x18
126 #define SET_ATN              0x1a
127 #define RESET_ATN            0x1b
128 #define SEND_MSG             0x20
129 #define SEND_STATUS          0x21
130 #define SEND_DATA            0x22
131 #define DISCONN_SEQ          0x23
132 #define TERMINATE_SEQ        0x24
133 #define TARG_CMD_COMPLETE    0x25
134 #define DISCONN              0x27
135 #define RECV_MSG             0x28
136 #define RECV_CMD             0x29
137 #define RECV_DATA            0x2a
138 #define RECV_CMD_SEQ         0x2b
139 #define TARGET_ABORT_DMA     0x04
140
141 /*----------------------------------------------------------------*/
142 /* the following will set the monitor border color (useful to find
143    where something crashed or gets stuck at */
144 /* 1 = blue
145    2 = green
146    3 = cyan
147    4 = red
148    5 = magenta
149    6 = yellow
150    7 = white
151 */
152
153 #if NCR53C406A_DEBUG
154 #define rtrc(i) {inb(0x3da);outb(0x31,0x3c0);outb((i),0x3c0);}
155 #else
156 #define rtrc(i) {}
157 #endif
158 /*----------------------------------------------------------------*/
159
160 enum Phase {
161         idle,
162         data_out,
163         data_in,
164         command_ph,
165         status_ph,
166         message_out,
167         message_in
168 };
169
170 /* Static function prototypes */
171 static void NCR53c406a_intr(int, void *, struct pt_regs *);
172 static irqreturn_t do_NCR53c406a_intr(int, void *, struct pt_regs *);
173 static void wait_intr(void);
174 static void chip_init(void);
175 static void calc_port_addr(void);
176 #ifndef IRQ_LEV
177 static int irq_probe(void);
178 #endif
179
180 /* ================================================================= */
181
182 #if USE_BIOS
183 static void *bios_base;
184 #endif
185
186 #if PORT_BASE
187 static int port_base = PORT_BASE;
188 #else
189 static int port_base;
190 #endif
191
192 #if IRQ_LEV
193 static int irq_level = IRQ_LEV;
194 #else
195 static int irq_level = -1;      /* 0 is 'no irq', so use -1 for 'uninitialized' */
196 #endif
197
198 #if USE_DMA
199 static int dma_chan;
200 #endif
201
202 #if USE_PIO
203 static int fast_pio = USE_FAST_PIO;
204 #endif
205
206 static Scsi_Cmnd *current_SC;
207 static char info_msg[256];
208
209 /* ================================================================= */
210
211 /* possible BIOS locations */
212 #if USE_BIOS
213 static void *addresses[] = {
214         (void *) 0xd8000,
215         (void *) 0xc8000
216 };
217 #define ADDRESS_COUNT (sizeof( addresses ) / sizeof( unsigned ))
218 #endif                          /* USE_BIOS */
219
220 /* possible i/o port addresses */
221 static unsigned short ports[] = { 0x230, 0x330, 0x280, 0x290, 0x330, 0x340, 0x300, 0x310, 0x348, 0x350 };
222 #define PORT_COUNT (sizeof( ports ) / sizeof( unsigned short ))
223
224 /* possible interrupt channels */
225 static unsigned short intrs[] = { 10, 11, 12, 15 };
226 #define INTR_COUNT (sizeof( intrs ) / sizeof( unsigned short ))
227
228 /* signatures for NCR 53c406a based controllers */
229 #if USE_BIOS
230 struct signature {
231         char *signature;
232         int sig_offset;
233         int sig_length;
234 } signatures[] __initdata = {
235         /*          1         2         3         4         5         6 */
236         /* 123456789012345678901234567890123456789012345678901234567890 */
237         {
238 "Copyright (C) Acculogic, Inc.\r\n2.8M Diskette Extension Bios ver 4.04.03 03/01/1993", 61, 82},};
239
240 #define SIGNATURE_COUNT (sizeof( signatures ) / sizeof( struct signature ))
241 #endif                          /* USE_BIOS */
242
243 /* ============================================================ */
244
245 /* Control Register Set 0 */
246 static int TC_LSB;              /* transfer counter lsb         */
247 static int TC_MSB;              /* transfer counter msb */
248 static int SCSI_FIFO;           /* scsi fifo register   */
249 static int CMD_REG;             /* command register             */
250 static int STAT_REG;            /* status register              */
251 static int DEST_ID;             /* selection/reselection bus id */
252 static int INT_REG;             /* interrupt status register    */
253 static int SRTIMOUT;            /* select/reselect timeout reg  */
254 static int SEQ_REG;             /* sequence step register       */
255 static int SYNCPRD;             /* synchronous transfer period  */
256 static int FIFO_FLAGS;          /* indicates # of bytes in fifo */
257 static int SYNCOFF;             /* synchronous offset register  */
258 static int CONFIG1;             /* configuration register       */
259 static int CLKCONV;             /* clock conversion reg */
260                                 /*static int TESTREG;*//* test mode register           */
261 static int CONFIG2;             /* Configuration 2 Register     */
262 static int CONFIG3;             /* Configuration 3 Register     */
263 static int CONFIG4;             /* Configuration 4 Register     */
264 static int TC_HIGH;             /* Transfer Counter High */
265                                 /*static int FIFO_BOTTOM;*//* Reserve FIFO byte register   */
266
267 /* Control Register Set 1 */
268                                 /*static int JUMPER_SENSE;*//* Jumper sense port reg (r/w) */
269                                 /*static int SRAM_PTR;*//* SRAM address pointer reg (r/w) */
270                                 /*static int SRAM_DATA;*//* SRAM data register (r/w) */
271 static int PIO_FIFO;            /* PIO FIFO registers (r/w) */
272                                 /*static int PIO_FIFO1;*//*  */
273                                 /*static int PIO_FIFO2;*//*  */
274                                 /*static int PIO_FIFO3;*//*  */
275 static int PIO_STATUS;          /* PIO status (r/w) */
276                                 /*static int ATA_CMD;*//* ATA command/status reg (r/w) */
277                                 /*static int ATA_ERR;*//* ATA features/error register (r/w) */
278 static int PIO_FLAG;            /* PIO flag interrupt enable (r/w) */
279 static int CONFIG5;             /* Configuration 5 register (r/w) */
280                                 /*static int SIGNATURE;*//* Signature Register (r) */
281                                 /*static int CONFIG6;*//* Configuration 6 register (r) */
282
283 /* ============================================================== */
284
285 #if USE_DMA
286 static __inline__ int NCR53c406a_dma_setup(unsigned char *ptr, unsigned int count, unsigned char mode)
287 {
288         unsigned limit;
289         unsigned long flags = 0;
290
291         VDEB(printk("dma: before count=%d   ", count));
292         if (dma_chan <= 3) {
293                 if (count > 65536)
294                         count = 65536;
295                 limit = 65536 - (((unsigned) ptr) & 0xFFFF);
296         } else {
297                 if (count > (65536 << 1))
298                         count = (65536 << 1);
299                 limit = (65536 << 1) - (((unsigned) ptr) & 0x1FFFF);
300         }
301
302         if (count > limit)
303                 count = limit;
304
305         VDEB(printk("after count=%d\n", count));
306         if ((count & 1) || (((unsigned) ptr) & 1))
307                 panic("NCR53c406a: attempted unaligned DMA transfer\n");
308
309         flags = claim_dma_lock();
310         disable_dma(dma_chan);
311         clear_dma_ff(dma_chan);
312         set_dma_addr(dma_chan, (long) ptr);
313         set_dma_count(dma_chan, count);
314         set_dma_mode(dma_chan, mode);
315         enable_dma(dma_chan);
316         release_dma_lock(flags);
317
318         return count;
319 }
320
321 static __inline__ int NCR53c406a_dma_write(unsigned char *src, unsigned int count)
322 {
323         return NCR53c406a_dma_setup(src, count, DMA_MODE_WRITE);
324 }
325
326 static __inline__ int NCR53c406a_dma_read(unsigned char *src, unsigned int count)
327 {
328         return NCR53c406a_dma_setup(src, count, DMA_MODE_READ);
329 }
330
331 static __inline__ int NCR53c406a_dma_residual(void)
332 {
333         register int tmp;
334         unsigned long flags;
335
336         flags = claim_dma_lock();
337         clear_dma_ff(dma_chan);
338         tmp = get_dma_residue(dma_chan);
339         release_dma_lock(flags);
340
341         return tmp;
342 }
343 #endif                          /* USE_DMA */
344
345 #if USE_PIO
346 static __inline__ int NCR53c406a_pio_read(unsigned char *request, unsigned int reqlen)
347 {
348         int i;
349         int len;                /* current scsi fifo size */
350
351         REG1;
352         while (reqlen) {
353                 i = inb(PIO_STATUS);
354                 /*    VDEB(printk("pio_status=%x\n", i)); */
355                 if (i & 0x80)
356                         return 0;
357
358                 switch (i & 0x1e) {
359                 default:
360                 case 0x10:
361                         len = 0;
362                         break;
363                 case 0x0:
364                         len = 1;
365                         break;
366                 case 0x8:
367                         len = 42;
368                         break;
369                 case 0xc:
370                         len = 84;
371                         break;
372                 case 0xe:
373                         len = 128;
374                         break;
375                 }
376
377                 if ((i & 0x40) && len == 0) {   /* fifo empty and interrupt occurred */
378                         return 0;
379                 }
380
381                 if (len) {
382                         if (len > reqlen)
383                                 len = reqlen;
384
385                         if (fast_pio && len > 3) {
386                                 insl(PIO_FIFO, request, len >> 2);
387                                 request += len & 0xfc;
388                                 reqlen -= len & 0xfc;
389                         } else {
390                                 while (len--) {
391                                         *request++ = inb(PIO_FIFO);
392                                         reqlen--;
393                                 }
394                         }
395                 }
396         }
397         return 0;
398 }
399
400 static __inline__ int NCR53c406a_pio_write(unsigned char *request, unsigned int reqlen)
401 {
402         int i = 0;
403         int len;                /* current scsi fifo size */
404
405         REG1;
406         while (reqlen && !(i & 0x40)) {
407                 i = inb(PIO_STATUS);
408                 /*    VDEB(printk("pio_status=%x\n", i)); */
409                 if (i & 0x80)   /* error */
410                         return 0;
411
412                 switch (i & 0x1e) {
413                 case 0x10:
414                         len = 128;
415                         break;
416                 case 0x0:
417                         len = 84;
418                         break;
419                 case 0x8:
420                         len = 42;
421                         break;
422                 case 0xc:
423                         len = 1;
424                         break;
425                 default:
426                 case 0xe:
427                         len = 0;
428                         break;
429                 }
430
431                 if (len) {
432                         if (len > reqlen)
433                                 len = reqlen;
434
435                         if (fast_pio && len > 3) {
436                                 outsl(PIO_FIFO, request, len >> 2);
437                                 request += len & 0xfc;
438                                 reqlen -= len & 0xfc;
439                         } else {
440                                 while (len--) {
441                                         outb(*request++, PIO_FIFO);
442                                         reqlen--;
443                                 }
444                         }
445                 }
446         }
447         return 0;
448 }
449 #endif                          /* USE_PIO */
450
451 static int __init NCR53c406a_detect(Scsi_Host_Template * tpnt)
452 {
453         int present = 0;
454         struct Scsi_Host *shpnt = NULL;
455 #ifndef PORT_BASE
456         int i;
457 #endif
458
459 #if USE_BIOS
460         int ii, jj;
461         bios_base = 0;
462         /* look for a valid signature */
463         for (ii = 0; ii < ADDRESS_COUNT && !bios_base; ii++)
464                 for (jj = 0; (jj < SIGNATURE_COUNT) && !bios_base; jj++)
465                         if (!memcmp((void *) addresses[ii] + signatures[jj].sig_offset, (void *) signatures[jj].signature, (int) signatures[jj].sig_length))
466                                 bios_base = addresses[ii];
467
468         if (!bios_base) {
469                 printk("NCR53c406a: BIOS signature not found\n");
470                 return 0;
471         }
472
473         DEB(printk("NCR53c406a BIOS found at 0x%x\n", (unsigned int) bios_base);
474             );
475 #endif                          /* USE_BIOS */
476
477 #ifdef PORT_BASE
478         if (!request_region(port_base, 0x10, "NCR53c406a"))     /* ports already snatched */
479                 port_base = 0;
480
481 #else                           /* autodetect */
482         if (port_base) {        /* LILO override */
483                 if (!request_region(port_base, 0x10, "NCR53c406a"))
484                         port_base = 0;
485         } else {
486                 for (i = 0; i < PORT_COUNT && !port_base; i++) {
487                         if (!request_region(ports[i], 0x10, "NCR53c406a")) {
488                                 DEB(printk("NCR53c406a: port 0x%x in use\n", ports[i]));
489                         } else {
490                                 VDEB(printk("NCR53c406a: port 0x%x available\n", ports[i]));
491                                 outb(C5_IMG, ports[i] + 0x0d);  /* reg set 1 */
492                                 if ((inb(ports[i] + 0x0e) ^ inb(ports[i] + 0x0e)) == 7 && (inb(ports[i] + 0x0e) ^ inb(ports[i] + 0x0e)) == 7 && (inb(ports[i] + 0x0e) & 0xf8) == 0x58) {
493                                         port_base = ports[i];
494                                         VDEB(printk("NCR53c406a: Sig register valid\n"));
495                                         VDEB(printk("port_base=0x%x\n", port_base));
496                                         break;
497                                 }
498                                 release_region(ports[i], 0x10);
499                         }
500                 }
501         }
502 #endif                          /* PORT_BASE */
503
504         if (!port_base) {       /* no ports found */
505                 printk("NCR53c406a: no available ports found\n");
506                 return 0;
507         }
508
509         DEB(printk("NCR53c406a detected\n"));
510
511         calc_port_addr();
512         chip_init();
513
514 #ifndef IRQ_LEV
515         if (irq_level < 0) {    /* LILO override if >= 0 */
516                 irq_level = irq_probe();
517                 if (irq_level < 0) {    /* Trouble */
518                         printk("NCR53c406a: IRQ problem, irq_level=%d, giving up\n", irq_level);
519                         goto err_release;
520                 }
521         }
522 #endif
523
524         DEB(printk("NCR53c406a: using port_base 0x%x\n", port_base));
525
526         present = 1;
527         tpnt->proc_name = "NCR53c406a";
528
529         shpnt = scsi_register(tpnt, 0);
530         if (!shpnt) {
531                 printk("NCR53c406a: Unable to register host, giving up.\n");
532                 goto err_release;
533         }
534
535         if (irq_level > 0) {
536                 if (request_irq(irq_level, do_NCR53c406a_intr, 0, "NCR53c406a", shpnt)) {
537                         printk("NCR53c406a: unable to allocate IRQ %d\n", irq_level);
538                         goto err_free_scsi;
539                 }
540                 tpnt->can_queue = 1;
541                 DEB(printk("NCR53c406a: allocated IRQ %d\n", irq_level));
542         } else if (irq_level == 0) {
543                 tpnt->can_queue = 0;
544                 DEB(printk("NCR53c406a: No interrupts detected\n"));
545                 printk("NCR53c406a driver no longer supports polling interface\n");
546                 printk("Please email linux-scsi@vger.kernel.org\n");
547                         
548 #if USE_DMA
549                 printk("NCR53c406a: No interrupts found and DMA mode defined. Giving up.\n");
550 #endif                          /* USE_DMA */
551                 goto err_free_scsi;
552         } else {
553                 DEB(printk("NCR53c406a: Shouldn't get here!\n"));
554                 goto err_free_scsi;
555         }
556
557 #if USE_DMA
558         dma_chan = DMA_CHAN;
559         if (request_dma(dma_chan, "NCR53c406a") != 0) {
560                 printk("NCR53c406a: unable to allocate DMA channel %d\n", dma_chan);
561                 goto err_free_irq;
562         }
563
564         DEB(printk("Allocated DMA channel %d\n", dma_chan));
565 #endif                          /* USE_DMA */
566
567         shpnt->irq = irq_level;
568         shpnt->io_port = port_base;
569         shpnt->n_io_port = 0x10;
570 #if USE_DMA
571         shpnt->dma = dma_chan;
572 #endif
573
574 #if USE_DMA
575         sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, DMA channel %d.", port_base, irq_level, dma_chan);
576 #else
577         sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, %s PIO mode.", port_base, irq_level, fast_pio ? "fast" : "slow");
578 #endif
579
580         return (present);
581
582 #if USE_DMA
583       err_free_irq:
584         if (irq_level)
585                 free_irq(irq_level, shpnt);
586 #endif
587       err_free_scsi:
588         scsi_unregister(shpnt);
589       err_release:
590         release_region(port_base, 0x10);
591         return 0;
592 }
593
594 static int NCR53c406a_release(struct Scsi_Host *shost)
595 {
596         if (shost->irq)
597                 free_irq(shost->irq, NULL);
598 #ifdef USE_DMA
599         if (shost->dma_channel != 0xff)
600                 free_dma(shost->dma_channel);
601 #endif
602         if (shost->io_port && shost->n_io_port)
603                 release_region(shost->io_port, shost->n_io_port);
604
605         scsi_unregister(shost);
606         return 0;
607 }
608
609 /* called from init/main.c */
610 static void __init NCR53c406a_setup(char *str, int *ints)
611 {
612         static size_t setup_idx = 0;
613         size_t i;
614
615         DEB(printk("NCR53c406a: Setup called\n");
616             );
617
618         if (setup_idx >= PORT_COUNT - 1) {
619                 printk("NCR53c406a: Setup called too many times.  Bad LILO params?\n");
620                 return;
621         }
622         if (ints[0] < 1 || ints[0] > 3) {
623                 printk("NCR53c406a: Malformed command line\n");
624                 printk("NCR53c406a: Usage: ncr53c406a=<PORTBASE>[,<IRQ>[,<FASTPIO>]]\n");
625                 return;
626         }
627         for (i = 0; i < PORT_COUNT && !port_base; i++)
628                 if (ports[i] == ints[1]) {
629                         port_base = ints[1];
630                         DEB(printk("NCR53c406a: Specified port_base 0x%x\n", port_base);
631                             )
632                 }
633         if (!port_base) {
634                 printk("NCR53c406a: Invalid PORTBASE 0x%x specified\n", ints[1]);
635                 return;
636         }
637
638         if (ints[0] > 1) {
639                 if (ints[2] == 0) {
640                         irq_level = 0;
641                         DEB(printk("NCR53c406a: Specified irq %d\n", irq_level);
642                             )
643                 } else
644                         for (i = 0; i < INTR_COUNT && irq_level < 0; i++)
645                                 if (intrs[i] == ints[2]) {
646                                         irq_level = ints[2];
647                                         DEB(printk("NCR53c406a: Specified irq %d\n", port_base);
648                                             )
649                                 }
650                 if (irq_level < 0)
651                         printk("NCR53c406a: Invalid IRQ %d specified\n", ints[2]);
652         }
653
654         if (ints[0] > 2)
655                 fast_pio = ints[3];
656
657         DEB(printk("NCR53c406a: port_base=0x%x, irq=%d, fast_pio=%d\n", port_base, irq_level, fast_pio);)
658 }
659
660 __setup("ncr53c406a=", NCR53c406a_setup);
661
662 static const char *NCR53c406a_info(struct Scsi_Host *SChost)
663 {
664         DEB(printk("NCR53c406a_info called\n"));
665         return (info_msg);
666 }
667
668 static void wait_intr(void)
669 {
670         unsigned long i = jiffies + WATCHDOG;
671
672         while (time_after(i, jiffies) && !(inb(STAT_REG) & 0xe0)) {     /* wait for a pseudo-interrupt */
673                 cpu_relax();
674                 barrier();
675         }
676
677         if (time_before_eq(i, jiffies)) {       /* Timed out */
678                 rtrc(0);
679                 current_SC->result = DID_TIME_OUT << 16;
680                 current_SC->SCp.phase = idle;
681                 current_SC->scsi_done(current_SC);
682                 return;
683         }
684
685         NCR53c406a_intr(0, NULL, NULL);
686 }
687
688 static int NCR53c406a_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
689 {
690         int i;
691
692         VDEB(printk("NCR53c406a_queue called\n"));
693         DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n", SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->target, SCpnt->lun, SCpnt->request_bufflen));
694
695 #if 0
696         VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
697              printk("cmd[%d]=%02x  ", i, SCpnt->cmnd[i]));
698         VDEB(printk("\n"));
699 #endif
700
701         current_SC = SCpnt;
702         current_SC->scsi_done = done;
703         current_SC->SCp.phase = command_ph;
704         current_SC->SCp.Status = 0;
705         current_SC->SCp.Message = 0;
706
707         /* We are locked here already by the mid layer */
708         REG0;
709         outb(SCpnt->device->id, DEST_ID);       /* set destination */
710         outb(FLUSH_FIFO, CMD_REG);      /* reset the fifos */
711
712         for (i = 0; i < SCpnt->cmd_len; i++) {
713                 outb(SCpnt->cmnd[i], SCSI_FIFO);
714         }
715         outb(SELECT_NO_ATN, CMD_REG);
716
717         rtrc(1);
718         return 0;
719 }
720
721 static int NCR53c406a_abort(Scsi_Cmnd * SCpnt)
722 {
723         DEB(printk("NCR53c406a_abort called\n"));
724         return FAILED;          /* Don't know how to abort */
725 }
726
727 static int NCR53c406a_host_reset(Scsi_Cmnd * SCpnt)
728 {
729         DEB(printk("NCR53c406a_reset called\n"));
730         outb(C4_IMG, CONFIG4);  /* Select reg set 0 */
731         outb(CHIP_RESET, CMD_REG);
732         outb(SCSI_NOP, CMD_REG);        /* required after reset */
733         outb(SCSI_RESET, CMD_REG);
734         chip_init();
735
736         rtrc(2);
737         return SUCCESS;
738 }
739
740 static int NCR53c406a_device_reset(Scsi_Cmnd * SCpnt)
741 {
742         return FAILED;
743 }
744
745 static int NCR53c406a_bus_reset(Scsi_Cmnd * SCpnt)
746 {
747         return FAILED;
748 }
749
750 static int NCR53c406a_biosparm(struct scsi_device *disk,
751                                struct block_device *dev,
752                                sector_t capacity, int *info_array)
753 {
754         int size;
755
756         DEB(printk("NCR53c406a_biosparm called\n"));
757
758         size = capacity;
759         info_array[0] = 64;     /* heads */
760         info_array[1] = 32;     /* sectors */
761         info_array[2] = size >> 11;     /* cylinders */
762         if (info_array[2] > 1024) {     /* big disk */
763                 info_array[0] = 255;
764                 info_array[1] = 63;
765                 info_array[2] = size / (255 * 63);
766         }
767         return 0;
768 }
769
770 static irqreturn_t do_NCR53c406a_intr(int unused, void *dev_id,
771                                         struct pt_regs *regs)
772 {
773         unsigned long flags;
774         struct Scsi_Host *dev = dev_id;
775
776         spin_lock_irqsave(dev->host_lock, flags);
777         NCR53c406a_intr(0, dev_id, regs);
778         spin_unlock_irqrestore(dev->host_lock, flags);
779         return IRQ_HANDLED;
780 }
781
782 static void NCR53c406a_intr(int unused, void *dev_id, struct pt_regs *regs)
783 {
784         DEB(unsigned char fifo_size;
785             )
786             DEB(unsigned char seq_reg;
787             )
788         unsigned char status, int_reg;
789 #if USE_PIO
790         unsigned char pio_status;
791         struct scatterlist *sglist;
792         unsigned int sgcount;
793 #endif
794
795         VDEB(printk("NCR53c406a_intr called\n"));
796
797 #if USE_PIO
798         REG1;
799         pio_status = inb(PIO_STATUS);
800 #endif
801         REG0;
802         status = inb(STAT_REG);
803         DEB(seq_reg = inb(SEQ_REG));
804         int_reg = inb(INT_REG);
805         DEB(fifo_size = inb(FIFO_FLAGS) & 0x1f);
806
807 #if NCR53C406A_DEBUG
808         printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x", status, seq_reg, int_reg, fifo_size);
809 #if (USE_DMA)
810         printk("\n");
811 #else
812         printk(", pio=%02x\n", pio_status);
813 #endif                          /* USE_DMA */
814 #endif                          /* NCR53C406A_DEBUG */
815
816         if (int_reg & 0x80) {   /* SCSI reset intr */
817                 rtrc(3);
818                 DEB(printk("NCR53c406a: reset intr received\n"));
819                 current_SC->SCp.phase = idle;
820                 current_SC->result = DID_RESET << 16;
821                 current_SC->scsi_done(current_SC);
822                 return;
823         }
824 #if USE_PIO
825         if (pio_status & 0x80) {
826                 printk("NCR53C406A: Warning: PIO error!\n");
827                 current_SC->SCp.phase = idle;
828                 current_SC->result = DID_ERROR << 16;
829                 current_SC->scsi_done(current_SC);
830                 return;
831         }
832 #endif                          /* USE_PIO */
833
834         if (status & 0x20) {    /* Parity error */
835                 printk("NCR53c406a: Warning: parity error!\n");
836                 current_SC->SCp.phase = idle;
837                 current_SC->result = DID_PARITY << 16;
838                 current_SC->scsi_done(current_SC);
839                 return;
840         }
841
842         if (status & 0x40) {    /* Gross error */
843                 printk("NCR53c406a: Warning: gross error!\n");
844                 current_SC->SCp.phase = idle;
845                 current_SC->result = DID_ERROR << 16;
846                 current_SC->scsi_done(current_SC);
847                 return;
848         }
849
850         if (int_reg & 0x20) {   /* Disconnect */
851                 DEB(printk("NCR53c406a: disconnect intr received\n"));
852                 if (current_SC->SCp.phase != message_in) {      /* Unexpected disconnect */
853                         current_SC->result = DID_NO_CONNECT << 16;
854                 } else {        /* Command complete, return status and message */
855                         current_SC->result = (current_SC->SCp.Status & 0xff)
856                             | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
857                 }
858
859                 rtrc(0);
860                 current_SC->SCp.phase = idle;
861                 current_SC->scsi_done(current_SC);
862                 return;
863         }
864
865         switch (status & 0x07) {        /* scsi phase */
866         case 0x00:              /* DATA-OUT */
867                 if (int_reg & 0x10) {   /* Target requesting info transfer */
868                         rtrc(5);
869                         current_SC->SCp.phase = data_out;
870                         VDEB(printk("NCR53c406a: Data-Out phase\n"));
871                         outb(FLUSH_FIFO, CMD_REG);
872                         LOAD_DMA_COUNT(current_SC->request_bufflen);    /* Max transfer size */
873 #if USE_DMA                     /* No s/g support for DMA */
874                         NCR53c406a_dma_write(current_SC->request_buffer, current_SC->request_bufflen);
875 #endif                          /* USE_DMA */
876                         outb(TRANSFER_INFO | DMA_OP, CMD_REG);
877 #if USE_PIO
878                         if (!current_SC->use_sg)        /* Don't use scatter-gather */
879                                 NCR53c406a_pio_write(current_SC->request_buffer, current_SC->request_bufflen);
880                         else {  /* use scatter-gather */
881                                 sgcount = current_SC->use_sg;
882                                 sglist = current_SC->request_buffer;
883                                 while (sgcount--) {
884                                         NCR53c406a_pio_write(page_address(sglist->page) + sglist->offset, sglist->length);
885                                         sglist++;
886                                 }
887                         }
888                         REG0;
889 #endif                          /* USE_PIO */
890                 }
891                 break;
892
893         case 0x01:              /* DATA-IN */
894                 if (int_reg & 0x10) {   /* Target requesting info transfer */
895                         rtrc(6);
896                         current_SC->SCp.phase = data_in;
897                         VDEB(printk("NCR53c406a: Data-In phase\n"));
898                         outb(FLUSH_FIFO, CMD_REG);
899                         LOAD_DMA_COUNT(current_SC->request_bufflen);    /* Max transfer size */
900 #if USE_DMA                     /* No s/g support for DMA */
901                         NCR53c406a_dma_read(current_SC->request_buffer, current_SC->request_bufflen);
902 #endif                          /* USE_DMA */
903                         outb(TRANSFER_INFO | DMA_OP, CMD_REG);
904 #if USE_PIO
905                         if (!current_SC->use_sg)        /* Don't use scatter-gather */
906                                 NCR53c406a_pio_read(current_SC->request_buffer, current_SC->request_bufflen);
907                         else {  /* Use scatter-gather */
908                                 sgcount = current_SC->use_sg;
909                                 sglist = current_SC->request_buffer;
910                                 while (sgcount--) {
911                                         NCR53c406a_pio_read(page_address(sglist->page) + sglist->offset, sglist->length);
912                                         sglist++;
913                                 }
914                         }
915                         REG0;
916 #endif                          /* USE_PIO */
917                 }
918                 break;
919
920         case 0x02:              /* COMMAND */
921                 current_SC->SCp.phase = command_ph;
922                 printk("NCR53c406a: Warning: Unknown interrupt occurred in command phase!\n");
923                 break;
924
925         case 0x03:              /* STATUS */
926                 rtrc(7);
927                 current_SC->SCp.phase = status_ph;
928                 VDEB(printk("NCR53c406a: Status phase\n"));
929                 outb(FLUSH_FIFO, CMD_REG);
930                 outb(INIT_CMD_COMPLETE, CMD_REG);
931                 break;
932
933         case 0x04:              /* Reserved */
934         case 0x05:              /* Reserved */
935                 printk("NCR53c406a: WARNING: Reserved phase!!!\n");
936                 break;
937
938         case 0x06:              /* MESSAGE-OUT */
939                 DEB(printk("NCR53c406a: Message-Out phase\n"));
940                 current_SC->SCp.phase = message_out;
941                 outb(SET_ATN, CMD_REG); /* Reject the message */
942                 outb(MSG_ACCEPT, CMD_REG);
943                 break;
944
945         case 0x07:              /* MESSAGE-IN */
946                 rtrc(4);
947                 VDEB(printk("NCR53c406a: Message-In phase\n"));
948                 current_SC->SCp.phase = message_in;
949
950                 current_SC->SCp.Status = inb(SCSI_FIFO);
951                 current_SC->SCp.Message = inb(SCSI_FIFO);
952
953                 VDEB(printk("SCSI FIFO size=%d\n", inb(FIFO_FLAGS) & 0x1f));
954                 DEB(printk("Status = %02x  Message = %02x\n", current_SC->SCp.Status, current_SC->SCp.Message));
955
956                 if (current_SC->SCp.Message == SAVE_POINTERS || current_SC->SCp.Message == DISCONNECT) {
957                         outb(SET_ATN, CMD_REG); /* Reject message */
958                         DEB(printk("Discarding SAVE_POINTERS message\n"));
959                 }
960                 outb(MSG_ACCEPT, CMD_REG);
961                 break;
962         }
963 }
964
965 #ifndef IRQ_LEV
966 static int irq_probe(void)
967 {
968         int irqs, irq;
969         unsigned long i;
970
971         inb(INT_REG);           /* clear the interrupt register */
972         irqs = probe_irq_on();
973
974         /* Invalid command will cause an interrupt */
975         REG0;
976         outb(0xff, CMD_REG);
977
978         /* Wait for the interrupt to occur */
979         i = jiffies + WATCHDOG;
980         while (time_after(i, jiffies) && !(inb(STAT_REG) & 0x80))
981                 barrier();
982         if (time_before_eq(i, jiffies)) {       /* Timed out, must be hardware trouble */
983                 probe_irq_off(irqs);
984                 return -1;
985         }
986
987         irq = probe_irq_off(irqs);
988
989         /* Kick the chip */
990         outb(CHIP_RESET, CMD_REG);
991         outb(SCSI_NOP, CMD_REG);
992         chip_init();
993
994         return irq;
995 }
996 #endif                          /* IRQ_LEV */
997
998 static void chip_init(void)
999 {
1000         REG1;
1001 #if USE_DMA
1002         outb(0x00, PIO_STATUS);
1003 #else                           /* USE_PIO */
1004         outb(0x01, PIO_STATUS);
1005 #endif
1006         outb(0x00, PIO_FLAG);
1007
1008         outb(C4_IMG, CONFIG4);  /* REG0; */
1009         outb(C3_IMG, CONFIG3);
1010         outb(C2_IMG, CONFIG2);
1011         outb(C1_IMG, CONFIG1);
1012
1013         outb(0x05, CLKCONV);    /* clock conversion factor */
1014         outb(0x9C, SRTIMOUT);   /* Selection timeout */
1015         outb(0x05, SYNCPRD);    /* Synchronous transfer period */
1016         outb(SYNC_MODE, SYNCOFF);       /* synchronous mode */
1017 }
1018
1019 static void __init calc_port_addr(void)
1020 {
1021         /* Control Register Set 0 */
1022         TC_LSB = (port_base + 0x00);
1023         TC_MSB = (port_base + 0x01);
1024         SCSI_FIFO = (port_base + 0x02);
1025         CMD_REG = (port_base + 0x03);
1026         STAT_REG = (port_base + 0x04);
1027         DEST_ID = (port_base + 0x04);
1028         INT_REG = (port_base + 0x05);
1029         SRTIMOUT = (port_base + 0x05);
1030         SEQ_REG = (port_base + 0x06);
1031         SYNCPRD = (port_base + 0x06);
1032         FIFO_FLAGS = (port_base + 0x07);
1033         SYNCOFF = (port_base + 0x07);
1034         CONFIG1 = (port_base + 0x08);
1035         CLKCONV = (port_base + 0x09);
1036         /* TESTREG          = (port_base+0x0A); */
1037         CONFIG2 = (port_base + 0x0B);
1038         CONFIG3 = (port_base + 0x0C);
1039         CONFIG4 = (port_base + 0x0D);
1040         TC_HIGH = (port_base + 0x0E);
1041         /* FIFO_BOTTOM      = (port_base+0x0F); */
1042
1043         /* Control Register Set 1 */
1044         /* JUMPER_SENSE     = (port_base+0x00); */
1045         /* SRAM_PTR         = (port_base+0x01); */
1046         /* SRAM_DATA        = (port_base+0x02); */
1047         PIO_FIFO = (port_base + 0x04);
1048         /* PIO_FIFO1        = (port_base+0x05); */
1049         /* PIO_FIFO2        = (port_base+0x06); */
1050         /* PIO_FIFO3        = (port_base+0x07); */
1051         PIO_STATUS = (port_base + 0x08);
1052         /* ATA_CMD          = (port_base+0x09); */
1053         /* ATA_ERR          = (port_base+0x0A); */
1054         PIO_FLAG = (port_base + 0x0B);
1055         CONFIG5 = (port_base + 0x0D);
1056         /* SIGNATURE        = (port_base+0x0E); */
1057         /* CONFIG6          = (port_base+0x0F); */
1058 }
1059
1060 MODULE_LICENSE("GPL");
1061
1062 /* NOTE:  scatter-gather support only works in PIO mode.
1063  * Use SG_NONE if DMA mode is enabled!
1064  */
1065
1066 static Scsi_Host_Template driver_template = 
1067 {
1068      .proc_name                 = "NCR53c406a"          /* proc_name */,        
1069      .name                      = "NCR53c406a"          /* name */,             
1070      .detect                    = NCR53c406a_detect     /* detect */,           
1071      .release                   = NCR53c406a_release,
1072      .info                      = NCR53c406a_info               /* info */,             
1073      .queuecommand              = NCR53c406a_queue      /* queuecommand */,     
1074      .eh_abort_handler          = NCR53c406a_abort      /* abort */,            
1075      .eh_bus_reset_handler      = NCR53c406a_bus_reset  /* reset */,            
1076      .eh_device_reset_handler   = NCR53c406a_device_reset       /* reset */,            
1077      .eh_host_reset_handler     = NCR53c406a_host_reset /* reset */,            
1078      .bios_param                = NCR53c406a_biosparm   /* biosparm */,         
1079      .can_queue                 = 1                     /* can_queue */,        
1080      .this_id                   = 7                     /* SCSI ID of the chip */,
1081      .sg_tablesize              = 32                    /*SG_ALL*/ /*SG_NONE*/, 
1082      .cmd_per_lun               = 1                     /* commands per lun */, 
1083      .unchecked_isa_dma         = 1                     /* unchecked_isa_dma */,
1084      .use_clustering            = ENABLE_CLUSTERING                               
1085 };
1086
1087 #include "scsi_module.c"
1088
1089 /*
1090  * Overrides for Emacs so that we get a uniform tabbing style.
1091  * Emacs will notice this stuff at the end of the file and automatically
1092  * adjust the settings for this buffer only.  This must remain at the end
1093  * of the file.
1094  * ---------------------------------------------------------------------------
1095  * Local variables:
1096  * c-indent-level: 4
1097  * c-brace-imaginary-offset: 0
1098  * c-brace-offset: -4
1099  * c-argdecl-indent: 4
1100  * c-label-offset: -4
1101  * c-continued-statement-offset: 4
1102  * c-continued-brace-offset: 0
1103  * indent-tabs-mode: nil
1104  * tab-width: 8
1105  * End:
1106  */