jtag.c

Go to the documentation of this file.
00001 //
00002 // JTAG.C
00003 //
00004 // Low level JTAG access code.  Handles all clocking of data in and
00005 // out of target hardware.
00006 //
00007 // Copyright (c) 2002, Jason Riffel - TotalEmbedded LLC.
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without 
00011 // modification, are permitted provided that the following conditions 
00012 // are met:
00013 //
00014 // Redistributions of source code must retain the above copyright 
00015 // notice, this list of conditions and the following disclaimer. 
00016 //
00017 // Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in 
00019 // the documentation and/or other materials provided with the
00020 // distribution. 
00021 //
00022 // Neither the name of TotalEmbedded nor the names of its 
00023 // contributors may be used to endorse or promote products derived 
00024 // from this software without specific prior written permission.
00025 // 
00026 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00027 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00028 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
00029 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00030 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
00031 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
00032 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00033 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00034 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00035 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
00036 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037 // POSSIBILITY OF SUCH DAMAGE.
00038 //
00039 
00040 //
00041 // INCLUDES
00042 //
00043 #include <errno.h>
00044 #include <signal.h>
00045 #include <stdio.h>
00046 #include <unistd.h>
00047 #include <sys/time.h>
00048 #include <sys/types.h>
00049 #include <sys/socket.h>
00050 #include <sys/wait.h>
00051 #include <netinet/in.h>
00052 #include <netdb.h>
00053 #include "jtag.h"
00054 #include "parport.h"
00055 
00056 //
00057 // DEFINES
00058 //
00059 // Define the pins each signal is assigned to on the data port
00060 #define dPARPORT_TAP_RESET_BIT     0x03
00061 #define dPARPORT_TAP_DI_BIT        0x06
00062 #define dPARPORT_TAP_MS_BIT        0x01
00063 #define dPARPORT_TAP_CLK_BIT       0x00
00064 #define dPARPORT_TARGET_RESET_BIT  0x04
00065 
00066 // Define the bit in the status register that is our TAP_DI
00067 #define dPARPORT_TAP_DO_BIT        0x80
00068 
00069 // Macro to calculate the bit settings when writing to the data port
00070 #define mPARPORT_TRS_BIT(x) (x << dPARPORT_TAP_RESET_BIT   )
00071 #define mPARPORT_TMS_BIT(x) (x << dPARPORT_TAP_MS_BIT      )
00072 #define mPARPORT_TDI_BIT(x) (x << dPARPORT_TAP_DI_BIT      )
00073 #define mPARPORT_TCK_BIT(x) (x << dPARPORT_TAP_CLK_BIT     )
00074 #define mPARPORT_RST_BIT(x) (x << dPARPORT_TARGET_RESET_BIT)
00075 
00076 //
00077 // PROTOTYPES
00078 //
00079 static unsigned char fJTAG_Wiggle(unsigned char uc_tms, unsigned char uc_tdo);
00080 
00081 //
00082 // fJTAG_Init
00083 //
00084 // Initializes the state of the JTAG hardware
00085 //
00086 void fJTAG_Init(void)
00087 {
00088   // First we need to init the parallel port
00089   fPARPORT_Init();
00090   
00091   // Wiggle the TAP_RESET bit - Be mindful of the target reset bit
00092 //  fPARPORT_Write((unsigned char)mPARPORT_RST_BIT(1)|mPARPORT_TRS_BIT(0));
00093 //  fPARPORT_Write((unsigned char)mPARPORT_RST_BIT(1)|mPARPORT_TRS_BIT(1));
00094 
00095   // Run through a handful of clock cycles with TMS high to make sure
00096   // we are in the TEST-LOGIC-RESET state.
00097   fJTAG_Wiggle(1,0);
00098   fJTAG_Wiggle(1,0);
00099   fJTAG_Wiggle(1,0);
00100   fJTAG_Wiggle(1,0);
00101   fJTAG_Wiggle(1,0);
00102   fJTAG_Wiggle(1,0);
00103   
00104   // Enter the RUN-TEST-IDLE state
00105   fJTAG_Wiggle(0,0);
00106 
00107 
00108   fJTAG_Instruction(1);
00109   printf("IDCODE=0x%08x\n",fJTAG_Data(1));
00110   fJTAG_Instruction(3);
00111   printf("IMPCODE=0x%08x\n",fJTAG_Data(3));
00112 
00113   return;
00114 }
00115 
00116 //
00117 // fJTAG_Wiggle
00118 //
00119 // Sets states of lines out to JTAG controller and returns
00120 // the one input bit from status (which is inverted in hardware)
00121 //
00122 static unsigned char fJTAG_Wiggle(unsigned char uc_tms, unsigned char uc_tdo)
00123 {
00124   unsigned char uc_dout;
00125   unsigned char uc_din;
00126 
00127   uc_dout = (unsigned char)(mPARPORT_TRS_BIT(1)|mPARPORT_TMS_BIT(uc_tms)|
00128                             mPARPORT_TDI_BIT(uc_tdo)|mPARPORT_TCK_BIT(0)|
00129                             mPARPORT_RST_BIT(1));
00130   fPARPORT_Write(uc_dout);
00131 
00132   uc_dout = (unsigned char)(mPARPORT_TRS_BIT(1)|mPARPORT_TMS_BIT(uc_tms)|
00133                             mPARPORT_TDI_BIT(uc_tdo)|mPARPORT_TCK_BIT(1)|
00134                             mPARPORT_RST_BIT(1));
00135   fPARPORT_Write(uc_dout);
00136 
00137   // Read in the status bit (inverted in hardware)
00138   uc_din = fPARPORT_Read() & dPARPORT_TAP_DO_BIT ? 0 : 1;
00139 
00140   return uc_din;
00141 }
00142 
00143 //
00144 // fJTAG_Data
00145 //
00146 // Write/Read from/to the JTAG data register
00147 //
00148 unsigned int fJTAG_Data (unsigned int ui_data_out)
00149 {
00150   unsigned int  ui_data_in;
00151   unsigned char uc_index;
00152   unsigned char uc_result;
00153   
00154   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00155   fJTAG_Wiggle(0, 0);  // enter capture-dr
00156   fJTAG_Wiggle(0, 0);  // enter shift-dr
00157 
00158   ui_data_in = 0x00;
00159 
00160   // Clock all but last bit out
00161   for (uc_index=0; uc_index<31; uc_index++)
00162     {
00163       uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00164       ui_data_out = ui_data_out >> 1;
00165       ui_data_in  = ui_data_in | (uc_result << uc_index);
00166     }
00167   // On last bit set the TAP_MS
00168   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00169   ui_data_in  = ui_data_in | (uc_result << uc_index);
00170 
00171   fJTAG_Wiggle(1, 0);  // enter update-dr
00172   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00173 
00174   return(ui_data_in);
00175 }
00176 
00177 //
00178 // Write/Read from/to the JTAG Fastdata register
00179 //
00180 unsigned int fJTAG_FastData (unsigned int ui_data_out)
00181 {
00182   unsigned int  ui_data_in;
00183   unsigned char uc_index;
00184   unsigned char uc_result;
00185   
00186   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00187   fJTAG_Wiggle(0, 0);  // enter capture-dr
00188   fJTAG_Wiggle(0, 0);  // enter shift-dr
00189 
00190   ui_data_in = 0x00;
00191 
00192       fJTAG_Wiggle(0, 0);  // write a 0 to the Fastdata register
00193   // Clock all but last bit out
00194   for (uc_index=0; uc_index<31; uc_index++)
00195     {
00196       uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00197       ui_data_out = ui_data_out >> 1;
00198       ui_data_in  = ui_data_in | (uc_result << uc_index);
00199     }
00200   // On last bit set the TAP_MS
00201   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00202   ui_data_in  = ui_data_in | (uc_result << uc_index);
00203 
00204   fJTAG_Wiggle(1, 0);  // enter update-dr
00205   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00206 
00207   return(ui_data_in);
00208 }
00209 
00210 //
00211 // Write/Read from/to the JTAG DBTX register
00212 //
00213 unsigned int fJTAG_DBTX(unsigned int ui_data_out)
00214 {
00215   unsigned int  ui_data_in;
00216   unsigned char uc_index;
00217   unsigned char uc_result;
00218   
00219   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00220   fJTAG_Wiggle(0, 0);  // enter capture-dr
00221   fJTAG_Wiggle(0, 0);  // enter shift-dr
00222 
00223   ui_data_in = 0x00;
00224 
00225       fJTAG_Wiggle(0, 0);  // write a 0 to the Fastdata register
00226   // Clock all but last bit out
00227   for (uc_index=0; uc_index<31; uc_index++)
00228     {
00229       uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00230       ui_data_out = ui_data_out >> 1;
00231       ui_data_in  = ui_data_in | (uc_result << uc_index);
00232     }
00233   // On last bit set the TAP_MS
00234   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00235   ui_data_in  = ui_data_in | (uc_result << uc_index);
00236 
00237   fJTAG_Wiggle(1, 0);  // enter update-dr
00238   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00239 
00240   return(ui_data_in);
00241 }
00242 
00243 #if 0
00244 //
00245 // Read from the JTAG DBDX register
00246 //
00247 unsigned int fJTAG_DBDX_TEST(unsigned int ui_data_out)
00248 {
00249   unsigned int  ui_data_in;
00250   unsigned char uc_index;
00251   unsigned int uc_result=0x00;
00252   unsigned int uc_result1=0x00;
00253   
00254   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00255   fJTAG_Wiggle(0, 0);  // enter capture-dr
00256   fJTAG_Wiggle(0, 0);  // enter shift-dr
00257 
00258   ui_data_in = 0x00;
00259 
00260       uc_result1=fJTAG_Wiggle(0, 1);  // write a 1 to the JTAG DBDX register
00261   // Clock all but last bit out
00262   for (uc_index=0; uc_index<31; uc_index++)
00263     {
00264       uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00265       ui_data_out = ui_data_out >> 1;
00266       ui_data_in  = ui_data_in | (uc_result << uc_index);
00267     }
00268   // On last bit set the TAP_MS
00269   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00270   ui_data_in  = ui_data_in | (uc_result << uc_index);
00271 
00272   fJTAG_Wiggle(1, 0);  // enter update-dr
00273   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00274 //  if(uc_result1)
00275 //      printf("fJTAG_DBDX_TEST : 0x%x\n",uc_result1);
00276   return(uc_result1);
00277 }
00278 #endif
00279 
00280 #if 1
00281 //
00282 // Read from the JTAG DBDX register
00283 //
00284 unsigned int fJTAG_DBDX_TEST(unsigned int ui_data_out)
00285 {
00286   unsigned int  ui_data_in;
00287 //  unsigned char uc_index;
00288   unsigned int uc_result=0x00;
00289   
00290   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00291   fJTAG_Wiggle(0, 0);  // enter capture-dr
00292   fJTAG_Wiggle(0, 0);  // enter shift-dr
00293 
00294 //  ui_data_in = 0x00;
00295 
00296 //      fJTAG_Wiggle(0, 0);  // write a 0 to the JTAG DBDX register
00297   // Clock all but last bit out
00298 //  for (uc_index=0; uc_index<31; uc_index++)
00299 //    {
00300 //      uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00301 //      ui_data_out = ui_data_out >> 1;
00302 //      ui_data_in  = ui_data_in | (uc_result << uc_index);
00303 //    }
00304   // On last bit set the TAP_MS
00305   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00306 //  ui_data_in  = ui_data_in | (uc_result << uc_index);
00307 
00308   fJTAG_Wiggle(1, 0);  // enter update-dr
00309   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00310   if(uc_result)
00311         printf("fJTAG_DBDX_TEST : 0x%x\n",uc_result);
00312   return(uc_result);
00313 }
00314 #endif
00315 
00316 //
00317 // Read from the JTAG DBDX register
00318 //
00319 unsigned int fJTAG_DBDX(unsigned int ui_data_out)
00320 {
00321   unsigned int  ui_data_in;
00322   unsigned char uc_index;
00323   unsigned char uc_result;
00324   
00325   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00326   fJTAG_Wiggle(0, 0);  // enter capture-dr
00327   fJTAG_Wiggle(0, 0);  // enter shift-dr
00328 
00329   ui_data_in = 0x00;
00330 
00331       fJTAG_Wiggle(0, 0);  // write a 0 to the JTAG DBDX register
00332   // Clock all but last bit out
00333   for (uc_index=0; uc_index<31; uc_index++)
00334     {
00335       uc_result   = fJTAG_Wiggle(0, (ui_data_out & 0x01));
00336       ui_data_out = ui_data_out >> 1;
00337       ui_data_in  = ui_data_in | (uc_result << uc_index);
00338     }
00339   // On last bit set the TAP_MS
00340   uc_result   = fJTAG_Wiggle(1, (ui_data_out & 0x01));
00341   ui_data_in  = ui_data_in | (uc_result << uc_index);
00342 
00343   fJTAG_Wiggle(1, 0);  // enter update-dr
00344   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00345 //zgj printf("fJTAG_DBDX===data : 0x%x\n",ui_data_in);
00346   return(ui_data_in);
00347 }
00348 
00349 //
00350 // fJTAG_Instruction
00351 //
00352 // Read/Write from/to the instruction register
00353 //
00354 unsigned char fJTAG_Instruction(unsigned char uc_data_out)
00355 {
00356   unsigned char uc_data_in;
00357   unsigned char uc_result;
00358   unsigned char uc_index;
00359 
00360   fJTAG_Wiggle(1, 0);  // enter select-dr-scan
00361   fJTAG_Wiggle(1, 0);  // enter select-ir-scan
00362   fJTAG_Wiggle(0, 0);  // enter capture-ir
00363   fJTAG_Wiggle(0, 0);  // enter shift-ir (dummy)
00364 
00365   uc_data_in=0;
00366   
00367   // Clock all but last bit out
00368   for (uc_index=0; uc_index<4; uc_index++)
00369     {
00370       uc_result   = fJTAG_Wiggle(0, (uc_data_out & 0x01));
00371       uc_data_out = uc_data_out >> 1;
00372       uc_data_in  = uc_data_in | (uc_result << uc_index);
00373     }
00374   // On last bit set the TAP_MS
00375   uc_result   = fJTAG_Wiggle(1, (uc_data_out & 0x01));
00376   uc_data_in  = uc_data_in | (uc_result << uc_index);
00377   
00378   fJTAG_Wiggle(1, 0);  // enter update-ir
00379   fJTAG_Wiggle(0, 0);  // enter runtest-idle
00380 
00381   return(uc_data_in);
00382 }
00383 
00384 unsigned int fJTAG_FAST_UPLOAD(unsigned int upload_count)
00385 {
00386  printf("error no this method fJTAG_FAST_UPLOAD\n");
00387  exit(-1);
00388  return -1;
00389 }
00390 
00391 void fJTAG_unInit(void)
00392 {
00393 }
00394 
00395 
00396 unsigned int fJTAG_UPLOAD(unsigned char *buf,unsigned int upload_count)
00397 {
00398  printf("error no this method fJTAG_UPLOAD\n");
00399  exit(-1);
00400  return -1;
00401 }
00402 

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6