5e76aa522f7d2842c3bb8bdf59a440027303af03
[goodfet] / firmware / apps / jtag / jtag430x2.c
1 /*! \file jtag430x2.c
2   \author Travis Goodspeed <travis at radiantmachines.com>
3   
4   This is an implementation of the MSP430X2 JTAG protocol
5   for the GoodFET project at http://goodfet.sf.net/
6   
7   See the license file for details of proper use.
8 */
9
10 #include "platform.h"
11 #include "command.h"
12 #include "jtag.h"
13
14 unsigned char jtagid;
15
16 //! Get the JTAG ID
17 unsigned char jtag430x2_jtagid(){
18   jtag430_resettap();
19   return jtagid=jtag_ir_shift8(IR_BYPASS);
20 }
21 //! Start JTAG, take pins
22 unsigned char jtag430x2_start(){
23   jtagsetup();
24   
25   //Known-good starting position.
26   //Might be unnecessary.
27   SETTST;
28   SETRST;
29   
30   delay(0xFFFF);
31   
32   //Entry sequence from Page 67 of SLAU265A for 4-wire MSP430 JTAG
33   CLRRST;
34   delay(10);
35   CLRTST;
36
37   delay(5);
38   SETTST;
39   msdelay(5);
40   SETRST;
41   P5DIR&=~RST;
42   
43   delay(0xFFFF);
44   
45   //Perform a reset and disable watchdog.
46   return jtag430x2_jtagid();
47 }
48
49 //! Grab the core ID.
50 unsigned int jtag430_coreid(){
51   jtag_ir_shift8(IR_COREIP_ID);
52   return jtag_dr_shift16(0);
53 }
54
55 //! Grab the device ID.
56 unsigned long jtag430_deviceid(){
57   jtag_ir_shift8(IR_DEVICE_ID);
58   return jtag_dr_shift20(0);
59 }
60
61
62 //! Write data to address
63 unsigned int jtag430x2_writemem(unsigned long adr,
64                                 unsigned long data){
65   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
66   if(jtag_ir_shift8(0) & 0x0301){
67     CLRTCLK;
68     jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
69     if(adr>=0x100)
70       jtag_dr_shift16(0x0500);//word mode
71     else
72       jtag_dr_shift16(0x0510);//byte mode
73     jtag_ir_shift8(IR_ADDR_16BIT);
74     jtag_dr_shift20(adr);
75     
76     SETTCLK;
77     
78     jtag_ir_shift8(IR_DATA_TO_ADDR);
79     jtag_dr_shift16(data);//16 word
80
81     CLRTCLK;
82     jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
83     jtag_dr_shift16(0x0501);
84     SETTCLK;
85
86     CLRTCLK;
87     SETTCLK;
88     //init state
89   }else{
90     while(1) P1OUT^=1; //loop if locked up
91   }
92 }
93
94 //! Read data from address
95 unsigned int jtag430x2_readmem(unsigned long adr){
96   unsigned int toret=0;
97
98   jtag_ir_shift8(IR_CNTRL_SIG_CAPTURE);
99   if(jtag_dr_shift16(0) & 0x0301){
100     // Read Memory                                                                                                                                                                 
101     CLRTCLK;
102     jtag_ir_shift8(IR_CNTRL_SIG_16BIT);
103     if(adr>=0x100){
104       jtag_dr_shift16(0x0501);//word read
105     }else{
106       jtag_dr_shift16(0x0511);//byte read
107     }
108     jtag_ir_shift8(IR_ADDR_16BIT);
109     jtag_dr_shift20(adr); //20
110
111     jtag_ir_shift8(IR_DATA_TO_ADDR);
112     SETTCLK;
113     CLRTCLK;
114     toret = jtag_dr_shift16(0x0000);
115     
116     SETTCLK;
117     // one or more cycle, so CPU is driving correct MAB
118
119     CLRTCLK;
120     SETTCLK;
121     // Processor is now again in Init State
122   }else{
123     return 0xDEAD;
124   }
125   
126   return toret;
127 }
128
129
130 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
131 void jtag430x2handle(unsigned char app,
132                    unsigned char verb,
133                    unsigned char len){
134   jtag430_resettap();
135   switch(verb){
136   case START:
137     //Enter JTAG mode.
138     do cmddata[0]=jtag430x2_start();
139     while(cmddata[0]==00 || cmddata[0]==0xFF);
140     
141     //MSP430 or MSP430X
142     if(jtagid==MSP430JTAGID){ 
143       jtag430mode=MSP430MODE;
144       drwidth=16;
145     }else if(jtagid==MSP430X2JTAGID){
146       jtag430mode=MSP430X2MODE;
147       drwidth=20;
148     }else{
149       txdata(app,NOK,1);
150       return;
151     }
152     
153     //TAP setup, fuse check
154     //jtag430_resettap();
155     txdata(app,verb,1);
156     break;
157   case JTAG430_READMEM:
158   case PEEK:
159     //cmddataword[0]=jtag430x2_readmem(cmddataword[0]);
160     cmddataword[0]=jtag430x2_readmem(cmddatalong[0]);
161     txdata(app,verb,2);
162     break;
163   case JTAG430_COREIP_ID:
164     cmddataword[0]=jtag430_coreid();
165     txdata(app,verb,2);
166     break;
167   case JTAG430_DEVICE_ID:
168     cmddatalong[0]=jtag430_deviceid();
169     txdata(app,verb,4);
170     break;
171   case JTAG430_HALTCPU:
172   case JTAG430_RELEASECPU:
173   case JTAG430_SETINSTRFETCH:
174   case JTAG430_WRITEMEM:
175   case POKE:
176     jtag430x2_writemem(cmddataword[0],
177                        cmddataword[1]);
178     cmddataword[0]=jtag430x2_readmem(cmddataword[0]);
179     txdata(app,verb,2);
180     break;
181   case JTAG430_WRITEFLASH:
182   case JTAG430_ERASEFLASH:
183   case JTAG430_SETPC:
184   default:
185     jtaghandle(app,verb,len);
186   }
187   jtag430_resettap();
188 }