edt_bitload.c

Go to the documentation of this file.
00001 /* #pragma ident "@(#)edt_bitload.c     1.88 11/05/07 EDT" */
00002 
00022 #include "edtinc.h"
00023 #include "edt_bitload.h"
00024 #include "pciload.h" /* xtype */
00025 #include <stdlib.h>
00026 
00027 #ifndef WIN32
00028 
00029 #include <unistd.h>
00030 
00031 #endif
00032 
00033 /* shorthand debug level */
00034 #define DEBUG1 EDTLIB_MSG_INFO_1
00035 #define DEBUG2 EDTLIB_MSG_INFO_2
00036 
00037 typedef struct
00038 {
00039     char    path[MAXPATH];
00040     int     size;
00041 }       BFENTRY;
00042 
00043 static int bitload_prog_xilinx(EdtDev * edt_p, FILE * xfd, char *header, int alt, int alt_param, int do_sleep);
00044 
00045 #ifdef NO_FS
00046 static int bitload_prog_xilinx_nofs(EdtDev * edt_p, u_char *xa, char *header, int do_sleep);
00047 #endif
00048 
00049 static int bitload_rc_prog_xilinx(EdtDev * edt_p, FILE * xfd, char *header);
00050 static int bitload_get_subdir_files(const char *basedir, char *devdir, char *fname, BFENTRY bitfiles[], int *nbfiles);
00051 static int bitload_sort_bitfile_list(EdtDev *edt_p, BFENTRY bitfiles[], int nbfiles);
00052 static int bitload_prom_loadit(EdtDev *edt_p, u_char *addr, int size, int do_sleep);
00053 static void rc_prog_write(EdtDev * edt_p, u_char val);
00054 static int valid_bit_dirname(char *name);
00055 static int has_slashes(char *name);
00056 
00057 
00058 #ifdef NO_FS
00059 #include "nofs_bitfiles.h"
00060 #endif
00061 
00062 u_char *Prombuf;
00063 
00064 
00065 #ifdef PCD
00066 
00067 int
00068 edt_program_ocm(EdtDev *edt_p, u_char *buf, int size, int channel)
00069 
00070 {
00071     int loop;
00072     int inner_loop;
00073     int base;
00074     int xfer;
00075     int regrd;
00076     int count;
00077     int control;
00078     int release;
00079     int done = 0;
00080     int bad_program = 0;
00081 
00082     /* drive init and prog_l low */
00083     /* select channel for data */
00084     control = edt_reg_read(edt_p, OCM_X_CONT);
00085     if (channel == 1) 
00086     {
00087         control |= (OCM_CONT_CH1_INIT | OCM_CONT_CH1_PROG); /* tristate INIT; drive PROG_L low */ 
00088 
00089 
00090         release = control & ~OCM_CONT_CH1_PROG; /* set PROG_L high */
00091 
00092         /* start Xilinx's programming machine and tristate INIT, so it
00093          * is driven by FPGA being programmed.  */
00094         release |= (OCM_CONT_EN_FIFO | OCM_CONT_ENABLE | OCM_CONT_CH1_INIT | OCM_CONT_PRG_CH1); 
00095     }
00096     else
00097     {
00098         control &= ~OCM_CONT_PRG_CH1;
00099         control |= (OCM_CONT_CH0_INIT | OCM_CONT_CH0_PROG);
00100 
00101         release = control & ~(OCM_CONT_CH0_PROG);
00102         release |= (OCM_CONT_EN_FIFO | OCM_CONT_ENABLE | OCM_CONT_CH0_INIT);
00103     }
00104     edt_reg_write(edt_p, OCM_X_CONT, control);
00105 
00106     /* wait for done to be low */
00107     loop = 0;
00108     edt_msg(DEBUG2, "\nwaiting for DONE low\n");
00109     while (((regrd = edt_reg_read(edt_p, OCM_X_STAT)) & (OCM_STAT_CH0_DONE << (channel * 2))) != 0)
00110     {
00111         if (loop++ > 10000000)
00112         {
00113             /* edt_msg(DEBUG2, "reading %02x waiting for not DONE %02x\n",
00114             regrd, OCM_STAT_CH0_DONE<< (channel *2)); */
00115             return (1);
00116         }
00117     }
00118     edt_msg(DEBUG2, "loop %d\n", loop);
00119 
00120     /* enable serializer fifo, set prog_l high and release INIT */
00121     edt_reg_write(edt_p, OCM_X_CONT, release);
00122 
00123     /* wait for INIT to go high */
00124     edt_msg(DEBUG2, "waiting for INIT high\n");
00125     loop = 0;
00126     while (((regrd = edt_reg_read(edt_p, OCM_X_STAT)) & (OCM_STAT_CH0_INIT << (channel * 2))) == 0)
00127     {
00128         /* edt_msg(DEBUG2, "reading %02x waiting for not INIT %02x\n", regrd, OCM_STAT_CH0_INIT<< (channel *2)); */
00129         if (loop++ > 1000000)
00130         {
00131             /* edt_msg(DEBUG2, "reading %02x waiting for not INIT %02x\n", regrd, OCM_STAT_CH0_INIT<< (channel *2)); */
00132             return (1);
00133         }
00134     }
00135     edt_msg(DEBUG2, "loop %d stat %02x\n", loop, regrd);
00136 
00137 
00138     /* load the data */
00139     /* the channel has been selected above */
00140     bad_program = 0;
00141     base = 0;
00142     while ((base < size) && !bad_program && !done)
00143     {
00144         regrd = edt_reg_read(edt_p, OCM_X_STAT);
00145         if ((regrd & (OCM_STAT_CH0_DONE << (channel * 2))) != 0)
00146         {
00147             done = 1;
00148         }
00149         else if ((regrd & (OCM_STAT_CH0_INIT << (channel * 2))) == 0)
00150         {
00151             bad_program = 1;
00152         }
00153         /* fifo holds 15 bytes amount to transfer is then 15 - the current count */
00154         count = (edt_reg_read(edt_p, OCM_X_STAT) & OCM_FCNT_MSK) >> OCM_FCNT_SHFT;
00155         xfer = 15 - count;
00156 
00157         if (base + xfer > size)
00158         {
00159                 xfer = size - base;
00160         }
00161 
00162         for(inner_loop = 0; inner_loop < xfer; inner_loop++)
00163         {
00164             edt_reg_write(edt_p, OCM_X_DATA, buf[base+inner_loop]);
00165         }
00166         base += xfer;
00167     } 
00168 
00169     edt_msg(DEBUG2, "waiting for DONE high\n");
00170 
00171     loop = 0;
00172 
00173     /* output 1s until DONE is high or count exceeds reasonable amount */
00174     edt_msg(DEBUG2, "OCM Status %02x DONE %02x\n", edt_reg_read(edt_p, OCM_X_STAT),
00175         (OCM_STAT_CH0_DONE << (channel * 2)));
00176 
00177     while ((((regrd = edt_reg_read(edt_p, OCM_X_STAT)) & (OCM_STAT_CH0_DONE << (channel * 2))) == 0) &&
00178         !bad_program )
00179     {
00180         /* wait for data fifo to be empty */
00181         inner_loop = 0;
00182         while (((regrd = edt_reg_read(edt_p, OCM_X_STAT)) & OCM_FCNT_MSK) != 0)
00183         {
00184             if (inner_loop++ > 1000000)
00185             {
00186                 edt_msg(DEBUG2, "timeout wait for fifo zero state register %02x\n", regrd);
00187                 return (1);
00188             }
00189         }
00190         /* CLKSAFTER must be greater than 1 so we will output at least on batch of ones */
00191         if (loop > CLKSAFTER)
00192         {
00193             edt_msg(DEBUG2, "> %d writes without done - fail\n", CLKSAFTER);
00194             bad_program = 1;
00195         }
00196         /* output 15 bytes of ones if done not high yet */
00197         for(inner_loop = 0; inner_loop < 15; inner_loop++)
00198         {
00199             edt_reg_write(edt_p, OCM_X_DATA, 0xff);
00200         }
00201         loop++;
00202     }
00203     edt_msg(DEBUG2, "loop %d\n", loop);
00204 
00205     return (bad_program);
00206 }
00207 
00219 int
00220 edt_program_srxl(EdtDev *edt_p, u_char *buf, int size, int dummy)
00221 {
00222     unsigned char regrd;
00223     int i;
00224 
00225     edt_reg_write(edt_p, SRXL_X_CTRL, SRXL_X_CTRL_INIT); /* stop driving INITL */
00226     edt_reg_write(edt_p, SRXL_X_CTRL, SRXL_X_CTRL_PROG);        /* drive progl low */
00227 
00228 
00229     edt_msleep(100);    /* for 0.1 seconds  */
00230     if ( (edt_reg_read(edt_p, SRXL_X_STAT) & SRXL_X_STAT_INIT) != 0) {  
00231         /* INIT should be asserted (INIT low)  */
00232         edt_msg(DEBUG2, "Err, INITL still hi when PROGL to Xilinx was asserted\n");
00233         return 1;
00234     }
00235 
00236 
00237     /* drive progl hi (zero that bit) while INIT stays 1  */
00238     edt_reg_write(edt_p, SRXL_X_CTRL, SRXL_X_CTRL_INIT);        
00239 
00240     edt_msleep(100);            /* for 0.1 seconds  */
00241     if (!(edt_reg_read(edt_p, SRXL_X_STAT) & SRXL_X_STAT_INIT)){  
00242         /* INIT should be unasserted (INIT hi)  */
00243         edt_msg(DEBUG2, "INITL still low after PROGL to Xilinx was deasserted\n");
00244         return 1;
00245     }
00246 
00247 
00248     /* turn on programming state machine  */
00249     edt_reg_write(edt_p, SRXL_X_CTRL, SRXL_X_CTRL_INIT | SRXL_X_CTRL_PROG_EN);
00250     /* turn on data buffer and CCLK (bit 5) is set low  */
00251     edt_reg_write(edt_p, SRXL_X_CTRL, 
00252         SRXL_X_CTRL_INIT | SRXL_X_CTRL_PROG_EN | SRXL_X_CTRL_FIFO_EN);
00253 
00254 
00255     /* load the data */
00256     for (i = 0; i < size; ++i) {
00257         /* If part too small, will smoke if we don't abort the load!  */
00258         if (edt_reg_read(edt_p, SRXL_X_STAT) & SRXL_X_STAT_DONE) {
00259             if (i == 0) {
00260                 edt_msg(DEBUG2, "Err, DONE was true before download was started\n");
00261                 return 1;
00262             } else break;
00263         }
00264 
00265         /* if 16-byte fifo is getting full, wait. */
00266         /* (FYI, the 12 byte limit used was arbitrarily picked.) */
00267         /* while ((edt_reg_read(edt_p, SRXL_X_STAT) >> SRXL_X_STAT_FCNT_SHFT) > 12 ); */
00268         while ( SRXL_X_STAT_FIFO_CNT(edt_p) > 12 );
00269 
00270         edt_reg_write(edt_p, SRXL_X_DATA, buf[i]); /* send data bytes  */
00271     }
00272 
00273     edt_reg_write(edt_p, SRXL_X_DATA, 0);
00274     edt_reg_write(edt_p, SRXL_X_DATA, 0);
00275 
00276     /* wait a while to let fifo clear. It shouldn't take more 
00277     * than .1 second for 12 or less bytes to pass thru fifo. */
00278     edt_msleep(100);    /* for 0.1 seconds  */
00279 
00280     if (((regrd = edt_reg_read(edt_p, SRXL_X_STAT)) & SRXL_X_STAT_FCNT_MSK) != 0) {
00281         edt_msg(DEBUG2, "timeout waiting for fifo to empty. status register: %02x\n", regrd);
00282         return (1);
00283     }
00284 
00285     /* if xilinx doesn't think it is done being programmed, we've got a problem */
00286     if (!(edt_reg_read(edt_p, SRXL_X_STAT) & SRXL_X_STAT_DONE)){
00287         edt_msg(DEBUG2, "Err, DONE was not true after download was completed\n");
00288         return 1;
00289     }
00290 
00291     return 0;
00292 }
00293 
00294 
00295 #endif /* end PCD */
00296 
00297 #include "pciload.h"
00298 
00299 int
00300 edt_bitload_select_fox_file(EdtDev *edt_p,
00301                             char *rbtfile)
00302                             /* Kludge to address the tlk1 vs. tlk4 issue */
00303 {
00304     int     promcode;
00305     u_char  stat;
00306     int vb = 0;
00307     char name[MAX_STRING+1];
00308     int pci_channels;
00309     char bitname[128];
00310     size_t len;
00311 
00312     promcode = edt_x_prom_detect(edt_p, &stat, vb);
00313     edt_x_getname(edt_p, promcode, name);
00314 
00315     edt_msg(DEBUG1, "Looking at board pci id = %s\n", name);
00316 
00317     if (!strcmp(name, "dvtlk1"))
00318     {
00319         pci_channels = 1;
00320     }
00321     else if (!strcmp(name, "dvtlk4"))
00322     {
00323         pci_channels = 4;
00324     }
00325     else 
00326     {
00327         edt_msg(EDT_MSG_FATAL, "DVFOX pci bitfile <%s> not dvtlk4 or dvtlk4\n", name);
00328         return -1;
00329     }
00330 
00331     /* split file name to remove .bit */
00332     strcpy(bitname, rbtfile);
00333     edt_msg(DEBUG1, "Expand bitfile name %s -> ", bitname);
00334     len = strlen(bitname);
00335     if ((len >= 4) && (strcasecmp(&bitname[len-4], ".bit") == 0))
00336     {
00337         bitname[len-4] = '\0';
00338 
00339         sprintf(rbtfile,"%s%d.bit", bitname,pci_channels);
00340 
00341 
00342     }
00343     edt_msg(DEBUG1, "%s\n", rbtfile);
00344 
00345     return 0;
00346 }
00347 
00348 int
00349 bitload_read_header(char *bitpath, char *header, int *size);
00350 
00368 int
00369 edt_bitload(EdtDev *edt_p, const char *basedir, const char *bitfilename, int flags, int skip)
00370 {
00371     int     i, size;
00372     int     nofs = 0, ovr=0;
00373     int         alt_param = ALT_INTERFACE, prg_alt = 0;
00374     int     fullpath;
00375     int     nbfiles = 0;
00376     int     do_sleep = 0;
00377     int     do_readback = 0;
00378     char    devdir[MAXPATH];
00379     char    header[256];
00380     BFENTRY bitfiles[64];
00381     char    fname[MAXPATH];
00382 #ifdef NO_FS
00383     u_char *ba;
00384 #endif
00385 
00386     if (flags & BITLOAD_FLAGS_NOFS)
00387         nofs = 1;
00388     if (flags & BITLOAD_FLAGS_OVR)
00389         ovr = 1;
00390     if (flags & BITLOAD_FLAGS_SLEEP)
00391         do_sleep = 1;
00392     if (flags & BITLOAD_FLAGS_READBACK)
00393         do_readback = 1;
00394 
00395     edt_ioctl(edt_p, EDTS_PROG_READBACK, &do_readback);
00396 
00397 #ifdef PCD
00398     if ((flags & BITLOAD_FLAGS_MEZZANINE) || (flags & BITLOAD_FLAGS_SRXL)) {
00399         /* For any mezzanine, we skip the comparison of 
00400         * mezz. xilinx size to ss/gs intfc xilinx size.
00401         */
00402         ovr = 1;
00403 
00404         prg_alt = ALT_MEZZANINE;
00405     }
00406 
00407 #endif
00408     if (flags & BITLOAD_FLAGS_CH1)
00409         alt_param = 1;
00410 
00411     /* added so the FOX HACK can modify the file name */
00412     strcpy(fname, bitfilename);
00413 
00414     /* FOX HACK - we need to change the name to 
00415     match the PCI bitfile */
00416 
00417     if (edt_p->devid == PDVFOX_ID)
00418     {
00419 
00420         if (!strcmp(fname,"aiag.bit") ||
00421             !strcmp(fname,"aiagcl.bit"))
00422             if (edt_bitload_select_fox_file(edt_p,
00423                 fname) == -1)
00424                 return -1;
00425     }
00426     
00427 #ifdef PCD
00428         /* OC192 HACK - need to change the name for rev2 boards */
00429         if (prg_alt == ALT_MEZZANINE  &&  edt_get_board_id(edt_p) == MEZZ_OC192) {
00430                 edt_oc192_rev2_fname_hack(edt_p, bitfilename, fname);
00431         }
00432 #endif
00433 
00434     /* ALERT: else if pcie dvfox --  no dvtlk1/tlk4 issue so just do it? */
00435 
00436 #ifdef NO_FS
00437     /* special case nofs  -- fname isn't really fname but instead pointer to data */
00438     if (nofs)
00439     {
00440         char nofsname[MAXPATH];
00441 
00442         /* ALERT: to be really complete, we'd detect (?) and prepend appropriate
00443         * xilinx here, but should work to punt and just hardcode to 4013
00444         * unless/until used with nofs pmc card that's other than 4013
00445         */
00446         sprintf(nofsname, "4013_%s", fname);
00447 
00448         if (nofs) /* strip off .bit from name if nofs */
00449         {
00450             int len = strlen(nofsname);
00451             if ((len >= 4) && (strcasecmp(&nofsname[len-4], ".bit") == 0))
00452                 nofsname[len-4] = '\0';
00453         }
00454 
00455         MAPBITARRAY(nofsname, ba);
00456 
00457         if (ba == NULL)
00458         {
00459             printf("no match for nofs header array '%s' (bitload.c & nofs_bitfiles.h)\n", fname);
00460             exit(1);
00461         }
00462 
00463         edt_msg(DEBUG2, "edt_bitload(basedir=%s array=%x nofs=%d flags=0x%x skip=%d)\n",
00464             basedir, fname, nofs, flags, skip);
00465         return bitload_prog_xilinx_nofs(edt_p, ba, header, do_sleep);
00466     }
00467 #endif /* NO_FS */
00468 
00469     edt_msg(DEBUG2, "edt_bitload(basedir=%s fname=%s nofs=%d flags=0x%x skip=%d)\n",
00470         basedir, fname, nofs, flags, skip);
00471 
00472     edt_bitload_devid_to_bitdir(edt_p, devdir);
00473 
00474     fullpath = has_slashes(fname);
00475 
00476     if (fullpath)
00477     {
00478         if (!edt_access(fname, 0) == 0)
00479         {
00480             edt_msg_perror(DEBUG2, fname);
00481             return -1;
00482         }
00483 
00484         bitload_read_header(fname, header, &size);
00485 
00486         if (edt_bitload_loadit(edt_p, fname, size, skip, ovr, prg_alt, alt_param, 1, do_sleep) == 0)
00487             return 0;
00488     }
00489     else
00490     {
00491         char    tmppath[MAXPATH];
00492         char    header[256];
00493         int     size;
00494 
00495         /* dir/file */
00496         sprintf(tmppath, "%s/%s", basedir, fname);
00497         if ((edt_access(tmppath, 0) == 0)
00498             && (bitload_read_header(tmppath, header, &size) == 0))
00499         {
00500             bitfiles[nbfiles].size = size;
00501             strcpy(bitfiles[nbfiles++].path, tmppath);
00502         }
00503 
00504         /* dir[/devdir]/subdirs.../file */
00505         bitload_get_subdir_files(basedir, devdir, fname, bitfiles, &nbfiles);
00506 
00507         /* dir[/devdir]/bitfiles/subdirs.../file */
00508         sprintf(tmppath, "%s/bitfiles", basedir);
00509         bitload_get_subdir_files(tmppath, devdir, fname, bitfiles, &nbfiles);
00510 
00511         bitload_sort_bitfile_list(edt_p, bitfiles, nbfiles);
00512 
00513         for (i = 0; i < nbfiles; i++)
00514         {
00515             if (edt_bitload_loadit(edt_p, bitfiles[i].path, bitfiles[i].size, skip, ovr, prg_alt, alt_param, i+1, do_sleep) == 0)
00516                 return 0;
00517         }
00518     }
00519 
00520     return -1;
00521 }
00522 
00523 #define PROM_BUFSIZE ((SIZED_DATASIZE/4) - sizeof(u_int))
00524 
00525 
00526 
00531 edt_bitload_from_prom(EdtDev *edt_p, u_char *addr1, int size1, u_char *addr2, int size2, int flags)
00532 {
00533     int ret, do_sleep, do_readback = 0;
00534 
00535 
00536     if (flags & BITLOAD_FLAGS_SLEEP)
00537         do_sleep = 1;
00538     if (flags & BITLOAD_FLAGS_READBACK)
00539         do_readback = 1;
00540 
00541     edt_ioctl(edt_p, EDTS_PROG_READBACK, &do_readback);
00542 
00543     if ((ret = edt_program_xilinx_start(edt_p)) != 0)
00544         return ret;
00545 
00546     if (bitload_prom_loadit(edt_p, addr1, size1, do_sleep) != 0)
00547         return -1;
00548     if (bitload_prom_loadit(edt_p, addr2, size2, do_sleep) != 0)
00549         return -1;
00550 
00551     return edt_program_xilinx_end(edt_p);
00552         
00553 }
00554 
00555 /*
00556  * returns 0 if bitfile available, including uncompressing
00557  * analogous to edt_access
00558  */
00559 
00560 int
00561 edt_access_bitfile(char *tmppath, int perm)
00562 
00563 {
00564     int found_bitfile = 0;
00565     int     found_compressed = 0 ;
00566     static char    ztmppath[MAXPATH];
00567     static char    cmd[MAXPATH];
00568 
00569     edt_correct_slashes(tmppath);
00570 
00571     if (edt_access(tmppath, perm) == 0)
00572         found_bitfile = 1 ;
00573 
00574     else /* Check for compressed version of bitfile (.Z or .gz) */
00575     {
00576         strcpy(ztmppath, tmppath);
00577         strcat(ztmppath, ".Z");
00578 
00579         if (edt_access(ztmppath, perm) == 0)
00580         {
00581             sprintf(cmd, "uncompress %s", tmppath) ;
00582             edt_msg(DEBUG1, "%s\n", cmd);
00583             edt_system(cmd) ;
00584             found_compressed = 1 ;
00585         }
00586 
00587         if (!found_compressed)
00588         {
00589             strcpy(ztmppath, tmppath);
00590             strcat(ztmppath, ".gz");
00591             if (edt_access(ztmppath, perm) == 0)
00592             {
00593                 sprintf(cmd, "gunzip %s", tmppath) ;
00594                 edt_msg(DEBUG1, "%s\n", cmd);
00595                 edt_system(cmd) ;
00596                 found_compressed = 1 ;
00597             }
00598         }
00599 
00600         if (!found_compressed)
00601         {
00602             strcpy(ztmppath, tmppath);
00603             strcat(ztmppath, ".zip");
00604             if (edt_access(ztmppath, perm) == 0)
00605             {
00606                 
00607                 size_t  i;
00608                 i = strlen(ztmppath)-1;
00609                 
00610                 while (i && ztmppath[i] != '/' && ztmppath[i] != '\\')
00611                     i--;
00612                 
00613                 if (i)
00614                 {  
00615                     char *fname; 
00616                     char ch = ztmppath[i];
00617                     ztmppath[i] = 0;
00618                     fname = ztmppath + i + 1;
00619 
00620                     sprintf(cmd,"cd %s ; unzip %s",
00621                         ztmppath, fname);
00622 
00623                     ztmppath[i] = ch;
00624 
00625                 }
00626                 else
00627                     sprintf(cmd, "unzip %s", ztmppath);
00628 
00629                 edt_correct_slashes(tmppath);
00630                 edt_msg(DEBUG1, "%s\n", cmd);
00631                 edt_system(cmd) ;
00632                 edt_msg(DEBUG1, "rm %s\n", ztmppath) ;
00633                 unlink(ztmppath) ;
00634                 found_compressed = 1 ;
00635             }
00636         }
00637 
00638         if (found_compressed && edt_access(tmppath, perm) == 0)
00639             found_bitfile = 1 ;
00640     }
00641 
00642     /* return 0 for success, so it matches edt_access */
00643 
00644     return (found_bitfile) ? 0 : -1;
00645 }
00646 
00647 /*
00648  * SPECIAL CODE only for loading from prom (currently only PMC FOX
00649  * for SLAC).
00650  * Loads part or all of a bitfile, reading out of the PROM, starting
00651  * at a given pronm address, called by edt_bitload_from_prom which also
00652  * does the bitload preamble
00653  */
00654 int
00655 bitload_prom_loadit(EdtDev *edt_p, u_char *addr, int size, int do_sleep)
00656 {
00657     int i;
00658     u_char buf[PROM_BUFSIZE];
00659     u_char *addr_p = addr;
00660     u_char *buf_p;
00661     u_char dmy;
00662     int xfer;
00663 
00664 #if 0
00665     /* back up 256 bytes from addr then scan forward thru 0xc3s until c3 a5 */
00666     if ((dmy = edt_flipbits(edt_x_read(edt_p, (u_int)addr_p, XTYPE_BT))) != 0xc3)
00667     {
00668         edt_msg(DEBUG1, "\nread/load: addr_p %x: %02x s/b 0xc3!\n", addr_p, dmy);
00669         return -1;
00670     }
00671 
00672     while ((dmy = edt_flipbits(edt_x_read(edt_p, (u_int)addr_p++, XTYPE_BT))) == 0xc3)
00673     {
00674         if (addr_p == addr+(size/2)) /* bail out if too many */
00675         {
00676             edt_msg(DEBUG1, "\n*read/load: too many 0xc3s!\n");
00677             return -1;
00678         }
00679     }
00680 
00681     /* okay got NOT 0xc3, next one better be 0xa5 */
00682     if (dmy != 0xa5)
00683         return -1;
00684 #endif
00685 
00686     /* read/program */
00687     xfer = 0;
00688     buf_p = buf;
00689     edt_msg(DEBUG1, "\naddr_p %06x\n", addr_p);
00690     for (i=0;i<size; i++)
00691     {
00692         *buf_p = edt_flipbits(edt_x_read(edt_p, (u_int)addr_p, XTYPE_BT));
00693         /* ALERT: hard-coded xtype */
00694 
00695 #if DUMP_HEX
00696         if (!(i%16))
00697             printf("\n%07d ", i);
00698         printf("%02x%s", *buf_p, i%2?" ":"" );
00699 #endif
00700 
00701         ++addr_p;
00702         ++buf_p;
00703         ++xfer;
00704 
00705         if ((xfer == PROM_BUFSIZE) || (i == size-1))
00706         {
00707 #ifndef DUMP_HEX
00708             printf(".");
00709             fflush(stdout);
00710 #endif
00711             edt_program_xilinx_chunk(edt_p, buf, xfer, do_sleep);
00712 
00713             xfer = 0;
00714             buf_p = buf;
00715 
00716         }
00717     }
00718     return 0;
00719 }
00720 
00721 
00722 /*
00723 * get files and sizes from all subdirs in a given dir
00724 * 
00725 * return 0 on success, -1 on failure
00726 */
00727 int
00728 bitload_get_subdir_files(const char *basedir, char *devdir, char *fname,
00729                          BFENTRY bitfiles[], int *nbfiles)
00730 {
00731     DIRHANDLE  dirp = (DIRHANDLE)0;
00732 
00733     char    d_name[MAXPATH];
00734     char    header[256];
00735     char    dirpath[MAXPATH];
00736 
00737     sprintf(dirpath, "%s%s%s", basedir, (devdir && (*devdir)) ? "/" : "", devdir);
00738 
00739     if ((dirp = edt_opendir(dirpath)) == (HANDLE)0)
00740     {
00741         edt_msg_perror(DEBUG2, dirpath);
00742         edt_msg(DEBUG2, "could not open directory <%s>\n", dirpath);
00743         return -1;
00744     }
00745 
00746     while (edt_readdir(dirp, d_name))
00747     {
00748         /* look for numeric dir names, at least 4 chars long */
00749         if (valid_bit_dirname(d_name))
00750         {
00751             static char    tmppath[MAXPATH];
00752             int     size;
00753             int     found_bitfile = 0 ;
00754 
00755             /* ALERT: check if d_name is directory here! */
00756             /* See stat (2) and stat (5) */
00757             sprintf(tmppath, "%s/%s/%s", dirpath, d_name, fname);
00758             
00759             found_bitfile = (edt_access_bitfile(tmppath, 0) == 0);
00760 
00761             if (found_bitfile && bitload_read_header(tmppath,header,&size) == 0)
00762             {
00763                 bitfiles[*nbfiles].size = size;
00764                 strcpy(bitfiles[*nbfiles].path, tmppath);
00765                 ++*nbfiles;
00766             }
00767         }
00768     }
00769 
00770     edt_closedir(dirp);
00771 
00772     return 0;
00773 }
00774 
00775 
00776 int
00777 bitload_read_header(char *bitpath, char *header, int *size)
00778 {
00779     FILE   *xfd;
00780     int     ret;
00781 
00782     if ((xfd = fopen(bitpath, "rb")) == NULL)
00783     {
00784         edt_msg_perror(DEBUG2, bitpath);
00785         return -1;
00786     }
00787     ret = edt_get_x_header(xfd, header, size);
00788     fclose(xfd);
00789     return ret;
00790 }
00791 
00792 
00793 
00794 /*
00795 * extract xilinx directory token (2nd to last in path) from path string
00796 * Return 0 on failure, 1 on success (with xilinx dir in arg)
00797 */
00798 int
00799 bitload_xilinx_from_path(char *path, char *xilinx)
00800 {
00801     char tmppath[MAXPATH];
00802     char *sp, *ep;
00803 
00804     xilinx[0] = '\0';
00805     strcpy(tmppath, path);
00806     edt_back_to_fwd(tmppath);
00807     if ((ep = strrchr(tmppath, '/')) == NULL)
00808         return 0;
00809     sp = ep-1;
00810     *ep = '\0';
00811     while (sp > tmppath)
00812     {
00813         if (*sp == '/')
00814         {
00815             strcpy(xilinx, sp+1);
00816             break;
00817         }
00818         else --sp;
00819     }
00820     if (*xilinx == '\0')
00821         return 0;
00822     return 1;
00823 }
00824 
00825 
00826 /*
00827 * bubble sort by size, in ascending order
00828 * if edt_parts.xpn xref file is present, read it and put
00829 * matching xilinx at the top of the sorted list
00830 */
00831 int
00832 bitload_sort_bitfile_list(EdtDev *edt_p, BFENTRY bitfiles[], int nbfiles)
00833 {
00834     int     i, sorted = 0;
00835     int     tmpsize = 0;
00836     char    esn[128];
00837     char    bd_xilinx[32];
00838     Edt_embinfo ei;
00839     BFENTRY bftmp;
00840 
00841     bd_xilinx[0] = 0;
00842 
00843     if (!nbfiles)
00844     {
00845         edt_msg(DEBUG2, "bitfile not found -- check pathname, or specify a different basedir (see -d)\n");
00846         return 0;
00847     }
00848 
00849 #if 0
00850     edt_msg(DEBUG2, "\nbitfile list before sorting:\n");
00851     for (i = 0; i < nbfiles; i++)
00852         edt_msg(DEBUG2, "%s %d\n", bitfiles[i].path, bitfiles[i].size);
00853     edt_msg(DEBUG2, "\n");
00854 #endif
00855 
00856     edt_get_esn(edt_p, esn);
00857     if (edt_parse_esn(esn, &ei) == 0)
00858     {
00859         /* if xxx-xxxxx-00, replace last 2 digits with rev */
00860         if ((strcmp(&(ei.pn[8]), "00") == 0))
00861             sprintf(&(ei.pn[8]), "%02d", ei.rev);
00862         if (*ei.ifx)
00863             strcpy(bd_xilinx, ei.ifx);
00864         else if (edt_find_xpn(ei.pn, bd_xilinx))
00865             edt_msg(DEBUG2, "matching xilinx found: pn <%s> xilinx <%s>\n", ei.pn, bd_xilinx);
00866         else edt_msg(DEBUG2, "no matching xilinx found: for pn <%s>\n", ei.pn);
00867     }
00868     else edt_msg(DEBUG2, "missing or invalid pn/xilinx, can't xref (esn <%s>)\n", esn);
00869 
00870     /*
00871     * if xref match exists, fake out the sort to make sure this one
00872     * ends up on top
00873     */
00874     if (bd_xilinx[0])
00875     {
00876         for (i=0; i<nbfiles; i++)
00877         {
00878             char bf_xilinx[MAXPATH];
00879             if (bitload_xilinx_from_path(bitfiles[i].path, bf_xilinx))
00880             {
00881                 if (strcasecmp(bf_xilinx, bd_xilinx) == 0)
00882                 {
00883                     tmpsize = bitfiles[i].size;
00884                     bitfiles[i].size = 1;
00885                     break;
00886                 }
00887             }
00888         }
00889     }
00890 
00891     while (!sorted)
00892     {
00893         sorted = 1;
00894         for (i = 0; i < nbfiles - 1; i++)
00895         {
00896             if (bitfiles[i].size > bitfiles[i + 1].size)
00897             {
00898                 sorted = 0;
00899                 bftmp.size = bitfiles[i].size;
00900                 strcpy(bftmp.path, bitfiles[i].path);
00901 
00902                 bitfiles[i].size = bitfiles[i + 1].size;
00903                 strcpy(bitfiles[i].path, bitfiles[i + 1].path);
00904 
00905                 bitfiles[i + 1].size = bftmp.size;
00906                 strcpy(bitfiles[i + 1].path, bftmp.path);
00907             }
00908         }
00909     }
00910 
00911     /* if xref match, reset entry to correct size */
00912     if (tmpsize > 0)
00913         bitfiles[0].size = tmpsize;
00914 
00915     edt_msg(DEBUG2, "bitfile list after sorting:\n");
00916     for (i = 0; i < nbfiles; i++)
00917         edt_msg(DEBUG2, "%s %d\n", bitfiles[i].path, bitfiles[i].size);
00918     edt_msg(DEBUG2, "\n");
00919 
00920     return 0;
00921 }
00922 
00923 /*
00924 * edt_bitload_loadit
00925 * 
00926 * ARGUMENTS
00927 *     edt_p     same as it ever was
00928 *     bitpath  pathname to bitfile
00929 *     size     size of bitfile
00930 *     skip     skip load; test only
00931 *     ovr       override GS/SS size test (mostly for eng/debug)
00932 * return 0 on success, -1 on failure
00933 */
00934 int
00935 edt_bitload_loadit(EdtDev * edt_p, char *bitpath, int size, int skip, int ovr, int alt, int alt_param, int try, int do_sleep)
00936 {
00937     FILE   *xfd;                /* file for xilinx test load */
00938     int     ret = -1;
00939     char    header[128];
00940    
00941     if (skip)
00942         edt_msg(DEBUG2, "would try <%s>...\n", bitpath);
00943     else
00944         edt_msg(DEBUG2, "trying <%s>...\n", bitpath);
00945 
00946     /* ovr = override gs/ss size test check */
00947     if (!ovr)
00948     {
00949         /* check SS and GS min and min/max size respectively */
00950         if ((edt_p->devid == PGS4_ID) || (edt_p->devid == PGS16_ID))
00951         {
00952             if (size < 2370000) /* approx. min xcvp250 size */
00953             {
00954                 edt_msg(DEBUG2, "size %d, out of range for GS; skipping (-o to override)\n", size);
00955                 return -1;
00956             }
00957         }
00958 
00959 
00960         if ((edt_p->devid == PSS4_ID) || (edt_p->devid == PSS16_ID))
00961         {
00962             if ((size < 494000) || (size > 1280000))
00963             {
00964                 edt_msg(DEBUG2, "size %d out of range for SS; skipping (-o to override)\n", size);
00965                 return -1;      /* approx. min/max xcv600e, xcv1000E, xcv2000e size */
00966             }
00967         }
00968     }
00969 
00970 
00971     if ((xfd = fopen(bitpath, "rb")) == NULL)
00972     {
00973         edt_msg_perror(DEBUG2, bitpath);
00974         edt_msg(DEBUG2, "could not open file <%s>\n", bitpath);
00975     }
00976     else
00977     {
00978         if (!skip)
00979         {
00980             if (bitload_prog_xilinx(edt_p, xfd, header, alt, alt_param,
00981                 do_sleep) == 0)
00982             {
00983                 edt_msg(DEBUG1, "file <%s>\n", bitpath);
00984                 edt_msg(DEBUG1, "id: \"%s\" loaded", header, try);
00985                 if (try == 1)
00986                     edt_msg(DEBUG1, " (0 retries)\n");
00987                 else edt_msg(DEBUG1, " (%d%s try)\n", try, try==2?"nd":try==3?"rd":"th");
00988                 if (alt == ALT_MEZZANINE) {
00989                     int id = edt_get_board_id(edt_p);
00990                     if (id == MEZZ_OCM || id == MEZZ_NET10G)
00991                         edt_set_mezz_chan_bitpath(edt_p, bitpath, alt_param);
00992                     else 
00993                         edt_set_mezz_bitpath(edt_p, bitpath);
00994                 } else {
00995                     edt_set_bitpath(edt_p, bitpath) ;
00996                 }
00997                 ret = 0;
00998             }
00999             else
01000             {
01001                 edt_msg(DEBUG2, "file <%s>\n", bitpath);
01002                 edt_msg(DEBUG2, "id: \"%s\" -- file/device mismatch\n", header);
01003                 if (alt == ALT_MEZZANINE) {
01004                     if (edt_get_board_id(edt_p) == MEZZ_OCM)
01005                         edt_set_mezz_chan_bitpath(edt_p, "", alt_param);
01006                     else 
01007                         edt_set_mezz_bitpath(edt_p, "");
01008                 } else {
01009                     edt_set_bitpath(edt_p, "") ;
01010                 }
01011             }
01012         }
01013         fclose(xfd);
01014     }
01015     return ret;
01016 }
01017 
01018 
01019 /*
01020 * apparently this isn't used anymore? --doug static u_char flipbits(u_char
01021 * val) { int     i; u_char  ret = 0;
01022 * 
01023 * for (i = 0; i < 8; i++) { if (val & (1 << i)) ret |= 0x80 >> i; } return
01024 * (ret); }
01025 */
01026 
01027 #define X_DATA          0x010000
01028 #define X_CCLK          0x020000
01029 #define X_PROG          0x040000
01030 #define X_INIT          0x080000
01031 #define X_DONE          0x100000
01032 #define X_INITSTAT      0x200000
01033 
01034 
01035 /*
01036 * program the xilinx part
01037 */
01038 static int
01039 bitload_prog_xilinx(EdtDev * edt_p, FILE * xfile, char *header, int alt, int alt_param, int do_sleep)
01040 {
01041     int     i;
01042     int     size;
01043     size_t  got;
01044 
01045     if (edt_get_x_header(xfile, header, &size) != 0)
01046         return -1;
01047 
01048     if (Prombuf)                /* already read/initialized */
01049     {
01050         free(Prombuf);
01051         Prombuf = NULL;
01052     }
01053 
01054     if ((Prombuf = (u_char *) malloc(size)) == NULL)
01055     {
01056         edt_msg(EDT_MSG_FATAL, "Error allocating memory for bitfile!\n");
01057         return -1;
01058     }
01059 
01060     if ((got = fread(Prombuf, 1, size, xfile)) < (size_t) size)
01061     {
01062         edt_msg(EDT_MSG_FATAL, "Error -- FPGA file truncated, got %d expected %d\n", got, size);
01063         return -1;
01064     }
01065 
01066 #if DUMP_HEX
01067     for (i=0; i<size; i++)
01068     {
01069     if (!(i%16))
01070         printf("\n%07d ", i);
01071     printf("%02x%s", Prombuf[i], i%2?" ":"" );
01072     }
01073 #endif
01074 
01075     if (alt == ALT_INTERFACE) 
01076         return (edt_program_xilinx(edt_p, Prombuf, size, do_sleep));
01077 #ifdef PCD
01078     else if (alt == ALT_MEZZANINE)
01079         return (edt_program_mezzanine(edt_p, Prombuf, size, alt_param));
01080 #endif
01081     else
01082     {
01083         edt_msg(EDT_MSG_FATAL, "Error -- unknown alternate programming!\n");
01084         return -1;
01085     }
01086 }
01087 
01088 /*
01089 * program the xilinx part (nofs)
01090 */
01091 #ifdef NO_FS
01092 static int
01093 bitload_prog_xilinx_nofs(EdtDev * edt_p, u_char *xa, char *header, int do_sleep)
01094 {
01095     int     size;
01096     u_char *start_of_data;
01097 
01098     if ((start_of_data = edt_get_x_array_header(xa, header, &size)) == NULL)
01099         return -1;
01100 
01101     return (edt_program_xilinx(edt_p, start_of_data, size, do_sleep));
01102 }
01103 #endif
01104 
01105 /*
01106 * program the remote camera through mode code bits in the PDV_MODE_CNTL
01107 * register
01108 */
01109 #define RC_MASK         PDV_AIA_MC_MASK
01110 #define RC_X_DATA       PDV_AIA_MC2
01111 #define RC_X_CCLK       PDV_AIA_MC1
01112 #define RC_X_PROG       PDV_AIA_MC3
01113 /*
01114 * program status readback is through the AIA CHAN bits of the status
01115 * register PDV_STAT
01116 */
01117 #define RC_X_INITSTAT   PDV_CHAN_ID1
01118 #define RC_X_DONE       PDV_CHAN_ID0
01119 
01120 static void
01121 rc_prog_write(EdtDev * edt_p, u_char val)
01122 {
01123     u_char  regrd;
01124 
01125     regrd = (u_char) edt_reg_read(edt_p, PDV_MODE_CNTL);
01126     regrd = (regrd & ~RC_MASK) | (val & RC_MASK);
01127     edt_reg_write(edt_p, PDV_MODE_CNTL, regrd);
01128 }
01129 
01130 static  u_char
01131 rc_prog_read(EdtDev * edt_p)
01132 {
01133     return (u_char) (edt_reg_read(edt_p, PDV_STAT));
01134 }
01135 
01136 /*
01137 * program the xilinx part -- Remote Camera version
01138 */
01139 static int
01140 bitload_rc_prog_xilinx(EdtDev * edt_p, FILE * xfile, char *header)
01141 {
01142     int     loop;
01143     int     ret;
01144     u_char  c;
01145     u_char  bit;
01146     u_char  regrd;
01147     u_char *tmpptr;
01148     u_char *endptr;
01149     size_t  got;
01150     int     size;
01151     int     cnt;
01152     int     i;
01153 
01154     if (edt_get_x_header(xfile, header, &size) != 0)
01155         return 1;
01156 
01157     if (Prombuf)                /* already read/initialized */
01158     {
01159         free(Prombuf);
01160         Prombuf = NULL;
01161     }
01162 
01163     if ((Prombuf = (u_char *) malloc(size)) == NULL)
01164     {
01165         edt_msg(EDT_MSG_FATAL, "Error allocating memory for bitfile!\n");
01166         return -1;
01167     }
01168 
01169     if ((got = fread(Prombuf, 1, size, xfile)) < (size_t) size)
01170     {
01171         edt_msg(EDT_MSG_FATAL, "Error -- FPGA file truncated, got %d expected %d\n", got, size);
01172         return -1;
01173     }
01174 
01175     /* set PROG  DIN CCLK all low */
01176     rc_prog_write(edt_p, 0);
01177 
01178     /* wait for done to be low */
01179     loop = 0;
01180     /* edt_msg(DEBUG2, "\nwaiting for DONE low\n"); */
01181     while ((regrd = rc_prog_read(edt_p)) & RC_X_DONE)
01182     {
01183         if (loop++ > 10000000)
01184         {
01185             /*
01186             * edt_msg(DEBUG2, "reading %02x waiting for not DONE %02x\n",
01187             * regrd, RC_X_DONE);
01188             */
01189             return (1);
01190         }
01191     }
01192     edt_msg(DEBUG2, "loop %d\n", loop);
01193     /* set PROG high */
01194     rc_prog_write(edt_p, RC_X_PROG);
01195 
01196     /* wait for INITSTAT to go high */
01197     edt_msg(DEBUG2, "waiting for INIT high\n");
01198     loop = 0;
01199     while (((regrd = rc_prog_read(edt_p)) & RC_X_INITSTAT) == 0)
01200     {
01201         /*
01202         * edt_msg(DEBUG2, "reading %02x waiting for not INIT %08x\n", regrd,
01203         * RC_X_INITSTAT);
01204         */
01205         if (loop++ > 1000000)
01206         {
01207             /*
01208             * edt_msg(DEBUG2, "reading %02x waiting for not INIT %08x\n",
01209             * regrd, RC_X_INITSTAT);
01210             */
01211             return (1);
01212         }
01213     }
01214     edt_msg(DEBUG2, "loop %d\n", loop);
01215 
01216     tmpptr = Prombuf;
01217     endptr = &Prombuf[size];
01218     /* write all data bits */
01219     ret = 0;
01220     cnt = 0;
01221     while (tmpptr < endptr && (ret == 0))
01222     {
01223         c = *tmpptr++;
01224         for (i = 0; i < 8; i++)
01225         {
01226             cnt++;
01227             bit = c & 0x80;
01228             c <<= 1;
01229             if (bit)
01230             {
01231                 rc_prog_write(edt_p, RC_X_DATA | RC_X_CCLK | RC_X_PROG);
01232                 rc_prog_write(edt_p, RC_X_DATA | RC_X_PROG);
01233             }
01234             else
01235             {
01236                 rc_prog_write(edt_p, RC_X_CCLK | RC_X_PROG);
01237                 rc_prog_write(edt_p, RC_X_PROG);
01238             }
01239         }
01240         if ((cnt % 0x100) == 0)
01241             edt_msg(DEBUG2, "%08x\r", cnt);
01242     }
01243     /* write 1's until DONE is high */
01244 
01245     edt_msg(DEBUG2, "waiting for DONE high\n");
01246     loop = 0;
01247     ret = 0;
01248     edt_msg(DEBUG2, "prog_read %x DONE %x\n", rc_prog_read(edt_p), RC_X_DONE);
01249     while ((((regrd = rc_prog_read(edt_p)) & RC_X_DONE) == 0) && (ret == 0))
01250     {
01251         rc_prog_write(edt_p, RC_X_DATA | RC_X_CCLK | RC_X_PROG);
01252         rc_prog_write(edt_p, RC_X_DATA | RC_X_PROG);
01253         if (loop > CLKSAFTER)
01254         {
01255             edt_msg(DEBUG2, "> %d writes without done - fail\n", CLKSAFTER);
01256             ret = -1;
01257         }
01258         loop++;
01259     }
01260 
01261     return (ret);
01262 }
01263 
01264 
01265 /*
01266 * look for valid bitfile dir name -- should 4 or more chars and start with a
01267 * number from 4-9, or 'X'
01268 */
01269 static int
01270 valid_bit_dirname(char *name)
01271 {
01272     if ((strlen(name) < 4)
01273         || (((name[0] != 'X') && (name[0] != 'x'))
01274         && ((name[0] < '4') || (name[0]) > '9')))
01275         return 0;
01276     return 1;
01277 }
01278 
01279 
01280 int
01281 has_slashes(char *name)
01282 {
01283     char   *p = name;
01284 
01285     while (*p)
01286     {
01287         if ((*p == '/') || (*p == '\\'))
01288             return 1;
01289         ++p;
01290     }
01291     return 0;
01292 }
01293 
01294 /*
01295 * fill in the devdir string based on the device ID
01296 */
01297 void
01298 edt_bitload_devid_to_bitdir(EdtDev * edt_p, char *devdir)
01299 {
01300     switch(edt_p->devid)
01301     {
01302     case PDV44_ID:
01303     case PDVK_ID:
01304         strcpy(devdir, "dvk");
01305         break;
01306 
01307     case PDV_ID:
01308     case PGP_RGB_ID:
01309         strcpy(devdir, "dv");
01310         break;
01311 
01312     case PDVA_ID:
01313     case PDVA16_ID:
01314         strcpy(devdir, "dva");
01315         break;
01316 
01317     case PDVFOX_ID:
01318         strcpy(devdir, "dvfox");
01319         break;
01320 
01321     case PDVFCI_AIAG_ID:
01322     case PDVFCI_USPS_ID:
01323         strcpy(devdir, "dvfci");
01324         break;
01325 
01326     case PDVAERO_ID:
01327         strcpy(devdir, "dvaero");
01328         break;
01329 
01330     case PDVCL2_ID:
01331         strcpy(devdir, "dvcl2");
01332         break;
01333 
01334     default:
01335         devdir[0] = '\0';
01336     }
01337 }
01338 
01339 /* Load the OC192 mezzanine */
01340 
01341 #define PROGL 0x08
01342 #define INITL_OUTPUT_OFF 0x04
01343 #define CCLK_ON 0x10
01344 #define PROG_ENABLE 0x20
01345 #define BUF_ENABLE 0x40
01346 
01347 #define INITL 0x01
01348 #define DONE  0x02
01349 
01350 #define DATA_REG 0x40   /* data register */
01351 #define PROG_REG 0x41   /* control register for loading xilinx */
01352 #define STAT_REG 0x42   /* status of programming phase */
01353 
01354 
01355 

Generated on Mon May 12 16:38:52 2008 by  doxygen 1.5.1