os/linux-2.6-tag--devboard-R2_10-4/arch/cris/arch-v32/drivers/elphel/fpgajtag.c

Go to the documentation of this file.
00001 /*!***************************************************************************
00002 *! FILE NAME  : fpgajtag353.c
00003 *! DESCRIPTION: TBD
00004 *! Copyright 2002-2007 (C) Elphel, Inc.
00005 *! -----------------------------------------------------------------------------**
00006 *!
00007 *!  This program is free software: you can redistribute it and/or modify
00008 *!  it under the terms of the GNU General Public License as published by
00009 *!  the Free Software Foundation, either version 3 of the License, or
00010 *!  (at your option) any later version.
00011 *!
00012 *!  This program is distributed in the hope that it will be useful,
00013 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 *!  GNU General Public License for more details.
00016 *!
00017 *!  You should have received a copy of the GNU General Public License
00018 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 *! -----------------------------------------------------------------------------**
00020 *!  $Log: fpgajtag353.c,v $
00021 *!  Revision 1.2  2008/04/11 23:16:51  elphel
00022 *!  removed unneeded local_irq_disable() after local_irq_save_flags()
00023 *!
00024 *!  Revision 1.1.1.1  2007/08/17 10:23:18  elphel
00025 *!  This is a fresh tree based on elphel353-2.10
00026 *!
00027 *!  Revision 1.7  2007/08/17 10:23:18  spectr_rain
00028 *!  switch to GPL3 license
00029 *!
00030 *!  Revision 1.6  2007/07/20 10:17:46  spectr_rain
00031 *!  *** empty log message ***
00032 *!
00033 *!  Revision 1.5  2007/06/28 02:20:39  elphel
00034 *!  Slowed down sensor FPGA programming while working with long cables. Problem was different, so maybe that change may be undone.
00035 *!
00036 *!  Revision 1.4  2007/05/21 21:23:50  elphel
00037 *!  remove compile-time warning
00038 *!
00039 *!  Revision 1.3  2007/05/21 17:45:11  elphel
00040 *!  boundary scan support, added 359/347 detection
00041 *!
00042 *!  Revision 1.2  2007/03/25 10:14:23  elphel
00043 *!  Accommodating 10359 board
00044 *!
00045 *!  Revision 1.1.1.1  2007/02/23 10:11:48  elphel
00046 *!  initial import into CVS
00047 *!
00048 *!  Revision 1.2  2005/05/10 21:08:49  elphel
00049 *!  *** empty log message ***
00050 *!
00051 
00052 TODO: replace static buffer (what a waste!)
00053 I suspect "somebody" is is playing with portA during JTAG configuration.
00054 To test that I'll use 256K static buffer, copy all the bitstream there,
00055 disable interrupts and do the programming.
00056 No debug with printk ... 
00057 */
00058 
00059 /****************** INCLUDE FILES SECTION ***********************************/
00060 #include <linux/autoconf.h>
00061 
00062 #include <linux/module.h>
00063 #include <linux/sched.h>
00064 #include <linux/slab.h>
00065    #include <linux/ioport.h> // needed?
00066 #include <linux/errno.h>
00067 #include <linux/kernel.h>
00068 #include <linux/fs.h>
00069 #include <linux/string.h>
00070 //#include <linux/poll.h>
00071 #include <linux/init.h>
00072 
00073 //#include <linux/interrupt.h>
00074 //#include <linux/spinlock.h>
00075 
00076 
00077 //#include <asm/svinto.h> - obsolete
00078 
00079 // #include <asm/etraxgpio.h> // probably just for gpio driver
00080 #include <asm/arch/hwregs/reg_map.h>
00081 #include <asm/arch/hwregs/reg_rdwr.h>
00082 #include <asm/arch/hwregs/gio_defs.h>
00083 #include <asm/arch/hwregs/intr_vect_defs.h>
00084 #include <asm/arch/hwregs/pinmux_defs.h>
00085 
00086 
00087 #include <asm/io.h>
00088 #include <asm/system.h>
00089 #include <asm/irq.h>
00090 
00091 #include <asm/elphel/fpgactrl.h> //defines for fpga_state fields
00092 
00093 
00094 #include <asm/delay.h>
00095 #include <asm/uaccess.h>
00096 
00097 #include "fpgactrl.h" // extern fpga_state, defines port_csp0_addr, port_csp4_addr
00098 #include "x3x3.h"     // FPGA registers and macros
00099 
00100 
00101 //#include <asm/fpgaconfa.h>
00102 
00103 
00104 //#define JTAG_DISABLE_IRQ y
00105 
00106 #define D(x)
00107 //#define D(x) printk("%s:%d:",__FILE__,__LINE__);x
00108 
00109 /*
00110 port C 353:
00111  0 - TDO  (in)
00112  1 - TDI  (out)
00113  2 - TMS  (out)
00114  3 - TCK  (out)
00115  4 - NC (was INIT (i/o) )
00116  5 - DONE (in)
00117  6 - RSTBTN
00118  7 - PGM  (out)
00119 */
00120 #define FPGAJTAG_TDO_BIT 0
00121 #define FPGAJTAG_TDI_BIT 1
00122 #define FPGAJTAG_TMS_BIT 2
00123 #define FPGAJTAG_TCK_BIT 3
00124 #define FPGAJTAG_DONE_BIT 5
00125 #define FPGAJTAG_RSTBTN_BIT 6
00126 #define FPGAJTAG_PGM_BIT 7
00127 
00128 
00129 #ifndef XC2S300E_BITSIZE
00130   #define XC3S1000_BITSIZE  3223488
00131   #define XC3S1200E_BITSIZE 3841189
00132   #define XC3S1200E_BOUNDARY_SIZE 772
00133 //  #define XC3S1200E_BOUNDARY_SIZE 812
00134   #define FJTAG_BUF_SIZE 0x77000
00135   #define FJTAG_MAX_HEAD 0x1000
00136   #define FJTAG_RAW_WSIZE 0x40000 // shared with  bitstream buffer
00137   #define FJTAG_RAW_RSIZE 0x30000 // shared with  bitstream buffer
00138   #define FJTAG_IDSIZE    0x40    // bits - ID and User
00139 
00140 #endif
00141 
00142 //=========================
00143 #define FPGA_JTAG_MAJOR 132  /* LOCAL/EXPERIMENTAL */
00144 #define FPGA_JTAG_DRIVER_NAME "Elphel (R) model 353 FPGA (Xilinx (R) XC3S1200E) configuration driver"
00145 #define FPGA_JTAG_MAXMINOR 10
00146 #define FPGA_JTAG_RESET_MINOR   0  // just close open files
00147 #define FPGA_JTAG_RAW_MINOR 0  // just close open files
00148 #define FPGA_JTAG_MINOR     1
00149 #define FPGA_SJTAG_MINOR    2
00150 #define FPGA_AJTAG_MINOR    3
00151 
00152 #define FPGA_JTAG_BOUNDARY_MINOR    5  // read/write boundary pins of the main FPGA
00153 #define FPGA_SJTAG_BOUNDARY_MINOR   6  // read/write boundary pins of the sensor board FPGA
00154 #define FPGA_AJTAG_BOUNDARY_MINOR   7  // read/write boundary pins of the aux board FPGA
00155 
00156 #define JTAG_RAW         0 // raw JTAG access to any FPGA
00157 #define JTAG_MAIN_FPGA   1 // main board FPGA access  (10353)
00158 #define JTAG_SENSOR_FPGA 2 // sensor board FPGA access (10347, 10359) 
00159 #define JTAG_AUX_FPGA    3 // 
00160 #define JTAG_NCHANNELS 4
00161 
00162 #define JTAG_MODE_CLOSED   0 // JTAG channel is closed
00163 #define JTAG_MODE_RDID     1 // JTAG channel read ID
00164 #define JTAG_MODE_PGM      2 // JTAG channel PROGRAM
00165 #define JTAG_MODE_BOUNDARY 3 // JTAG channel boundary scan (just opened, will become one of the 2:JTAG_MODE_SAMPLE, JTAG_MODE_EXTEST
00166 #define JTAG_MODE_SAMPLE   4 // JTAG channel boundary scan - sample (the first operation after open is read)
00167 #define JTAG_MODE_EXTEST   5 // JTAG channel boundary scan - EXTEST (the first operation after open is read)
00168 #define JTAG_MODE_RAW      6 // JTAG raw command mode
00169 
00170 // configuration and raw minors use whole buffer, ID and boundary can be opened at the same time
00171 struct JTAG_channel_t {
00172   int mode; // 0..5 -JTAG_MODE_CLOSED...JTAG_MODE_EXTEST
00173   unsigned char * dbuf; // data buffer (shared, boundary mode use different parts)
00174   int sizew;            // byte size that can be written
00175   int sizer;            // byte size that can be read
00176   int bitsw;            // bit size to be written
00177   int bitsr;            // bit size to be read
00178   int wp;               // byte pointer for file write
00179   int rp;               // byte pointer for file read
00180   int wdirty;           // some data is buffered but not yet sent out in EXTEST mode
00181 };
00182 static unsigned char  bitstream_data[FJTAG_BUF_SIZE];    // will fit bitstream and the header (if any). Also used for boundary write
00183 //static unsigned short *raw_fifo_w= (unsigned short) &bitstream_data[0];
00184 static unsigned char *raw_fifo_w=  &bitstream_data[0];
00185 static unsigned char *raw_fifo_r=  &bitstream_data[FJTAG_RAW_WSIZE];
00186 static struct  JTAG_channel_t JTAG_channels[JTAG_NCHANNELS];
00187 // boundary scan is read always at open. written - at close (only if there were any writes)
00188 static int data_modified=0;
00189 
00190 static reg_gio_rw_pc_dout  pc_dout;
00191 #define PC_DOUT_INITIAL 0
00192 //static        int buf8i=0;    // current buffer length (in bytes!)
00193 //static  int datastart=0;
00194 //static int prev32;
00195 //static int prev64;
00196 
00197 //static int fpga_jtag_state=0; 
00198 
00199 // inteface functions
00200 static const char fpga_jtag_name[] = "fpga_jtag_loader";
00201 static int minors[FPGA_JTAG_MAXMINOR+1];        // each minor can be opened only once
00202 //static int thisminor;
00203 static int     fpga_jtag_open   (struct inode *inode, struct file *filp);
00204 static int     fpga_jtag_release(struct inode *inode, struct file *filp);
00205 static ssize_t fpga_jtag_write  (struct file * file, const char * buf, size_t count, loff_t *off);
00206 static loff_t  fpga_jtag_lseek  (struct file * file, loff_t offset, int orig);
00207 static ssize_t fpga_jtag_read   (struct file * file, char * buf, size_t count, loff_t *off);
00208 static int __init fpga_jtag_init(void);
00209 
00210 static struct file_operations fpga_jtag_fops = {
00211         owner:    THIS_MODULE,
00212         open:     fpga_jtag_open,
00213         release:  fpga_jtag_release,
00214         llseek:   fpga_jtag_lseek,
00215         read:     fpga_jtag_read,
00216         write:    fpga_jtag_write
00217 };
00218 
00219 
00220 
00221 // internal functions
00222 loff_t fjtag_bitsize (int minor);
00223 loff_t fjtag_bytesize (int minor);
00224 int JTAG_channel(int minor);
00225 void initPortC(void);
00226 void set_pgm_mode (int chn, int en);
00227 void set_pgm (int chn, int pgmon);
00228 int read_done (int chn);
00229 int jtag_send (int chn, int tms, int len, int d);
00230 int jtag_write_bits (int chn,
00231                      unsigned char *buf, // data to write
00232                      int len,            // number of bytes to write
00233                      int check,          // compare readback data with previously written, abort on mismatch
00234                      int last,           // output last bit with TMS=1
00235                      int prev[2]);       // if null - don't use
00236 int JTAG_configure (int chn, unsigned char * buf, int len);
00237 int JTAG_readID (int chn, unsigned char * buf);
00238 int JTAG_openChannel (int chn);
00239 int JTAG_resetChannel (int chn);
00240 int JTAG_CAPTURE (int chn, unsigned char * buf, int len);
00241 int JTAG_EXTEST (int chn, unsigned char * buf, int len);
00242 
00243 void JTAG_push_raw (int b);
00244 int JTAG_process_raw(void);
00245 
00246 
00247 int JTAG_channel(int minor) {
00248   switch (minor) {
00249     case FPGA_JTAG_RESET_MINOR : // same as RAW
00250         return JTAG_RAW;
00251     case FPGA_JTAG_MINOR:
00252     case FPGA_JTAG_BOUNDARY_MINOR:
00253         return JTAG_MAIN_FPGA;
00254     case FPGA_SJTAG_MINOR:
00255     case FPGA_SJTAG_BOUNDARY_MINOR:
00256         return JTAG_SENSOR_FPGA;
00257     case FPGA_AJTAG_MINOR:
00258     case FPGA_AJTAG_BOUNDARY_MINOR:
00259         return JTAG_AUX_FPGA;
00260   }
00261   return 0;
00262 }
00263 static int raw_fifo_w_wp;
00264 static int raw_fifo_w_rp;
00265 static int raw_fifo_r_wp;
00266 static int raw_fifo_r_rp;
00267 static int raw_chn;
00268 
00269 /*
00270  * send raw JTAG commands. Each command consists of 2 bytes
00271  * byte 0 - data to send through TDI, lsb aligned (if less than 8 bits - hign bits are not used
00272  * byte 1 - 0001TNNN - send NNN?NNN:8 bits of byte 0 through TDI, keeping TDS at value of T (reads back TDO - before TCL)
00273  *        - 00100000 - select JTAG channel from byte 0 (reads back channel)
00274  *        - 00100010 - de-activate JTAG access         (reads back 0)
00275  *        - 00100011 - activate JTAG access            (reads back 0)
00276  *        - 00100100 - PGM off                         (reads back ready in bit 0)
00277  *        - 00100101 - PGM on                          (reads back 0xf0)
00278  *        - 1TTTTTTT - delay usec byte0+ ((byte1 &0x7f) << 8)  (reads back 80)
00279  *        - 0??????? (other) - nop                     (reads back 0xff)
00280  * if no channel is selected, no output is generated
00281 void set_pgm_mode (int chn, int en);
00282 void set_pgm (int chn, int pgmon);
00283  */
00284 #define JTAG_RAW_SEND   0x10
00285 #define JTAG_RAW_SETCHN 0x20
00286 #define JTAG_RAW_DEACT  0x22
00287 #define JTAG_RAW_ACT    0x23
00288 #define JTAG_RAW_PGMOFF 0x24
00289 #define JTAG_RAW_PGMON  0x25
00290 #define JTAG_RAW_WAIT   0x80
00291 
00292 void JTAG_push_raw (int b) {
00293   raw_fifo_r[raw_fifo_r_wp++]=b;
00294   if (raw_fifo_r_wp > FJTAG_RAW_RSIZE) raw_fifo_r_wp-=FJTAG_RAW_RSIZE;
00295 }
00296 int JTAG_process_raw(void) {
00297   unsigned char b0, b1;
00298   while (raw_fifo_w_rp != (raw_fifo_w_wp & ~1)) {
00299     b0=raw_fifo_w[raw_fifo_w_rp++];
00300     b1=raw_fifo_w[raw_fifo_w_rp++];
00301     if (raw_fifo_w_rp > FJTAG_RAW_WSIZE) raw_fifo_w_rp-=FJTAG_RAW_WSIZE;
00302     if        (b1 == JTAG_RAW_SETCHN) { // set channel number
00303       raw_chn = b0;
00304       if (raw_chn>=JTAG_NCHANNELS) raw_chn=0; //illegal channel
00305       JTAG_push_raw (raw_chn);
00306     } else if (raw_chn) { // ignore commands until the JTAG channel number is specified
00307       if        ((b1 & 0xf0) == JTAG_RAW_SEND) { // send JTAG data
00308         JTAG_push_raw (jtag_send(raw_chn, (b1 >> 3) & 1, b1 & 7, (int) b0 ));
00309       } else if ((b1 & 0x80) == JTAG_RAW_WAIT) { // delay
00310         udelay(((b1 & 0x7f) <<8) + b0);
00311         JTAG_push_raw (0x80);
00312       } else switch (b1) {
00313         case JTAG_RAW_DEACT:
00314            set_pgm_mode (raw_chn, 0);
00315            JTAG_push_raw (0x0);
00316            break;
00317         case JTAG_RAW_ACT:
00318            set_pgm_mode (raw_chn, 1);
00319            JTAG_push_raw (0x0);
00320            break;
00321         case JTAG_RAW_PGMOFF:
00322            set_pgm (raw_chn, 0);
00323            JTAG_push_raw (read_done(raw_chn));
00324         case JTAG_RAW_PGMON:
00325            set_pgm (raw_chn, 1);
00326            JTAG_push_raw (0xf0);
00327            break;
00328         default:
00329            JTAG_push_raw (0xff);
00330         }
00331     } else { // make output always be 1 byte for 2 bytes input
00332        JTAG_push_raw (0xf0);
00333     } // end of if (raw_chn) /else
00334   } // while (raw_fifo_w_rp != (raw_fifo_w_wp & ~1)) 
00335   return 0; // will think of return value later
00336 }
00337 
00338 //returns 0 if all channels closed
00339 // 
00340 int JTAG_whatopen(void) {
00341   int i,r=0;
00342   for (i=0;i<JTAG_NCHANNELS;i++) r |= 1 << JTAG_channels[i].mode;
00343   return r;
00344 }
00345 
00346 //++++++++++++++++++++++++++++++++++++ open() ++++++++++++++++++++++++++++++++++++++++++++++++++++++
00347 
00348 static int fpga_jtag_open(struct inode *inode, struct file *filp) {
00349    int i;
00350 //   int res;
00351    int p = MINOR(inode->i_rdev);
00352    int chn= JTAG_channel(p);
00353    reg_intr_vect_rw_mask intr_mask;
00354    D(printk("fpga_open: minor=%x, channel=%x, buf=%p\r\n",p,chn,bitstream_data ));
00355    switch ( p ) {
00356      case FPGA_JTAG_RESET_MINOR : // same as RAW
00357        for (i=1; i<JTAG_NCHANNELS; i++) JTAG_channels[i].mode=JTAG_MODE_CLOSED;
00358        JTAG_channels[chn].mode=JTAG_MODE_RAW;
00359        JTAG_channels[chn].sizew =  FJTAG_RAW_WSIZE;
00360        JTAG_channels[chn].sizer =  FJTAG_RAW_RSIZE;
00361        JTAG_channels[chn].wp =  0;
00362        JTAG_channels[chn].rp =  0; // will read IDs if actually read
00363        raw_fifo_w_wp=0; 
00364        raw_fifo_w_rp=0;
00365        raw_fifo_r_wp=0;
00366        raw_fifo_r_rp=0;
00367        raw_chn=0;
00368        break;
00369      case FPGA_JTAG_MINOR :
00370        if ( JTAG_whatopen() & 0x7e) return -EACCES; // none of the channels could be open when opening this file
00371 //       JTAG_channels[chn].mode  =  JTAG_MODE_PGM;
00372 //       JTAG_channels[chn].dbuf  = &bitstream_data[0];
00373 //       JTAG_channels[chn].sizew =  FJTAG_BUF_SIZE;
00374 //       JTAG_channels[chn].sizer =  FJTAG_IDSIZE >> 3;
00375 //       JTAG_channels[chn].wp =  0;
00376 //       JTAG_channels[chn].rp =  0; // will read IDs if actually read
00377        fpga_state &= ~FPGA_STATE_LOADED;     // is it still used?
00378        fpga_state &= ~FPGA_STATE_SDRAM_INIT; // not needed
00379 // disable camera interrupts here (while reprogramming FPGA could generate stray interrupts;
00380  /* Disable external interrupts.. */
00381        intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
00382        intr_mask.ext = 0;
00383        REG_WR(intr_vect, regi_irq, rw_mask, intr_mask); 
00384 //     printk ("Camera interrupts disabled\r\n");
00385 //       break;
00386 // fall through
00387      case FPGA_SJTAG_MINOR :
00388      case FPGA_AJTAG_MINOR :
00389        if ( JTAG_whatopen() & 0x7e) return -EACCES; // none of the channels could be open when opening this file
00390        JTAG_channels[chn].mode  =  JTAG_MODE_PGM;
00391        JTAG_channels[chn].dbuf  = &bitstream_data[0];
00392        JTAG_channels[chn].sizew =  FJTAG_BUF_SIZE;
00393        JTAG_channels[chn].sizer =  FJTAG_IDSIZE >> 3;
00394        JTAG_channels[chn].wp =  0;
00395        JTAG_channels[chn].rp =  0; // will read IDs if actually read
00396        JTAG_channels[chn].bitsw = XC3S1200E_BITSIZE;  // bit size to be written
00397        JTAG_channels[chn].bitsr=  FJTAG_IDSIZE;
00398        JTAG_openChannel (chn); // configure channel access, reset JTAG and to RUN-TEST/IDLE state
00399        break;
00400      case FPGA_JTAG_BOUNDARY_MINOR :
00401      case FPGA_SJTAG_BOUNDARY_MINOR :
00402      case FPGA_AJTAG_BOUNDARY_MINOR :
00403        if ( JTAG_whatopen() & 0x46)  return -EACCES; // none of the channels could be open for program/id/raw when opening this file
00404        if ( JTAG_channels[chn].mode != JTAG_MODE_CLOSED) return -EACCES; // already open
00405        JTAG_channels[chn].mode  =  JTAG_MODE_BOUNDARY;
00406        JTAG_channels[chn].sizew =  (XC3S1200E_BOUNDARY_SIZE+7) >> 3;
00407        JTAG_channels[chn].sizer =  (XC3S1200E_BOUNDARY_SIZE+7) >> 3;
00408        JTAG_channels[chn].dbuf  = &bitstream_data[JTAG_channels[chn].sizew * chn];
00409        JTAG_channels[chn].wp =  0;
00410        JTAG_channels[chn].rp =  0; // will read IDs if actually read
00411        JTAG_channels[chn].bitsw = XC3S1200E_BOUNDARY_SIZE;  // bit size to be written
00412        JTAG_channels[chn].bitsr=  XC3S1200E_BOUNDARY_SIZE;
00413        JTAG_openChannel (chn); // configure channel access, reset JTAG and to RUN-TEST/IDLE state
00414        break;
00415      default: return -EINVAL;
00416    }
00417    D(printk("fpga_open: chn=%x, JTAG_channels[chn].sizew=%x, JTAG_channels[chn].sizer=%x\r\n", chn,JTAG_channels[chn].sizew, JTAG_channels[chn].sizer) );
00418    D(printk("fpga_open: chn=%x, JTAG_channels[chn].bitsw=%x, JTAG_channels[chn].bitsr=%x\r\n", chn,JTAG_channels[chn].bitsw, JTAG_channels[chn].bitsr) );
00419    JTAG_channels[chn].wdirty=0;
00420 //   inode->i_size=fjtag_bytesize(p);
00421    inode->i_size=JTAG_channels[chn].sizer;
00422    minors[p]=p;
00423    filp->private_data = &minors[p];
00424    D(printk("fpga_open: inode->i_size=%x, chn=%x\r\n", (int) inode->i_size, chn) );
00425    return 0;
00426 }
00427 
00428 //++++++++++++++++++++++++++++++++++++ release() ++++++++++++++++++++++++++++++++++++++++++++++++++++++
00429 
00430 static int fpga_jtag_release(struct inode *inode, struct file *filp) {
00431    int res=0;
00432    int p = MINOR(inode->i_rdev);
00433    int chn= JTAG_channel(p);
00434 //   reg_intr_vect_rw_mask intr_mask;
00435   D(printk("fpga_jtag_release: p=%x,chn=%x,  wp=0x%x, rp=0x%x\r\n",p,chn, JTAG_channels[chn].wp, JTAG_channels[chn].rp));
00436    switch ( p ) {
00437      case FPGA_JTAG_RESET_MINOR : // same as RAW - do nothing, raw code should do it on it's own
00438        break;
00439      case FPGA_JTAG_MINOR :
00440      case FPGA_SJTAG_MINOR :
00441      case FPGA_AJTAG_MINOR :
00442        if (JTAG_channels[chn].wp > 0) { // anything written?
00443          res=JTAG_configure (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].wp);
00444          if ((res >=0) & (chn == JTAG_MAIN_FPGA)) fpga_state|=1; // Add 16-bit revision number here? (from file header)
00445        }
00446 // reset initializing in any case:
00447        if (chn == JTAG_MAIN_FPGA) fpga_state &=~FPGA_STATE_INITIALIZED;
00448        JTAG_resetChannel (chn);
00449 
00450        break;
00451      case FPGA_JTAG_BOUNDARY_MINOR :
00452      case FPGA_SJTAG_BOUNDARY_MINOR :
00453      case FPGA_AJTAG_BOUNDARY_MINOR :
00454 // "dirty"? Send to JTAG
00455        if (JTAG_channels[chn].wp >0) JTAG_EXTEST (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].bitsw); // size in bits
00456        JTAG_resetChannel (chn);
00457        break;
00458     default: return -EINVAL;
00459    }
00460    minors[p]=0;
00461    JTAG_channels[chn].mode=JTAG_MODE_CLOSED;
00462    D(printk("fpga_jtag_release:  done\r\n"));
00463    return (res<0)?res:0;
00464 }
00465 
00466 
00467 //++++++++++++++++++++++++++++++++++++ write() ++++++++++++++++++++++++++++++++++++++++++++++++++++++
00468 
00469 //for boundary scan: writing before wp+1 will start EXTEST cycle (either rollover or lseek)
00470 static ssize_t fpga_jtag_write(struct file * file, const char * buf, size_t count, loff_t *off) {
00471    int p = ((int *)file->private_data)[0];
00472    int chn= JTAG_channel(p);
00473    size_t size = JTAG_channels[chn].sizew;
00474   D(printk("fpga_jtag_write: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, wp=%lx,size=0x%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].wp, (int) size));
00475 
00476    switch (p) {
00477      case FPGA_JTAG_RESET_MINOR : // same as RAW - do nothing, raw code should do it on it's own
00478        if (count > size) count= size;
00479        if ((raw_fifo_w_wp+count) > size) { // read tail, then roll over to the head to the total of count
00480          if (copy_from_user(&raw_fifo_w[raw_fifo_w_wp],buf,size-raw_fifo_w_wp)) return -EFAULT; // read tail
00481          if (copy_from_user(&raw_fifo_w[0],&buf[size-raw_fifo_w_wp],count+raw_fifo_w_wp-size)) return -EFAULT; // read head
00482        } else {
00483          if (copy_from_user(&raw_fifo_w[raw_fifo_w_wp],buf,count)) return -EFAULT; // read count
00484        }
00485        raw_fifo_w_wp+=count;
00486        if (raw_fifo_w_wp > size) raw_fifo_w_wp -= size;
00487        JTAG_process_raw(); // send all the received data to JTAG - will cause read fifo to get ~1/2 of the number of bytes written
00488        break;
00489      case FPGA_JTAG_MINOR  :
00490      case FPGA_SJTAG_MINOR :
00491      case FPGA_AJTAG_MINOR : // read configuration data to buffer
00492        if (*off > size) return -EFAULT;
00493        if ((*off + count) > size) count= (size - *off);
00494        if (copy_from_user(&(JTAG_channels[chn].dbuf[*off]),buf,count)) return -EFAULT;
00495        *off+=count;
00496        if (*off > JTAG_channels[chn].wp) JTAG_channels[chn].wp= *off;
00497        break;
00498      case FPGA_JTAG_BOUNDARY_MINOR :
00499      case FPGA_SJTAG_BOUNDARY_MINOR :
00500      case FPGA_AJTAG_BOUNDARY_MINOR :
00501        if (*off > size) return -EFAULT;
00502        if ((*off + count) > size) count= (size - *off);
00503 //       if ((*off < JTAG_channels[chn].wp) || (JTAG_channels[chn].mode!=JTAG_MODE_EXTEST)) {
00504        if (*off < JTAG_channels[chn].wp) {
00505          JTAG_EXTEST (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].bitsw); // writing "before" causes EXTEST to fill in boundary scan register
00506          JTAG_channels[chn].wdirty=0;
00507        }
00508        if (copy_from_user(&(JTAG_channels[chn].dbuf[*off]),buf,count)) return -EFAULT;
00509        *off+=count;
00510        JTAG_channels[chn].wp= *off; // before rolling over
00511        if (*off >= size) {
00512          *off=0; // roll over
00513        }
00514        JTAG_channels[chn].mode=JTAG_MODE_EXTEST; //should write the last byte before reading - or buffer data will be just lost
00515        JTAG_channels[chn].wdirty=1;
00516        break;
00517      default:               return -EINVAL;
00518    }
00519  D(printk("fpga_jtag_write end: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, wp=%lx,size=0x%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].wp, (int) size));
00520    return count;
00521 }
00522 
00523 //++++++++++++++++++++++++++++++++++++ read() ++++++++++++++++++++++++++++++++++++++++++++++++++++++
00524 
00525 ssize_t fpga_jtag_read(struct file * file, char * buf, size_t count, loff_t *off) {
00526    int p = ((int *)file->private_data)[0];
00527    int chn= JTAG_channel(p);
00528    size_t size = JTAG_channels[chn].sizer;
00529    int size_av;  // available data
00530  //  D(printk("fpga_jtag_read from 0x%x, count=0x%x, size=0x%x\n", (int) *off, (int) count, (int) size));
00531  D(printk("fpga_jtag_read: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, rp=%lx,size=0x%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].rp, (int) size));
00532    switch (p) {
00533      case FPGA_JTAG_RESET_MINOR : // same as RAW - do nothing, raw code should do it on it's own
00534        size_av=(raw_fifo_r_wp >= raw_fifo_r_rp)?(raw_fifo_r_wp - raw_fifo_r_rp):(size+raw_fifo_r_wp - raw_fifo_r_rp);
00535        if (count > size_av) count= size_av;
00536        if ((raw_fifo_r_rp+count) > size) { // read tail, then roll over to the head to the total of count
00537          if (copy_to_user(buf, &raw_fifo_r[raw_fifo_r_rp],size-raw_fifo_r_rp)) return -EFAULT; // read tail
00538          if (copy_to_user(&buf[size-raw_fifo_r_rp],&raw_fifo_r[0],count+raw_fifo_r_rp-size)) return -EFAULT; // read head
00539        } else {
00540          if (copy_to_user(buf,&raw_fifo_w[raw_fifo_w_wp],count)) return -EFAULT; // read count
00541        }
00542        raw_fifo_r_rp+=count;
00543        if (raw_fifo_r_rp > size) raw_fifo_r_rp -= size;
00544        break;
00545      case FPGA_JTAG_MINOR  :
00546      case FPGA_SJTAG_MINOR :
00547      case FPGA_AJTAG_MINOR : // read configuration data to buffer
00548        if ((JTAG_channels[chn].wp==0) && (JTAG_channels[chn].rp==0)) { // starting from read - get ID
00549          JTAG_channels[chn].mode=JTAG_MODE_RDID;
00550          JTAG_readID (chn, JTAG_channels[chn].dbuf);
00551        }
00552        if (*off > size) return -EFAULT;
00553        if ((*off + count) > size) count= (size - *off);
00554 D(printk("fpga_jtag_read_01: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, rp=%lx,size=0x%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].rp, (int) size)); // D(printk("count= 0x%x, pos= 0x%x\n", (int) count, (int)*off));
00555 
00556        if (copy_to_user(buf,&(JTAG_channels[chn].dbuf[*off]),count)) return -EFAULT;
00557        *off+=count;
00558        JTAG_channels[chn].rp= *off;
00559        break;
00560      case FPGA_JTAG_BOUNDARY_MINOR :
00561      case FPGA_SJTAG_BOUNDARY_MINOR :
00562      case FPGA_AJTAG_BOUNDARY_MINOR :
00563        if ((JTAG_channels[chn].mode==JTAG_MODE_EXTEST) && (JTAG_channels[chn].wdirty || (*off < JTAG_channels[chn].rp))) {
00564          JTAG_EXTEST (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].bitsr); // writing last byte causes EXTEST to fill in boundary scan register
00565          JTAG_channels[chn].wdirty=0;
00566        }
00567 // (re)-read capture pins if it was a roll-over or the first access after open
00568        if ((JTAG_channels[chn].mode!=JTAG_MODE_EXTEST) && ((*off < JTAG_channels[chn].rp) || (JTAG_channels[chn].mode==JTAG_MODE_BOUNDARY))) {
00569          JTAG_CAPTURE (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].bitsr);
00570        }
00571        if (*off > size) return -EFAULT;
00572        if ((*off + count) > size) count= (size - *off);
00573 D(printk("fpga_jtag_read_01: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, rp=%lx,size=0x%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].rp, (int) size)); // D(printk("count= 0x%x, pos= 0x%x\n", (int) count, (int)*off));
00574 //       if (*off < JTAG_channels[chn].rp) {
00575 //         JTAG_EXTEST (chn, JTAG_channels[chn].dbuf, JTAG_channels[chn].bitsw); // writing last byte causes EXTEST to fill in boundary scan register
00576 //         JTAG_channels[chn].wdirty=0;
00577 //       }
00578        if (copy_to_user(buf,&(JTAG_channels[chn].dbuf[*off]),count)) return -EFAULT;
00579        *off+=count;
00580        JTAG_channels[chn].rp= *off; // before rolling over
00581        if (*off >= size) {
00582          *off=0; // roll over
00583        }
00584        if (JTAG_channels[chn].mode == JTAG_MODE_BOUNDARY) JTAG_channels[chn].mode=JTAG_MODE_SAMPLE; //should write the last byte before reading - or buffer data will be just lost
00585        break;
00586      default:               return -EINVAL;
00587    }
00588 D(printk("fpga_jtag_read_end: p=%x,chn=%x, buf address=%lx count=%lx *offs=%lx, rp=%lx,size=0x%x, mode=%x\r\n",p,chn,(long) buf, (long) count, (long) *off, (long)JTAG_channels[chn].rp, (int) size, JTAG_channels[chn].mode)); // D(printk("count= 0x%x, pos= 0x%x\n", (int) count, (int)*off));
00589   return count;
00590 }
00591 
00592 //++++++++++++++++++++++++++++++++++++ lseek() ++++++++++++++++++++++++++++++++++++++++++++++++++++++
00593 
00594 static loff_t  fpga_jtag_lseek(struct file * file, loff_t offset, int orig) {
00595 /*
00596  *  orig 0: position from begning of eeprom
00597  *  orig 1: relative from current position
00598  *  orig 2: position from last eeprom address
00599  */
00600    int p = ((int *)file->private_data)[0];
00601    int chn= JTAG_channel(p);
00602    size_t size;
00603      if (chn==JTAG_RAW) {
00604        size=raw_fifo_r_wp-raw_fifo_r_rp;
00605        if (size<0) size+=FJTAG_RAW_RSIZE;
00606      } else   size = JTAG_channels[chn].sizew;
00607    if (JTAG_channels[chn].mode == JTAG_MODE_RDID) size = JTAG_channels[chn].sizer;
00608    D(printk("fpga_jtag_lseek, fsize= 0x%x\n", (int) size));
00609         switch (orig) {
00610         case 0:
00611                 file->f_pos = offset;
00612                 break;
00613         case 1:
00614                 file->f_pos += offset;
00615                 break;
00616         case 2:
00617                 file->f_pos = size + offset;
00618                 break;
00619         default:
00620                 return -EINVAL;
00621         }
00622         /* truncate position */
00623         if (file->f_pos < 0) {
00624                 file->f_pos = 0;
00625                 return (-EOVERFLOW);
00626         }
00627 
00628         if (file->f_pos > size) {
00629                 file->f_pos = size;
00630                 return (-EOVERFLOW);
00631         }
00632    D(printk("fpga_jtag_lseek, file->f_pos= 0x%x\n", (int) file->f_pos));
00633         return (file->f_pos);
00634 }
00635 
00636 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00637 
00638 // Initialize GPIOs of the CPU to access JPTAG/programming of the main FPGA
00639 void initPortC(void) {
00640 // connect 8 lower bits of port C to GPIO, disconnect from IOP
00641     unsigned long tmp;
00642     reg_pinmux_rw_pc_iop    pinmux_c_iop;
00643     reg_pinmux_rw_pc_gio    pinmux_c_gio;
00644     reg_gio_rw_pc_oe        pc_oe;
00645 
00646     pinmux_c_iop= REG_RD(pinmux, regi_pinmux, rw_pc_iop);
00647     tmp = REG_TYPE_CONV(unsigned long, reg_pinmux_rw_pc_iop, pinmux_c_iop);
00648     tmp &= ~0xff;
00649     pinmux_c_iop = REG_TYPE_CONV(reg_pinmux_rw_pc_iop, unsigned long, tmp);
00650     REG_WR(pinmux, regi_pinmux, rw_pc_iop, pinmux_c_iop);
00651 
00652     pinmux_c_gio= REG_RD(pinmux, regi_pinmux, rw_pc_gio);
00653     tmp = REG_TYPE_CONV(unsigned long, reg_pinmux_rw_pc_gio, pinmux_c_gio);
00654     tmp |= 0xff;
00655     pinmux_c_gio = REG_TYPE_CONV(reg_pinmux_rw_pc_gio, unsigned long, tmp);
00656     REG_WR(pinmux, regi_pinmux, rw_pc_gio, pinmux_c_gio);
00657 
00658 // now set data of port C pins (static pc_dout)
00659     pc_dout = REG_RD(gio, regi_gio, rw_pc_dout);
00660     pc_dout.data &= ~0xff;
00661     pc_dout.data |= PC_DOUT_INITIAL;
00662     REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00663 
00664 // now set directions of port C pins
00665     pc_oe = REG_RD(gio, regi_gio, rw_pc_oe);
00666     pc_oe.oe &= ~( (1 << FPGAJTAG_TDO_BIT) |
00667                    (1 << FPGAJTAG_DONE_BIT) |
00668                    (1 << FPGAJTAG_RSTBTN_BIT)); 
00669     pc_oe.oe |=  ( (1 << FPGAJTAG_TDI_BIT) |
00670                    (1 << FPGAJTAG_TMS_BIT) |
00671                    (1 << FPGAJTAG_TCK_BIT) |
00672                    (1 << FPGAJTAG_PGM_BIT)); 
00673     REG_WR(gio, regi_gio, rw_pc_oe, pc_oe);
00674 }
00675 
00676 // set FPGA in programming/JTAG mode (only for sensor board)
00677 // NOP for the main board FPGA configuration
00678 void set_pgm_mode (int chn, int en) {
00679 D(printk ("set_pgm_mode (%d,%d)\n",chn,en));
00680 
00681     switch (chn) {
00682     case JTAG_SENSOR_FPGA:
00683       port_csp0_addr[X313_WA_SENSFPGA] = (en ? (3 << SFPGA_PGMEN_BIT): (SFPGA_RD_SENSPGMPIN | (2 << SFPGA_PGMEN_BIT))) | (2  << SFPGA_TCK_BIT); // turn off TCK (if not turned off already)
00684       break;
00685     }
00686    udelay (2);
00687 }
00688 
00689 void set_pgm (int chn, int pgmon) {
00690 D(printk ("set_pgm (%d,%d)\n",chn,pgmon));
00691     switch (chn) {
00692     case JTAG_MAIN_FPGA:
00693       if (pgmon) pc_dout.data  &= ~0x80; // set PGM low (active)
00694       else pc_dout.data  |= 0x80;        // set PGM high (inactive)
00695       REG_WR(gio, regi_gio, rw_pc_dout, pc_dout); // extend low?
00696       break;
00697      case JTAG_SENSOR_FPGA:
00698       port_csp0_addr[X313_WA_SENSFPGA] = (2 | (pgmon & 1)) << SFPGA_PROG_BIT;
00699       break;
00700      case JTAG_AUX_FPGA:
00701       break;
00702     }
00703    udelay (2);
00704 }
00705 
00706 
00707 int read_done (int chn) {
00708    switch (chn) {
00709     case JTAG_MAIN_FPGA:
00710       return ((((REG_RD(gio, regi_gio, r_pc_din)).data & 0x20)==0) ? 0 : 1 ); 
00711     case JTAG_SENSOR_FPGA:
00712       port_csp0_addr[X313_WA_SENSFPGA] = SFPGA_RD_DONE;
00713       udelay (1); 
00714       return (port_csp0_addr[X313__RA__SENSFPGA] >> SFPGA_RD_BIT) & 1 ;
00715     case JTAG_AUX_FPGA:
00716       return 0;
00717    }
00718    return 0; // just in case
00719 }
00720 
00721 //  send 1..8 bits through JTAG 
00722 int jtag_send (int chn, int tms, int len, int d) {
00723    int i; //,m;
00724    int r=0;
00725    int d0;
00726    i = len & 7;
00727    if (i==0) i=8;
00728    d &= 0xff;
00729    d0=d;
00730   D( printk("jtag_send(0x%x, 0x%x, 0x%x, 0x%x)\r\n", chn, tms,len,d));
00731    switch (chn) {
00732     case JTAG_MAIN_FPGA:
00733 
00734        pc_dout.data &= ~0x0e;
00735        pc_dout.data |= (tms & 1) << FPGAJTAG_TMS_BIT;
00736        for (;i>0;i--){
00737          r= (r<<1)+ ((REG_RD(gio, regi_gio, r_pc_din)).data & 1); // read TDO before TCK pulse
00738          pc_dout.data = (pc_dout.data & ~0x0a) | (((d<<=1)>>7) & 2);
00739          REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00740          pc_dout.data |= (1 << FPGAJTAG_TCK_BIT);
00741          REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00742          pc_dout.data &= ~(1 << FPGAJTAG_TCK_BIT);
00743          REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00744        }
00745        break;
00746     case JTAG_SENSOR_FPGA:
00747        port_csp0_addr[X313_WA_SENSFPGA] = SFPGA_RD_TDO;
00748        udelay (1); // wait MUX
00749        for (;i>0;i--){
00750    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00751            port_csp0_addr[X313_WA_SENSFPGA] = ((2 | (tms & 1)) << SFPGA_TMS_BIT) |
00752                                               (((((d<<=1)>>8) & 1) | 2) << SFPGA_TDI_BIT) |
00753                                               (2  << SFPGA_TCK_BIT) ;
00754    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00755          port_csp0_addr[X313_WA_SENSFPGA] = (3  << SFPGA_TCK_BIT); // TCK=1
00756          r= (r<<1)+ ((port_csp0_addr[X313__RA__SENSFPGA] >> SFPGA_RD_BIT) & 1); // read TDO before TCK pulse
00757    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00758 
00759        }
00760        port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0
00761        break;
00762     case JTAG_AUX_FPGA:
00763       break;
00764    }
00765    return r;
00766 }
00767 
00768 //====================================
00769 //         port_csp0_addr[X313_WA_SENSFPGA] = 0; // nop
00770 // write data data bytes from buffer, read data, optionally compare/abort
00771 // return: 0- OK, !=0 - readback mismatch error
00772 // modified so it reads data in-place of the written one
00773 // send/receive bits, raising TMS during the last one (if last==1). If number of bits are not multiple of 8, lower bits of the last byte will not be used.
00774 
00775 int jtag_write_bits (int chn,
00776                      unsigned char *buf, // data to write
00777                      int len,            // number of bytes to write
00778                      int check,          // compare readback data with previously written, abort on mismatch
00779                      int last,           // output last bit with TMS=1
00780                      int prev[2])      // if null - don't use
00781 {
00782    int i,j;
00783    int r=0;
00784    int d,d0;
00785   D( printk("jtag_write_bits(0x%x, 0x%x, 0x%x, 0x%x, 0x%x)\r\n", (int) chn, (int) buf, len, check, last););
00786 
00787    switch (chn) {
00788     case JTAG_MAIN_FPGA: //TODO: save some cycles like for FPGA_SJTAG_MINOR
00789 
00790        for (i=0; len>0;i++) {
00791          pc_dout.data &= ~0x0e;
00792          d0=(d=buf[i]);
00793          for (j=0;j<8;j++) {
00794 //D(printk("i=%x, j=%x, len=%x, d=%x  ",i,j,len,d));
00795            if (len>0) {
00796              r= (r<<1)+ ((REG_RD(gio, regi_gio, r_pc_din)).data & 1);
00797              if ((len==1) && last) pc_dout.data = (pc_dout.data & ~0x0a) | (((d<<=1)>>7) & 2) | (1 << FPGAJTAG_TMS_BIT);
00798              else                  pc_dout.data = (pc_dout.data & ~0x0a) | (((d<<=1)>>7) & 2);
00799              REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00800              pc_dout.data |= (1 << FPGAJTAG_TCK_BIT);
00801              REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00802              pc_dout.data &= ~(1 << FPGAJTAG_TCK_BIT);
00803              REG_WR(gio, regi_gio, rw_pc_dout, pc_dout);
00804            } else r= (r<<1);
00805            len--;
00806 //D(printk(", r=%x\r\n",r));
00807          }
00808          buf[i]=r; // read back in-place
00809          if (check && ((r ^ (prev[1] >> 24)) & 0xff)) {
00810            return -((r & 0xff) | ((i+1) << 8)); //readback mismatch
00811          }
00812          if (prev) {
00813 //         prev64= (prev64<<8) | ((prev32>>24) & 0xff);
00814 //         prev32= (prev32<<8) | (d0 & 0xff);
00815            prev[1]= (prev[1]<<8) | ((prev[0]>>24) & 0xff);
00816            prev[0]= (prev[0]<<8) | (d0 & 0xff);
00817          }
00818        }
00819        break;
00820     case JTAG_SENSOR_FPGA:
00821        port_csp0_addr[X313_WA_SENSFPGA] = SFPGA_RD_TDO; // just in case, it should be in that mode whan calling jtag_write_bits()
00822        udelay (1); // wait MUX
00823        for (i=0; len>0;i++) {
00824          d0=(d=buf[i]);
00825          for (j=0;j<8;j++) {
00826    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00827            if (len>0) {
00828              if ((len==1) && last) port_csp0_addr[X313_WA_SENSFPGA] = 
00829                                               (3 << SFPGA_TMS_BIT) |
00830                                               (((((d<<=1)>>8) & 1) | 2) << SFPGA_TDI_BIT) |
00831                                               (2 << SFPGA_TMS_BIT) |
00832                                               (2 << SFPGA_TCK_BIT) ; 
00833 
00834              else port_csp0_addr[X313_WA_SENSFPGA] =
00835                                               (((((d<<=1)>>8) & 1) | 2) << SFPGA_TDI_BIT) |
00836                                               (2 << SFPGA_TMS_BIT) |
00837                                               (2 << SFPGA_TCK_BIT) ; 
00838    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00839              port_csp0_addr[X313_WA_SENSFPGA] = (3 << SFPGA_TCK_BIT); // TCK=1
00840 // add delays here if long cable?
00841              r= ((r<<1)+ ((port_csp0_addr[X313__RA__SENSFPGA] >> SFPGA_RD_BIT) & 1)); // read TDO before TCK pulse
00842    port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0 - just a delay
00843            } else r= (r<<1);
00844            len--;
00845          }
00846          buf[i]=r; // read back in-place
00847          if (check && ((r ^ (prev[1]>>24)) & 0xff)) {
00848            return -((r & 0xff) | ((i+1) << 8)); //readback mismatch
00849          }
00850          if (prev) {
00851 //         prev64= (prev64<<8) | ((prev32>>24) & 0xff);
00852 //         prev32= (prev32<<8) | (d0 & 0xff);
00853            prev[1]= (prev[1]<<8) | ((prev[0]>>24) & 0xff);
00854            prev[0]= (prev[0]<<8) | (d0 & 0xff);
00855          }
00856 
00857        }
00858      port_csp0_addr[X313_WA_SENSFPGA] = (2  << SFPGA_TCK_BIT); // TCK=0
00859      break;
00860     case JTAG_AUX_FPGA:
00861       break;
00862 
00863   }
00864   return 0;
00865 }
00866 
00867 
00868 int JTAG_configure (int chn, unsigned char * buf, int len) {
00869   int datastart, i, j ,r;
00870 //static int prev32;
00871 //static int prev64;
00872   int prev[2];
00873 #ifdef JTAG_DISABLE_IRQ
00874   unsigned long flags;
00875 #endif
00876   const unsigned char sync[]={0xff,0xff,0xff,0xff,0xaa,0x99,0x55,0x66};
00877   int skipvfy=8;
00878 
00879   D(printk("JTAG_configure: chn=%x,  wp=0x%x, rp=0x%x, len=0x%x\r\n",chn, JTAG_channels[chn].wp, JTAG_channels[chn].rp, len));
00880 
00881 // all the programming goes here...
00882 // find sync:
00883   
00884   datastart=-1;
00885   for (i=0;i<(FJTAG_MAX_HEAD-8);i++) {
00886     j=0;
00887     while ((j<8) && (buf[i+j]==sync[j])) j++;
00888     if (j==8) {
00889       datastart=i;
00890       break;
00891     }
00892   }
00893   if (datastart<0) {
00894     printk("Bitstream not found - bad file\r\n");
00895     return -EFAULT;
00896   }
00897 // check for right bitstream length
00898   if ((len-datastart)!=(XC3S1200E_BITSIZE>>3)) {
00899     printk("Wrong bitstream size - XC3S1200E has bitstream of %d bits (%d bytes)\n",XC3S1200E_BITSIZE,XC3S1200E_BITSIZE>>3);
00900     printk ("header size - %d, data size - %d\r\n",datastart, len-datastart);
00901     return -EFAULT;
00902   }
00903 // enable programmimg mode (nop for the 10353 FPGA)
00904    set_pgm_mode(chn, 1);
00905 // reset device  
00906    set_pgm (chn, 1);
00907    udelay (1000); // needed?
00908    set_pgm (chn, 0);
00909 // wait INIT over - no init connected, just wait >2ms
00910    udelay (2500);
00911 //*************************** NOW DISABLE INTERRUPS FOR THE WHOLE PROGRAMMING CYCLE ***********************
00912   D( udelay (100000);printk("JTAG_configure(): IRQ off!\r\n"); udelay (100000););
00913 #ifdef JTAG_DISABLE_IRQ
00914    local_irq_save(flags);
00915    //local_irq_disable();
00916 #endif
00917 // prepare JTAG
00918   jtag_send(chn, 1, 5, 0   ); //step 1 - set Test-Logic-Reset state
00919   jtag_send(chn, 0, 1, 0   ); //step 2 - set Run-Test-Idle state
00920   jtag_send(chn, 1, 2, 0   ); //step 3 - set SELECT-IR state
00921   jtag_send(chn, 0, 2, 0   ); //step 4 - set SHIFT-IR state
00922   jtag_send(chn, 0, 5, 0xa0); //step 5 - start of CFG_IN  ***NOW 6 bits ***
00923   jtag_send(chn, 1, 1, 0   ); //step 6 - finish CFG_IN
00924   jtag_send(chn, 1, 2, 0   ); //step 7 - set SELECT-DR state
00925   jtag_send(chn, 0, 2, 0   ); //step 8 - set SHIFT-DR state
00926 // write data (first 8 bytes - just fill the buffer, no readback comparison)
00927   jtag_write_bits (chn, 
00928                    &buf[datastart], // data to write
00929                    skipvfy << 3,               // number of bytes to write
00930                    0,                          // compare readback data with previously written, abort on mismatch
00931                    0,                          // do not raise TMS at last bit
00932                    prev);                      // 64 bits storage to verify configuration transmission
00933 
00934   if ((r=jtag_write_bits (chn, 
00935                           &buf[datastart+skipvfy],
00936 //                          (buf8i-(datastart+skipvfy)) << 3,
00937                           (len-(datastart+skipvfy)) << 3,
00938                           1,
00939                           1, prev))<0) {
00940      r= -r;
00941      i= (r>>8) -1 + (datastart+skipvfy);
00942      r &= 0xff;
00943 #ifdef JTAG_DISABLE_IRQ
00944      local_irq_restore(flags);
00945 #endif
00946      set_pgm (chn, 1);
00947      set_pgm (chn, 0);
00948 // disable programmimg mode (nop for the 10353 FPGA)
00949      set_pgm_mode(chn, 0);
00950      printk ("**** Configuration failed at byte # %d (%x)****\n", (i-datastart),(i-datastart));
00951      printk ("**** r= %x, prev64=%x prev32=%x****\n", r,prev[1], prev[0]);
00952      return -EFAULT; 
00953   }
00954   jtag_send(chn, 1, 1, 0   ); //step 11 - set UPDATE-DR state
00955   jtag_send(chn, 1, 2, 0   ); //step 12 - set SELECT-IR state
00956   jtag_send(chn, 0, 2, 0   ); //step 13 - set SHIFT-IR state
00957   jtag_send(chn, 0, 5, 0x30); //step 14 - start of JSTART  ***NOW 6 bits ***
00958   jtag_send(chn, 1, 1, 0   ); //step 15 - finish JSTART
00959   jtag_send(chn, 1, 2, 0   ); //step 16 - set SELECT-DR state
00960   jtag_send(chn, 0, 0, 0   ); //step 17 - set SHIFT-DR , clock startup
00961   jtag_send(chn, 0, 0, 0   ); //step 17a - (total >=12 clocks)
00962   jtag_send(chn, 1, 2, 0   ); //step 18 - set UPDATE-DR state
00963   jtag_send(chn, 0, 1, 0   ); //step 19 - set Run-Test-Idle state
00964   jtag_send(chn, 0, 0, 0   ); //step 19a - only here starts the sequence - adding 17b does not help (5 - sets done, 6 - releases outputs, 7 - releases reset)
00965   jtag_send(chn, 0, 0, 0   ); //step 19b - one more?
00966   // ready or not - device should start now
00967 #ifdef JTAG_DISABLE_IRQ
00968   local_irq_restore(flags);
00969 #endif
00970 //*************************** END OF NO INTERRUPS ***********************
00971   r=read_done(chn);
00972 // disable programmimg mode (nop for the 10353 FPGA)
00973   set_pgm_mode(chn, 0);
00974 
00975   if (r==0) {
00976     printk("*** FPGA did not start after configuration ***\r\n");
00977     return -EFAULT; 
00978   }
00979 
00980   D( udelay (100000);printk("\nJTAG_configure() OK!\r\n"));
00981   return 0;
00982 } //int JTAG_configure
00983 
00984 
00985 //=============================
00986 //
00987 // enable access to JTAG pins. For sensor FPGA that is not possible w/o deprogramming the chip
00988 // leaves in Run-Test-Idle state
00989 int JTAG_openChannel (int chn)  {
00990 D(printk ("JTAG_openChannel (%d)\n",chn));
00991 // enable programmimg mode (nop for the 10353 FPGA)
00992      set_pgm_mode(chn, 1);
00993 // for shared JTAG/data bus we need to de-program the chip to be able to read JTAG :-(
00994      switch (chn) {
00995       case JTAG_SENSOR_FPGA:
00996 // reset device  
00997        set_pgm (chn, 1);
00998        set_pgm (chn, 0);
00999 // wait INIT over - no init connected, just wait >2ms
01000        udelay (2500);
01001       break;
01002      }
01003      jtag_send(chn, 1, 5, 0   ); // set Test-Logic-Reset state
01004      jtag_send(chn, 0, 1, 0   ); // set Run-Test-Idle state
01005 } // int JTAG_openChannel (int chn)
01006 //
01007 int JTAG_resetChannel (int chn)  {
01008 D(printk ("JTAG_resetChannel (%d)\n",chn));
01009      jtag_send(chn, 1, 5, 0   ); // set Test-Logic-Reset state
01010 // disable programmimg mode (nop for the 10353 FPGA)
01011      set_pgm_mode(chn, 0); // only for sensor FPGA
01012 } // int JTAG_resetChannel (int chn)
01013 
01014 int JTAG_readID (int chn, unsigned char * buf)  {
01015   int i;
01016   unsigned long d1,d2=0;
01017   unsigned long * dp;
01018 #ifdef JTAG_DISABLE_IRQ
01019   unsigned long flags;
01020 #endif
01021 
01022 // read dev id, user id
01023 //*************************** NOW DISABLE INTERRUPS FOR THE WHOLE JTAG SEQUENCE ***********************
01024 #ifdef JTAG_DISABLE_IRQ
01025      local_irq_save(flags);
01026      //local_irq_disable();
01027 #endif
01028 // prepare JTAG
01029      jtag_send(chn, 1, 5, 0   ); //step 1 - set Test-Logic-Reset state
01030      jtag_send(chn, 0, 1, 0   ); //step 2 - set Run-Test-Idle state
01031      jtag_send(chn, 1, 2, 0   ); //step 3 - set SELECT-IR state
01032      jtag_send(chn, 0, 2, 0   ); //step 4 - set SHIFT-IR state
01033      jtag_send(chn, 0, 5, 0x90); //step 5 - start of IDCODE
01034      jtag_send(chn, 1, 1, 0   ); //step 6 - finish IDCODE
01035      jtag_send(chn, 1, 2, 0   ); //step 7 - set SELECT-DR state
01036      jtag_send(chn, 0, 2, 0   ); //step 8 - set CAPTURE-DR state
01037      jtag_write_bits (chn, 
01038                       &buf[0], // data to write
01039                       32, // number of bits to write/read
01040                       0,                          // don't compare readback data with previously written
01041                       1, 0) ;                        // raise TMS at last bit
01042      jtag_send(chn, 1, 5, 0   ); //reset state machine to Test-Logic-Reset state
01043      jtag_send(chn, 0, 1, 0   ); //step 2 - set Run-Test-Idle state
01044      jtag_send(chn, 1, 2, 0   ); //step 3 - set SELECT-IR state
01045      jtag_send(chn, 0, 2, 0   ); //step 4 - set SHIFT-IR state
01046      jtag_send(chn, 0, 5, 0x10); //step 5 - start of USERCODE
01047      jtag_send(chn, 1, 1, 0   ); //step 6 - finish USERCODE
01048      jtag_send(chn, 1, 2, 0   ); //step 7 - set SELECT-DR state
01049      jtag_send(chn, 0, 2, 0   ); //step 8 - set CAPTURE-DR state
01050      jtag_write_bits (chn,
01051                       &buf[4], // data to write
01052                       32, // number of bits to write/read
01053                       0,                          // don't compare readback data with previously written
01054                       1,0) ;                      // raise TMS at last bit
01055      jtag_send(chn,1, 5, 0   ); //reset state machine to Test-Logic-Reset state
01056 #ifdef JTAG_DISABLE_IRQ
01057      local_irq_restore(flags);
01058 #endif
01059 //*************************** END OF NO INTERRUPS ***********************
01060 // swap all bits in ID and user fields
01061      dp = (unsigned long *) &buf[0];
01062      d1= *dp;
01063      for (i=0;i<32;i++){
01064         d2 = (d2 << 1) | (d1 & 1);
01065         d1 >>= 1;
01066      }
01067      *dp=d2;
01068      dp = (unsigned long *) &buf[4];
01069      d1= *dp;
01070      for (i=0;i<32;i++){
01071         d2 = (d2 << 1) | (d1 & 1);
01072         d1 >>= 1;
01073      }
01074      *dp=d2;
01075      D(for (i=0; i<8;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");} ); 
01076      data_modified=0; //*************************************************
01077      return 0;
01078 } // int JTAG_readID (int chn, unsigned char * buf)
01079 
01080 /*
01081  * capture all pins w/o changing functionality
01082  * assuming Run-Test-Idle/UPDATE-DR leaving UPDATE-DR
01083  */
01084 
01085 int JTAG_CAPTURE (int chn, unsigned char * buf, int len) {
01086 //int i; // only in debug
01087 #ifdef JTAG_DISABLE_IRQ
01088   unsigned long flags;
01089 #endif
01090 D(printk("buf=%p\n",buf));
01091 //*************************** NOW DISABLE INTERRUPS FOR THE WHOLE JTAG SEQUENCE ***********************
01092 #ifdef JTAG_DISABLE_IRQ
01093      local_irq_save(flags);
01094      //local_irq_disable();
01095 #endif
01096 // prepare JTAG
01097 //     jtag_send(chn, 1, 5, 0   ); //step 1 - set Test-Logic-Reset state
01098 //     jtag_send(chn, 0, 1, 0   ); //step 2 - set Run-Test-Idle state
01099      jtag_send(chn, 1, 2, 0   ); //step 3 - set SELECT-IR state
01100      jtag_send(chn, 0, 2, 0   ); //step 4 - set SHIFT-IR state
01101      jtag_send(chn, 0, 5, 0x80); //step 5 - start of SAMPLE (which bit goes first???)
01102      jtag_send(chn, 1, 1, 0   ); //step 6 - finish SAMPLE
01103      jtag_send(chn, 1, 2, 0   ); //step 7 - set SELECT-DR state
01104      jtag_send(chn, 0, 2, 0   ); //step 8 - set CAPTURE-DR state
01105      jtag_write_bits (chn, 
01106                       buf, // data to write
01107                       len, // number of bits to read
01108                       0,   // don't compare readback data with previously written
01109                       1,0) ; // raise TMS at last bit
01110 //     D(printk ("\n"); for (i=0; i<((len+7)>>3) ;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");}printk ("\n"); ); 
01111 //     D(printk ("\n"); for (i=0; i<((len+7)>>3) ;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");}printk ("\n"); ); 
01112      jtag_send(chn, 1, 1, 0   ); //step 9 - set UPDATE-DR state
01113 //     D(printk ("\n"); for (i=0; i<((len+7)>>3) ;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");}printk ("\n"); ); 
01114 //     jtag_send(chn,1, 5, 0   ); //reset state machine to Test-Logic-Reset state
01115 #ifdef JTAG_DISABLE_IRQ
01116      local_irq_restore(flags);
01117 #endif
01118 //*************************** END OF NO INTERRUPS ***********************
01119      D(printk ("\n"); for (i=0; i<((len+7)>>3) ;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");}printk ("\n"); ); 
01120      data_modified=0;
01121      return 0;
01122 
01123 } // JTAG_CAPTURE (int chn, unsigned char * buf, int len) {
01124 
01125 
01126 /*
01127  * write new boundary registers (len should match BS register length), read pins in-place
01128  * TAP controller is supposed to be in "UPDATE-DR" state (or RUN-TEST/IDLE after just opening this mode)
01129  * TAP controller will be left in the same "UPDATE-DR" after the command
01130  */
01131 int JTAG_EXTEST (int chn, unsigned char * buf, int len) {
01132 #ifdef JTAG_DISABLE_IRQ
01133   unsigned long flags;
01134 #endif
01135 //int i; // only in debug
01136 #ifdef JTAG_DISABLE_IRQ
01137      local_irq_save(flags);
01138      //local_irq_disable();
01139 #endif
01140 D(printk("EXTEST: buf=%p, len=0x%x\n",buf,len));
01141 
01142 //     jtag_send(chn, 1, 5, 0   ); //step 1 - set Test-Logic-Reset state
01143 //     jtag_send(chn, 0, 1, 0   ); //step 2 - set Run-Test-Idle state
01144      jtag_send(chn, 1, 2, 0   ); //step 3 - set SELECT-IR state
01145      jtag_send(chn, 0, 2, 0   ); //step 4 - set SHIFT-IR state
01146      jtag_send(chn, 0, 5, 0xf0); //step 5 - start of EXTEST
01147      jtag_send(chn, 1, 1, 0   ); //step 6 - finish EXTEST
01148      jtag_send(chn, 1, 2, 0   ); //step 7 - set SELECT-DR state
01149      jtag_send(chn, 0, 2, 0   ); //step 8 - set CAPTURE-DR state
01150 
01151      jtag_write_bits  (chn, 
01152                        buf,
01153                        len, // number of bits to write
01154                        0,                         // don't compare readback data with previously written
01155                        1,0);                        // raise TMS at last bit
01156     jtag_send(chn, 1, 1, 0   ); //step 9 - set UPDATE-DR state
01157 #ifdef JTAG_DISABLE_IRQ
01158      local_irq_restore(flags);
01159 #endif
01160      D(printk ("\n"); for (i=0; i<((len+7)>>3) ;i++) {printk("%3x ",(int) buf[i]); if ((i & 0xf) == 0xf) printk ("\n");}printk ("\n"); ); 
01161 
01162     return 0;
01163 } //int JTAG_EXTEST (int chn, unsigned char * buf, int len)
01164 
01165 static int __init fpga_jtag_init(void) {
01166    int i,res;
01167    res = register_chrdev(FPGA_JTAG_MAJOR, fpga_jtag_name, &fpga_jtag_fops);
01168    if(res < 0) {
01169      printk(KERN_ERR "fpga_jtag_init: couldn't get a major number.\n");
01170      return res;
01171    }
01172    printk(FPGA_JTAG_DRIVER_NAME"\r\n");
01173    for (i=0;i<=FPGA_JTAG_MAXMINOR;i++) minors[i]=0;
01174    initPortC();
01175    fpga_state&=~0xffff;
01176         return 0;
01177 }
01178 
01179 
01180 
01181 /* this makes sure that fpga_init is called during boot */
01182 
01183 module_init(fpga_jtag_init);
01184 MODULE_LICENSE("GPL");
01185 MODULE_AUTHOR("Andrey Filippov <andrey@elphel.com>.");
01186 MODULE_DESCRIPTION(FPGA_JTAG_DRIVER_NAME);
01187 

Generated on Thu Aug 7 16:19:00 2008 for elphel by  doxygen 1.5.1