NCR53c406a.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:31k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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